Training the NN on a toy dataset#
To train a neural network on a toy dataset using PyTorch, we’ll follow these steps:
Prepare a toy dataset.
Define the neural network architecture.
Specify the loss function and optimizer.
Iterate over the dataset in mini-batches, performing forward and backward passes.
Monitor training progress and evaluate the model.
Let’s create a simple example where we generate a toy dataset and train a feedforward neural network to classify it.
Step 1: Prepare a Toy Dataset#
For simplicity, let’s create a toy dataset with random features and labels.
[1]:
import torch
from torch.utils.data import Dataset, DataLoader
class ToyDataset(Dataset):
def __init__(self, num_samples):
self.num_samples = num_samples
self.X = torch.randn(num_samples, 2) # Two-dimensional features
self.y = torch.randint(0, 2, (num_samples,)) # Binary labels (0 or 1)
def __len__(self):
return self.num_samples
def __getitem__(self, idx):
return self.X[idx], self.y[idx]
# Create the toy dataset
toy_dataset = ToyDataset(num_samples=100)
Step 2: Define the Neural Network Architecture#
Let’s define a simple feedforward neural network architecture for binary classification.
[2]:
import torch.nn as nn
class SimpleNN(nn.Module):
def __init__(self):
super(SimpleNN, self).__init__()
self.fc1 = nn.Linear(2, 64)
self.relu = nn.ReLU()
self.fc2 = nn.Linear(64, 1)
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
# Instantiate the model
model = SimpleNN()
Step 3: Specify the Loss Function and Optimizer#
Let’s use binary cross-entropy loss and stochastic gradient descent (SGD) optimizer.
[3]:
import torch.optim as optim
criterion = nn.BCELoss()
optimizer = optim.SGD(model.parameters(), lr=0.01)
Step 4: Train the Model#
Iterate over the dataset in mini-batches, performing forward and backward passes.
num_epochs = 100 batch_size = 10 dataloader = DataLoader(toy_dataset, batch_size=batch_size, shuffle=True)
for epoch in range(num_epochs): running_loss = 0.0 for i, data in enumerate(dataloader, 0): inputs, labels = data
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs.squeeze(), labels.float())
loss.backward()
optimizer.step()
running_loss += loss.item()
print(f'Epoch {epoch+1}/{num_epochs}, Loss: {running_loss / len(dataloader):.4f}')
Step 5: Evaluate the Model#
You can evaluate the trained model on a validation set or test set to assess its performance.
[4]:
# Example evaluation on the entire dataset
with torch.no_grad():
outputs = model(toy_dataset.X)
predicted = (outputs > 0.5).squeeze().int()
accuracy = (predicted == toy_dataset.y).float().mean()
print(f'Accuracy: {accuracy.item()*100:.2f}%')
Accuracy: 54.00%
Conclusion#
In this example, we trained a simple feedforward neural network on a toy dataset for binary classification using PyTorch. You can customize the dataset, neural network architecture, loss function, optimizer, and training parameters based on your specific task and dataset. Experiment with different configurations to improve the performance of your neural network.