A neural network implementation from scratch using NumPy to classify handwritten digits (1, 2, and 3) from the MNIST dataset.
This project implements a simple neural network from scratch (using NumPy) to classify handwritten digits (1, 2, and 3) from the MNIST dataset. The implementation includes:
- Data loading and preprocessing
- One-hot encoding of labels
- Neural network with single layer
- Softmax activation and cross-entropy loss
- Training with gradient descent
- Performance evaluation
mnist-digit-classification/
├── mnist_neural_network.py # Main implementation script
├── README.md # This documentation file
├── requirements.txt # Python dependencies
├── images/ # Directory for output plots
│ ├── accuracy_vs_epochs.png
│ ├── accuracy_vs_lr.png
│ └── loss_vs_epochs.png
└── data/ # MNIST dataset (auto-downloaded)
- Python 3.6+
- NumPy
- Matplotlib
- Keras (for dataset loading)
Install requirements:
pip install -r requirements.txtKey components of the implementation:
- Data Preparation
- Load MNIST dataset using Keras
- Filter for only digits 1, 2, and 3
- Flatten and normalize images (28x28 → 784-dim vectors)
- One-hot encode labels
- Neural Network
- Single layer architecture (784 input → 3 output)
- ReLU activation function
- Softmax output layer
- Cross-entropy loss function
- Training
- Batch gradient descent
- Learning rate comparison (0.1 to 0.5)
- 50 training epochs
- Evaluation
- Test accuracy calculation
- Loss and accuracy tracking
- Prediction visualization
The model achieves the following performance:
| Metric | Value |
|---|---|
| Training Accuracy | ~98% |
| Test Accuracy | ~96% |
| Training Time | <1 minute |
Learning rate comparison results:
| Learning Rate | Final Accuracy |
|---|---|
| 0.1 | 94.2% |
| 0.2 | 95.7% |
| 0.3 | 96.1% |
| 0.4 | 95.9% |
| 0.5 | 95.3% |
- MNIST dataset from Yann LeCun and Corinna Cortes
- NumPy for numerical computations
- Keras for dataset loading utilities