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 inferencelenet5_gpu.py— CUDA/GPU training and inference
- Download MNIST using
torchvision - Convert images to tensors and normalize pixel values from
[0, 1]to[-1, 1] - Randomly split the MNIST training set into 80% training / 20% validation using seed
1009 - Load training and validation samples in batches of 30
- Build the LeNet-5 CNN
- Train using Cross-Entropy Loss and SGD with momentum
- Track training and validation accuracy across epochs
- Evaluate the trained classifier on the MNIST test set
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.
| Parameter | Value |
|---|---|
| Optimizer | SGD |
| Learning rate | 0.001 |
| Momentum | 0.9 |
| Loss | Cross-Entropy |
| Batch size | 30 |
| CPU epochs | 10 |
| GPU epochs | 20 |
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
- 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
- PyTorch
- torchvision
- scikit-learn
- Matplotlib
