Sure! Let’s walk through how to use MLP (Multilayer Perceptron) in PyTorch, step-by-step, in simple and clear words, with a complete example.
✅ What We'll Do
We’ll build an MLP using PyTorch to classify digits from the MNIST dataset (images of handwritten digits from 0 to 9).
🧠 Step-by-Step Plan
- Install PyTorch
- Import libraries
- Load the dataset
- Define the MLP model
- Train the model
- Test the model
🧰 1. Install PyTorch
If not installed:pip install torch torchvision
🧑💻 2. Import Libraries
import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import datasets, transforms
from torch.utils.data import DataLoader
🗂️ 3. Load the MNIST Dataset
# Transform images to tensors and normalize
transform = transforms.Compose([
transforms.ToTensor(), # convert image to tensor
transforms.Normalize((0.5,), (0.5,)) # normalize pixel values
])
# Download and load training and test data
train_data = datasets.MNIST(root='./data', train=True, download=True, transform=transform)
test_data = datasets.MNIST(root='./data', train=False, download=True, transform=transform)
# Load into DataLoader for batch training
train_loader = DataLoader(train_data, batch_size=64, shuffle=True)
test_loader = DataLoader(test_data, batch_size=1000, shuffle=False)
🧱 4. Define the MLP Model
class MLP(nn.Module):
def __init__(self):
super(MLP, self).__init__()
self.model = nn.Sequential(
nn.Flatten(), # flatten 28x28 image to 784
nn.Linear(784, 128), # 1st hidden layer
nn.ReLU(), # activation
nn.Linear(128, 64), # 2nd hidden layer
nn.ReLU(),
nn.Linear(64, 10) # output layer (10 classes)
)
def forward(self, x):
return self.model(x)
🏋️ 5. Train the Model
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = MLP().to(device)
# Loss function and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
# Training loop
for epoch in range(5): # 5 epochs
model.train()
for images, labels in train_loader:
images, labels = images.to(device), labels.to(device)
outputs = model(images)
loss = criterion(outputs, labels)
optimizer.zero_grad()
loss.backward()
optimizer.step()
print(f"Epoch {epoch+1} - Loss: {loss.item():.4f}")
🧪 6. Test the Model
model.eval()
correct = 0
total = 0
with torch.no_grad():
for images, labels in test_loader:
images, labels = images.to(device), labels.to(device)
outputs = model(images)
_, predicted = torch.max(outputs, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
accuracy = 100 * correct / total
print(f"Test Accuracy: {accuracy:.2f}%")
✅ Final Summary
- MLP is a neural network with fully connected layers.
- In PyTorch:
- We use
nn.Linear() to create layers.
-
nn.ReLU() adds non-linearity.
-
CrossEntropyLoss is used for classification.
-
train_loader and test_loader help feed data in batches.
🎓 Output Example
Epoch 1 - Loss: 0.2931
Epoch 2 - Loss: 0.1854
...
Test Accuracy: 97.25%
Would you like to modify the MLP (add more layers or try another dataset)?