Skip to content

Latest commit

 

History

5 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

LeNet-5 on MNIST — CPU & GPU Implementation

Overview

This is an implementation of LeNet-5 convolutional neural network (CNN) architecture in PyTorch for handwritten digit classification on MNIST dataset. Two implementations are provided:

  • lenet5_cpu.py — CPU training and inference
  • lenet5_gpu.py — CUDA/GPU training and inference

Workflow

  1. Download MNIST using torchvision
  2. Convert images to tensors and normalize pixel values from [0, 1] to [-1, 1]
  3. Randomly split the MNIST training set into 80% training / 20% validation using seed 1009
  4. Load training and validation samples in batches of 30
  5. Build the LeNet-5 CNN
  6. Train using Cross-Entropy Loss and SGD with momentum
  7. Track training and validation accuracy across epochs
  8. Evaluate the trained classifier on the MNIST test set

Network Architecture

LeNet-5 Architecture

LeNet-5 CNN architecture used for MNIST digit classification.

Input: 1 × 28 × 28
    ↓
Conv2D: 1 → 6, 5×5 kernel, padding=2
    ↓
ReLU
    ↓
MaxPool: 2×2
    ↓
Conv2D: 6 → 16, 5×5 kernel
    ↓
ReLU
    ↓
MaxPool: 2×2
    ↓
Flatten: 16 × 5 × 5 = 400
    ↓
Fully Connected: 400 → 120
    ↓
ReLU
    ↓
Fully Connected: 120 → 84
    ↓
ReLU
    ↓
Fully Connected: 84 → 10

The final 10 outputs correspond to MNIST digits 0–9.

Training Configuration

Parameter Value
Optimizer SGD
Learning rate 0.001
Momentum 0.9
Loss Cross-Entropy
Batch size 30
CPU epochs 10
GPU epochs 20

CPU vs GPU

Both implementations use the same preprocessing, network architecture, loss function, optimizer, validation procedure, and test evaluation.

The GPU implementation additionally:

  • Moves the LeNet-5 model to CUDA with net.cuda()
  • Moves training, validation, and test inputs to the GPU
  • Transfers prediction tensors back to CPU before numpy based accuracy calculations
  • Trains for 20 epochs instead of 10

Techniques Used

  • LeNet-5 Convolutional Neural Networks (CNN)
  • ReLU activation
  • Max pooling
  • Backpropagation
  • Stochastic Gradient Descent (SGD)
  • Momentum method for optimization
  • Cross-Entropy Loss Function
  • Train/validation splitting

Python Libraries Used

  • PyTorch
  • torchvision
  • scikit-learn
  • Matplotlib

About

Implementation of LeNet-5 architecure on CPU and GPU using PyTorch

Resources

Stars

18 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages