0% found this document useful (0 votes)
15 views7 pages

Week 7 - Mnist-Mlp

The document outlines a PyTorch implementation of a Multilayer Perceptron (MLP) model for classifying the MNIST dataset of handwritten digits. It includes steps for data preprocessing, model training, evaluation, and visualization of results, achieving an accuracy of 96.39% on test images. Additionally, it provides functionality to visualize correctly and incorrectly classified images, as well as a confusion matrix for performance analysis.

Uploaded by

Deepak Joshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views7 pages

Week 7 - Mnist-Mlp

The document outlines a PyTorch implementation of a Multilayer Perceptron (MLP) model for classifying the MNIST dataset of handwritten digits. It includes steps for data preprocessing, model training, evaluation, and visualization of results, achieving an accuracy of 96.39% on test images. Additionally, it provides functionality to visualize correctly and incorrectly classified images, as well as a confusion matrix for performance analysis.

Uploaded by

Deepak Joshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

mnist-mlp

August 23, 2024

[1]: import torch


import torch.nn as nn
import torch.optim as optim
import torchvision
import torchvision.transforms as transforms
from torch.utils.data import DataLoader

/Users/sourangshu/miniconda3/envs/dl/lib/python3.11/site-
packages/torchvision/io/image.py:13: UserWarning: Failed to load image Python
extension: 'dlopen(/Users/sourangshu/miniconda3/envs/dl/lib/python3.11/site-
packages/torchvision/image.so, 0x0006): Symbol not found:
__ZN3c1017RegisterOperatorsD1Ev
Referenced from: <CFED5F8E-EC3F-36FD-AAA3-2C6C7F8D3DD9>
/Users/sourangshu/miniconda3/envs/dl/lib/python3.11/site-
packages/torchvision/image.so
Expected in: <CDAC6E34-8608-3E70-8B2F-32BCD38E90FB>
/Users/sourangshu/miniconda3/envs/dl/lib/python3.11/site-
packages/torch/lib/libtorch_cpu.dylib'If you don't plan on using image
functionality from `torchvision.io`, you can ignore this warning. Otherwise,
there might be something wrong with your environment. Did you have `libjpeg` or
`libpng` installed before building `torchvision` from source?
warn(

[2]: # Hyperparameters
input_size = 784 # 28x28 images flattened
hidden_size = 128
num_classes = 10
num_epochs = 5
batch_size = 100
learning_rate = 0.001

# MNIST dataset
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,)) # Normalizing the dataset
])

1
[3]: train_dataset = torchvision.datasets.MNIST(root='./data', train=True,␣
↪transform=transform, download=True)

test_dataset = torchvision.datasets.MNIST(root='./data', train=False,␣


↪transform=transform, download=True)

train_loader = DataLoader(dataset=train_dataset, batch_size=batch_size,␣


↪shuffle=True)

test_loader = DataLoader(dataset=test_dataset, batch_size=batch_size,␣


↪shuffle=False)

[4]: # Multilayer Perceptron Model


class MLP(nn.Module):
def __init__(self, input_size, hidden_size, num_classes):
super(MLP, self).__init__()
self.fc1 = nn.Linear(input_size, hidden_size)
self.relu = nn.ReLU()
self.fc2 = nn.Linear(hidden_size, num_classes)

def forward(self, x):


x = x.view(-1, input_size) # Flatten the image to a 1D vector
x = self.fc1(x)
x = self.relu(x)
x = self.fc2(x)
return x

model = MLP(input_size, hidden_size, num_classes)


print(model)

MLP(
(fc1): Linear(in_features=784, out_features=128, bias=True)
(relu): ReLU()
(fc2): Linear(in_features=128, out_features=10, bias=True)
)

[5]: # Loss and optimizer


criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=learning_rate)

# Training the model


for epoch in range(num_epochs):
for i, (images, labels) in enumerate(train_loader):
# Forward pass
outputs = model(images)
loss = criterion(outputs, labels)

# Backward pass and optimization


optimizer.zero_grad()

2
loss.backward()
optimizer.step()

if (i+1) % 100 == 0:
print(f'Epoch [{epoch+1}/{num_epochs}], Step [{i+1}/
↪{len(train_loader)}], Loss: {loss.item():.4f}')

Epoch [1/5], Step [100/600], Loss: 0.4044


