This repository contains a PyTorch implementation of a Multi-Layer Perceptron (MLP) for handwritten digit classification using the MNIST dataset. The project includes data loading, preprocessing, augmentation, training, validation, and evaluation.
This project demonstrates how to build a fully connected neural network (MLP) for image classification. The network is trained on the MNIST dataset, which contains 70,000 grayscale images of handwritten digits (28x28 pixels) split into training and test sets.
The MNIST dataset is used in IDX format. The dataset includes:
- Training images: 60,000
- Test images: 10,000
- Classes: 10 (digits 0–9)
Custom data loading functions are included to read the .idx files and convert them to PyTorch tensors.
- Normalization of input images
- Training/validation split (90%/10%)
- Fully connected MLP with dropout for regularization
- Dynamic learning rate scheduling using
OneCycleLR - Logging of training, validation, and test losses per epoch
- Input Layer: 784 neurons (28x28 flattened images)
- Hidden Layer 1: 512 neurons + Dropout(0.3)
- Hidden Layer 2: 256 neurons + Dropout(0.2)
- Output Layer: 10 neurons (softmax via
CrossEntropyLoss)
- Optimizer: Adam
- Learning Rate: 0.001 (managed by
OneCycleLRwith max_lr=0.005) - Loss Function: CrossEntropyLoss
- Epochs: 80
- Batch Size: 256
Training includes automatic validation and testing at each epoch. Augmented data is included in the training set to improve generalization.
- Final Test Accuracy: >98%
To achieve higher accuracy (>99%), a CNN is recommended.