pytorch project_1

One of the simplest and easiest starter projects for machine learning in PyTorch is building a basic classifier for the MNIST dataset. The MNIST dataset consists of grayscale images of handwritten digits (0–9), making it a popular and beginner-friendly dataset for getting hands-on with PyTorch. Here’s a breakdown of this starter project:

Project Overview: MNIST Digit Classifier

In this project, you’ll build a neural network to classify images of digits from the MNIST dataset. This is a good starter project because:

Simple dataset: Each image is a 28×28 pixel grayscale image, which makes the model easy to train and understand.

Multi-class classification: It introduces the idea of classifying data into multiple categories (one for each digit, 0–9).

Basic neural network: You’ll use a simple feedforward neural network, which is a great starting point to understand the basics of model building and training.

Steps for Building the MNIST Classifier in PyTorch

1. Set Up PyTorch and Import Libraries

import torch

import torch.nn as nn

import torch.optim as optim

from torchvision import datasets, transforms

2. Download and Prepare the Data

PyTorch has built-in support for the MNIST dataset through torchvision. The DataLoader makes it easy to load the data in mini-batches for training and testing.

transform = transforms.Compose([transforms.ToTensor()])

train_data = datasets.MNIST(root=’./data’, train=True, download=True, transform=transform)

test_data = datasets.MNIST(root=’./data’, train=False, download=True, transform=transform)

train_loader = torch.utils.data.DataLoader(train_data, batch_size=64, shuffle=True)

test_loader = torch.utils.data.DataLoader(test_data, batch_size=64, shuffle=False)

3. Define the Model

Build a simple neural network with one hidden layer. This model will take in a flattened 28×28 pixel image (784 inputs) and output a prediction for each of the 10 possible digit classes.

class SimpleNN(nn.Module):

    def __init__(self):

        super(SimpleNN, self).__init__()

        self.fc1 = nn.Linear(28*28, 128)

        self.fc2 = nn.Linear(128, 10)

    def forward(self, x):

        x = x.view(-1, 28*28)  # Flatten the input

        x = torch.relu(self.fc1(x))

        x = self.fc2(x)

        return x

model = SimpleNN()

4. Set Up the Loss Function and Optimizer

For this classification task, use Cross Entropy Loss and an SGD optimizer.

criterion = nn.CrossEntropyLoss()

optimizer = optim.SGD(model.parameters(), lr=0.01)

5. Train the Model

This loop goes through each batch in the training data, performs a forward pass, calculates the loss, and updates the model weights.

epochs = 5

for epoch in range(epochs):

    running_loss = 0.0

    for images, labels in train_loader:

        optimizer.zero_grad()

        outputs = model(images)

        loss = criterion(outputs, labels)

        loss.backward()

        optimizer.step()

        running_loss += loss.item()

    print(f”Epoch {epoch+1}, Loss: {running_loss/len(train_loader)}”)

6. Evaluate the Model

After training, test the model to see how well it performs on new data.

correct = 0

total = 0

with torch.no_grad():

    for images, labels in test_loader:

        outputs = model(images)

        _, predicted = torch.max(outputs.data, 1)

        total += labels.size(0)

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

print(f”Accuracy on test data: {100 * correct / total}%”)

Why This is a Good Starter Project

Minimal code: It uses basic PyTorch constructs without complicated architectures or hyperparameters.

Quick results: The model trains quickly, giving you instant feedback on how the network performs and allowing room to iterate and experiment.

Builds intuition: It introduces key ML concepts like training, testing, evaluation, and tuning, setting a foundation for more complex models in PyTorch.

This project covers the fundamentals of loading data, building a model, training, and evaluating—key skills needed for more advanced ML tasks!