A pure NumPy implementation of a deep neural network for classifying handwritten digits from the MNIST dataset. This project demonstrates how to build a neural network from scratch using only NumPy, implementing forward propagation, backpropagation, and various activation functions.
- Pure NumPy implementation of a deep neural network
- Three-layer architecture (784->128->64->10)
- ReLU and Softmax activation functions
- Cross-entropy loss function
- Mini-batch gradient descent
- Data normalization and preprocessing
- Training/validation split
- Accuracy evaluation
numpy
pandas
scikit-learn
matplotlib
pillow
idx2numpy
torch # Only used for input preprocessing
Install the required packages using:
pip install -r requirements.txt
The MNIST dataset needs to be downloaded from Kaggle's Digit Recognizer competition. After downloading:
- Extract the downloaded files
- Place
train.csv
andtest.csv
in the/data/raw
directory
├── data
│ ├── external # Data from third party sources
│ ├── interim # Intermediate data
│ ├── processed # Final, canonical data sets
│ └── raw # Original, immutable data dump
├── models # Trained models
├── notebooks # Jupyter notebooks
├── references # Data dictionaries, manuals, etc.
├── reports
│ └── figures # Generated graphics and figures
├── src
│ └── NN_Numpy.py # Source code for the neural network
├── requirements.txt # Project dependencies
└── README.md
The neural network implementation includes:
- FCLayer: Fully connected layer implementation with forward and backward passes
- NeuralNetwork: Main class that combines layers and implements: - Forward propagation - Backward propagation - Various activation functions (ReLU, Sigmoid, Softmax)
- Utils: Helper functions for data preprocessing and visualization
- Input Layer: 784 neurons (28x28 pixel images)
- First Hidden Layer: 128 neurons with ReLU activation
- Second Hidden Layer: 64 neurons with ReLU activation
- Output Layer: 10 neurons with Softmax activation (one for each digit)
- Ensure the MNIST dataset is placed in
/data/raw
- Run the neural network:
python src/NN_Numpy.py
The script will:
- Load and preprocess the MNIST data
- Train the neural network for 100 epochs
- Display training loss for each epoch
- Evaluate the model on validation data
- Show example predictions with visualizations
The model achieves reasonable accuracy on the validation set while being implemented purely in NumPy. Training progress is displayed during execution:
Epoch 1/100, Loss: X.XXXX
...
Epoch 100/100, Loss: X.XXXX
Validation Accuracy: XX.XX%
Example predictions are visualized using matplotlib, showing the original image and the model's prediction.
This project is licensed under the terms of the LICENSE file included in the repository.