Epoch [1/5], Step [200/600], Loss: 0.4077
Epoch [1/5], Step [300/600], Loss: 0.4788
Epoch [1/5], Step [400/600], Loss: 0.3434
Epoch [1/5], Step [500/600], Loss: 0.2571
Epoch [1/5], Step [600/600], Loss: 0.4612
Epoch [2/5], Step [100/600], Loss: 0.2610
Epoch [2/5], Step [200/600], Loss: 0.2137
Epoch [2/5], Step [300/600], Loss: 0.1935
Epoch [2/5], Step [400/600], Loss: 0.3608
Epoch [2/5], Step [500/600], Loss: 0.2034
Epoch [2/5], Step [600/600], Loss: 0.2378
Epoch [3/5], Step [100/600], Loss: 0.1099
Epoch [3/5], Step [200/600], Loss: 0.1228
Epoch [3/5], Step [300/600], Loss: 0.1886
Epoch [3/5], Step [400/600], Loss: 0.1115
Epoch [3/5], Step [500/600], Loss: 0.2169
Epoch [3/5], Step [600/600], Loss: 0.1055
Epoch [4/5], Step [100/600], Loss: 0.1818
Epoch [4/5], Step [200/600], Loss: 0.1778
Epoch [4/5], Step [300/600], Loss: 0.0959
Epoch [4/5], Step [400/600], Loss: 0.0850
Epoch [4/5], Step [500/600], Loss: 0.1227
Epoch [4/5], Step [600/600], Loss: 0.0723
Epoch [5/5], Step [100/600], Loss: 0.1405
Epoch [5/5], Step [200/600], Loss: 0.1163
Epoch [5/5], Step [300/600], Loss: 0.0760
Epoch [5/5], Step [400/600], Loss: 0.1561
Epoch [5/5], Step [500/600], Loss: 0.0652
Epoch [5/5], Step [600/600], Loss: 0.0456

[16]: # Testing the model


model.eval() # Set model to evaluation mode
with torch.no_grad():
correct = 0
total = 0
for images, labels in test_loader:
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)

3
correct += (predicted == labels).sum().item()

print(f'Accuracy of the model on the 10,000 test images: {100 * correct /␣


↪total:.2f}%')

# Saving the model


torch.save(model.state_dict(), 'mlp_mnist.pth')

Accuracy of the model on the 10,000 test images: 96.39%

[17]: import matplotlib.pyplot as plt


import torchvision

# Function to display a row of images with their predicted and true labels
def show_images(images, labels, preds, title):
fig, axes = plt.subplots(1, 10, figsize=(20, 2)) # Create a row of 10␣
↪subplots

for i in range(10):
axes[i].imshow(images[i].squeeze(), cmap='gray')
axes[i].set_title(f'True: {labels[i]}\nPred: {preds[i]}')
axes[i].axis('off') # Hide axis
fig.suptitle(title, fontsize=16)
plt.show()

# Set the model to evaluation mode


model.eval()

# Collecting correctly and incorrectly classified images


correct_examples = []
incorrect_examples = []

with torch.no_grad():
for images, labels in test_loader:
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)

for img, label, pred in zip(images, labels, predicted):


if label == pred and len(correct_examples) < 10:
correct_examples.append((img, label.item(), pred.item()))
elif label != pred and len(incorrect_examples) < 10:
incorrect_examples.append((img, label.item(), pred.item()))

# Break if we have collected enough examples


if len(correct_examples) >= 10 and len(incorrect_examples) >= 10:
break
if len(correct_examples) >= 10 and len(incorrect_examples) >= 10:
break

4
# Prepare data for visualization
correct_images = [img for img, _, _ in correct_examples]
correct_labels = [label for _, label, _ in correct_examples]
correct_preds = [pred for _, _, pred in correct_examples]

incorrect_images = [img for img, _, _ in incorrect_examples]


incorrect_labels = [label for _, label, _ in incorrect_examples]
incorrect_preds = [pred for _, _, pred in incorrect_examples]

# Visualizing Correctly Classified Images


print("Correctly Classified Examples:")
show_images(correct_images, correct_labels, correct_preds, title='')

# Visualizing Incorrectly Classified Images


print("Incorrectly Classified Examples:")
show_images(incorrect_images, incorrect_labels, incorrect_preds, title='')

Correctly Classified Examples:

Incorrectly Classified Examples:

[18]: import seaborn as sns


from sklearn.metrics import confusion_matrix
import numpy as np

# Initialize the confusion matrix


confusion_mat = np.zeros((num_classes, num_classes))

# Set the model to evaluation mode


model.eval()

# Collect true labels and predictions


all_labels = []

5
all_preds = []

with torch.no_grad():
for images, labels in test_loader:
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
all_labels.extend(labels.cpu().numpy())
all_preds.extend(predicted.cpu().numpy())

# Calculate the confusion matrix


confusion_mat = confusion_matrix(all_labels, all_preds)

# Plotting the confusion matrix


plt.figure(figsize=(10, 8))
sns.heatmap(confusion_mat, annot=True, fmt='d', cmap='Blues',␣
↪xticklabels=range(10), yticklabels=range(10))

plt.xlabel('Predicted Labels')
plt.ylabel('True Labels')
plt.title('Confusion Matrix for MNIST Classification')
plt.show()

6
[ ]:

You might also like