A single-layer neural network, often referred to as a single-layer perceptron, is the simplest form of a neural network. This type of model consists of only one layer of neurons, connected directly to both the input and output layers. In the case of an image classifier, the input layer represents the image, while the output layer assigns a class label.
In this tutorial, you will learn how to construct an image classifier using a single-layer neural network implemented in PyTorch. Specifically, you’ll cover:
- How to prepare your data.
- How to define the neural network model.
- How to train and evaluate the classifier.
Let’s get started!
Overview
This tutorial is organized into three primary sections:
- Preparing the Dataset
- Building the Model Architecture
- Training the Model
Preparing the Dataset
For this tutorial, you will use the CIFAR-10 dataset, which consists of 60,000 color images, each 32×32 pixels, categorized into 10 classes with 6,000 images per class. These classes include airplanes, cars, birds, cats, deer, dogs, frogs, horses, ships, and trucks. The CIFAR-10 dataset is a popular resource for machine learning and computer vision research due to its manageable size and versatility.
You can easily load the CIFAR-10 dataset using PyTorch as follows:
import torch
import torchvision
import torchvision.transforms as transforms
# Load the CIFAR-10 dataset
train_set = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transforms.ToTensor())
test_set = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transforms.ToTensor())
This code will download and extract the dataset if it hasn’t been previously downloaded. To verify the number of training and testing samples, you can check with:
print("Number of training samples: ", len(train_set))
print("Number of testing samples: ", len(test_set))
You should see:
Number of training samples: 60000
Number of testing samples: 10000
Each sample contains an image paired with its corresponding label. To inspect the data type and size of the first training sample, you can use:
print("Data type of the first training sample: ", train_set[0][0].type())
print("Size of the first training sample: ", train_set[0][0].size())
This will yield:
Data type of the first training sample: torch.FloatTensor
Size of the first training sample: torch.Size([3, 32, 32])
This output indicates that the first image is represented as a 32×32 pixel RGB image (3 color channels).
Building the Model Architecture
Next, you’ll build a simple neural network, named SimpleNet
, using PyTorch’s nn.Module
. This model will consist of two fully connected layers. Here’s how to define this model:
import torch.nn as nn
class SimpleNet(nn.Module):
def __init__(self, num_classes=10):
super(SimpleNet, self).__init__()
self.fc1 = nn.Linear(32 * 32 * 3, 100) # Fully connected layer with 100 hidden neurons
self.fc2 = nn.Linear(100, num_classes) # Fully connected layer for output classes
def forward(self, x):
x = x.view(-1, 32 * 32 * 3) # Flatten the input
x = torch.relu(self.fc1(x)) # Apply ReLU activation
x = self.fc2(x) # Output layer
return x
You can then instantiate the model:
# Instantiate the model
model = SimpleNet()
Training the Model
To train this model, you need to create instances of PyTorch’s DataLoader class for both training and testing. Set the training DataLoader with a batch size of 64 and enable shuffling to enhance model training.
Define the loss function and optimizer for your model training:
from torch.utils.data import DataLoader
# Load data into DataLoader
train_loader = DataLoader(dataset=train_set, batch_size=64, shuffle=True)
test_loader = DataLoader(dataset=test_set, batch_size=64, shuffle=False)
# Define the loss function and optimizer
criterion = nn.CrossEntropyLoss() # Suitable for multi-class classification
optimizer = torch.optim.SGD(model.parameters(), lr=0.001) # Stochastic Gradient Descent
Now you’ll set up a training loop to train your model over a specified number of epochs:
num_epochs = 20
train_loss_history = []
train_acc_history = []
val_loss_history = []
val_acc_history = []
for epoch in range(num_epochs):
train_loss = 0.0
train_acc = 0.0
# Set model to training mode
model.train()
for inputs, labels in train_loader:
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, labels)
train_loss += loss.item()
train_acc += (outputs.argmax(1) == labels).sum().item() # Count correct predictions
loss.backward() # Backpropagation
optimizer.step() # Update parameters
# Calculate average training loss and accuracy
train_loss /= len(train_loader)
train_loss_history.append(train_loss)
train_acc /= len(train_loader.dataset)
train_acc_history.append(train_acc)
print(f'Epoch: {epoch + 1}/{num_epochs}, Train Loss: {train_loss:.4f}, Train Acc: {train_acc:.4f}')
print("Training complete!")
During training, you will see progress updates showing the loss and accuracy for each epoch.
Visualizing Results
Visual representation of training and validation performance can be helpful. You can plot the loss and accuracy using Matplotlib:
import matplotlib.pyplot as plt
# Plot the training loss
plt.plot(train_loss_history, label='Training Loss')
plt.xlabel("Epochs")
plt.ylabel("Loss")
plt.title("Training Loss Over Epochs")
plt.legend()
plt.show()
# Plot the training accuracy
plt.plot(train_acc_history, label='Training Accuracy')
plt.xlabel("Epochs")
plt.ylabel("Accuracy")
plt.title("Training Accuracy Over Epochs")
plt.legend()
plt.show()
Summary
In this tutorial, you learned how to build an image classifier using a single-layer neural network with PyTorch. Specifically, you covered:
- How to load and preprocess the CIFAR-10 dataset.
- The construction of a simple neural network model.
- The implementation of the training process with cross-entropy loss.
This foundational knowledge will serve you well as you explore more advanced deep learning tasks and model architectures using PyTorch. With continued practice, you’ll become proficient in applying these techniques effectively in various machine learning applications.