This project implements a handwritten digit recognition system trained on the MNIST dataset using a custom neural network built from scratch with NumPy. A GUI built with Tkinter allows users to draw digits for real-time prediction using the trained model.
I learned all of the maths for implementing this from 3blue1brown
The MNIST dataset consists of 60,000 training images and 10,000 testing images. Each image is a grayscale 28x28 pixel representation of a handwritten digit (0–9). The dataset is used in CSV format where the first column represents the digit label and the remaining 784 columns represent pixel intensities.
The neural network follows a feedforward architecture with the following layers:
- Input Layer: 784 neurons (corresponding to the 28×28 image size)
- First Hidden Layer: 1024 neurons with ReLU activation
- Second Hidden Layer: 256 neurons with ReLU activation
- Output Layer: 10 neurons with Softmax activation
The model is trained using mini-batch gradient descent with a batch size of 64.
The ReLU (Rectified Linear Unit) activation function is defined as:
The softmax function outputs a probability distribution over classes:
For an input vector ( X ), the forward propagation steps are:
The loss function used is cross-entropy loss:
The gradients are computed as follows:
The GUI allows the user to draw a digit, which is then:
- Centered using the center of mass
- Denoised by removing low intensity noise
- Normalized using training set statistics
- Passed through the neural network for classification
- Real-time digit drawing on a canvas
- Display of predicted digit
- Uses saved model parameters from an .npz file
numpytkinterscipy.ndimage
recognition.npz: Contains all model weights, biases, and normalization parametersapp.py: Tkinter-based GUI for drawing and recognizing digitsrecognition.py: Neural network training and saving
- Final test accuracy: ~98%
- Training accuracy is evaluated at regular intervals during training
- Training log
- Drawing '5' on the canvas yields prediction: 5
- Drawing '3' yields prediction: 3
- Train the model by running:
python recognition.pyThis will generate a recognition.npz file containing all model parameters.
- Launch the GUI by running:
python app.pyEnsure the model file (recognition.npz) is in the same directory.
- Improve accuracy with convolutional layers
- Save training progress with checkpoints
- Allow user to load their own images
- Experiment with different layer sizes and architectures
This project is open-source and free to use



