Simple Feed-Forward NN in Pytorch#

Building a simple feedforward neural network using PyTorch involves defining the network architecture, specifying the forward pass computation, and training the network on a dataset. In this example, we’ll create a basic feedforward neural network for a binary classification task using PyTorch.

Step 1: Import Libraries#

[1]:
import torch
import torch.nn as nn
import torch.optim as optim

Step 2: Define the Neural Network Architecture#

[2]:
class SimpleNN(nn.Module):
    def __init__(self, input_size, hidden_size, output_size):
        super(SimpleNN, self).__init__()
        self.fc1 = nn.Linear(input_size, hidden_size)
        self.relu = nn.ReLU()
        self.fc2 = nn.Linear(hidden_size, output_size)
        self.sigmoid = nn.Sigmoid()

    def forward(self, x):
        x = self.fc1(x)
        x = self.relu(x)
        x = self.fc2(x)
        x = self.sigmoid(x)
        return x

Step 3: Instantiate the Model#

[3]:
input_size = 10  # Example: Number of input features
hidden_size = 64
output_size = 1  # Binary classification (1 output neuron)
model = SimpleNN(input_size, hidden_size, output_size)

Step 4: Define Loss Function and Optimizer#

[4]:
criterion = nn.BCELoss()  # Binary Cross-Entropy Loss
optimizer = optim.SGD(model.parameters(), lr=0.01)  # Stochastic Gradient Descent

Step 5: Prepare Data (Example)#

[5]:
# Example data (features and labels)
X_train = torch.randn(100, input_size)
y_train = torch.randint(0, 2, (100, 1)).float()

Step 6: Train the Model#

[6]:
num_epochs = 100
for epoch in range(num_epochs):
    # Forward pass
    outputs = model(X_train)
    loss = criterion(outputs, y_train)

    # Backward pass and optimization
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

    # Print progress
    if (epoch+1) % 10 == 0:
        print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}')
Epoch [10/100], Loss: 0.7338
Epoch [20/100], Loss: 0.7244
Epoch [30/100], Loss: 0.7170
Epoch [40/100], Loss: 0.7111
Epoch [50/100], Loss: 0.7064
Epoch [60/100], Loss: 0.7024
Epoch [70/100], Loss: 0.6991
Epoch [80/100], Loss: 0.6963
Epoch [90/100], Loss: 0.6938
Epoch [100/100], Loss: 0.6916

Step 7: Evaluate the Model (Optional)#

You can evaluate the trained model on a validation set or test set to assess its performance.

[8]:
# Example evaluation
with torch.no_grad():
    # Prepare validation data
    X_val = torch.randn(20, input_size)
    y_val = torch.randint(0, 2, (20, 1)).float()

    # Forward pass
    outputs = model(X_val)
    predictions = (outputs > 0.5).float()

    # Calculate accuracy
    accuracy = (predictions == y_val).float().mean()
    print(f'Validation Accuracy: {accuracy.item()*100:.2f}%')
Validation Accuracy: 45.00%

Conclusion#

This example demonstrates how to build, train, and evaluate a simple feedforward neural network for a binary classification task using PyTorch. You can customize the network architecture, loss function, optimizer, and training parameters based on your specific task and dataset. Experiment with different architectures and hyperparameters to improve the performance of your neural network.