This project demonstrates building, training, and evaluating a neural network to classify handwritten digits from the MNIST dataset. The goal is to create a model that can accurately recognize digits (0–9) in images, and explore the impact of different architectures and hyperparameters on performance.
The MNIST dataset is a benchmark dataset in computer vision, containing 60,000 training images and 10,000 test images of handwritten digits, each of size 28x28 pixels.
- Load and preprocess the MNIST dataset.
- Build a neural network (MLP and optionally CNN) using PyTorch.
- Train the network and monitor training and validation loss.
- Evaluate model performance on the test set.
- Experiment with different hyperparameters to improve accuracy.
- Save the trained model for future use.
- Dataset: MNIST
- Number of classes: 10 (digits 0–9)
- Training samples: 60,000
- Test samples: 10,000
- Image size: 28x28 pixels, grayscale
- The images are converted to PyTorch tensors using
transforms.ToTensor(). - No normalization is applied for visualization purposes, but normalization can be added for better training performance.
- Data is loaded using
torch.utils.data.DataLoaderwith batching and shuffling.
- MLP (Multi-Layer Perceptron):
- Flatten input 28x28 images into a vector of 784 elements.
- Two hidden layers with ReLU activations and optional Dropout for regularization.
- Output layer with 10 neurons (one for each digit) using logits.
- Optional CNN (for higher accuracy):
- Convolutional layers to extract spatial features.
- Max-pooling layers for downsampling.
- Fully connected layers for classification.
- Loss Function:
CrossEntropyLoss(suitable for multi-class classification) - Optimizer:
Adam(adaptive learning rate for efficient training)
- Model trained for multiple epochs (default: 10–15)
- Training loss recorded per epoch.
- Validation loss and accuracy computed using the test set.
- GPU acceleration used if available via
torch.deviceand.to(device).
- Batch size, learning rate, number of hidden units, and dropout rate were tuned to improve accuracy.
- Multiple runs tested to achieve optimal performance.
- Trained model is saved using
torch.save:- Entire model:
"mnist_model_full.pth" - State dictionary:
"mnist_model_state.pth"
- Entire model:
- Training and validation loss recorded for each epoch.
- Test accuracy calculated on unseen data.
- Achieved accuracy depends on chosen architecture and hyperparameters:
- Simple MLP: ~95–97% accuracy
- CNN (optional): >98% accuracy