Note_Tech

All technological notes.


Project maintained by simonangel-fong Hosted on GitHub Pages — Theme by mattgraham

Deep Learning - Multilayer Perceptrons (MLP)

Back


Terminology


Forward Propagation vs Backward Propagation



Example

import torch
import torch.nn as nn
import torch.optim as optim

# Example neural network architecture with one hidden layer
class ExampleModel(nn.Module):
    def __init__(self, input_size, hidden_size, output_size):
        super(ExampleModel, self).__init__()
        self.fc1 = nn.Linear(input_size, hidden_size)
        self.activation = nn.Sigmoid()
        self.fc2 = nn.Linear(hidden_size, output_size)

    def forward(self, x):
        # Forward Propagation
        hidden_layer_output = self.activation(self.fc1(x))
        predicted_output = self.activation(self.fc2(hidden_layer_output))
        return predicted_output, hidden_layer_output

# Example data
input_data = torch.tensor([[0, 1], [1, 0], [1, 1], [0, 0]], dtype=torch.float32)
target_output = torch.tensor([[1], [1], [0], [0]], dtype=torch.float32)

# Initialize the neural network, loss function, and optimizer
input_size = 2
hidden_size = 2
output_size = 1
learning_rate = 0.1
epochs = 10000

model = ExampleModel(input_size, hidden_size, output_size)
criterion = nn.MSELoss()
optimizer = optim.SGD(model.parameters(), lr=learning_rate)

# Training the neural network
for epoch in range(epochs):
    # Forward Propagation
    predicted_output, _ = model(input_data)

    # Compute the loss
    loss = criterion(predicted_output, target_output)

    # Backward Propagation
    optimizer.zero_grad()  # Clear previous gradients
    loss.backward()  # Compute gradients
    optimizer.step()  # Update weights

    # Print the loss every 1000 epochs
    if epoch % 1000 == 0:
        print(f"Epoch {epoch}, Loss: {loss.item()}")

# Test the trained model
test_input = torch.tensor([[1, 1], [0, 1]], dtype=torch.float32)
predicted_output, _ = model(test_input)
print("Predicted Output for Test Input:", predicted_output)


Dropout

class ModelWithDropout(nn.Module):
    def __init__(self, dropout_rate=0.5):
        super(ModelWithDropout, self).__init__()
        self.fc1 = nn.Linear(in_features=..., out_features=...)
        self.dropout = nn.Dropout(p=dropout_rate)               # drop out
        self.fc2 = nn.Linear(in_features=..., out_features=...)

    def forward(self, x):
        x = self.fc1(x)
        x = self.dropout(x)  # Applying dropout after the first layer
        x = torch.relu(x)
        x = self.fc2(x)
        return x

TOP