A Python implementation of a neural network built from scratch using NumPy for handwritten digit recognition on the MNIST dataset.
This project implements a two-layer neural network entirely from scratch without using deep learning frameworks like TensorFlow or PyTorch. The network is designed to classify handwritten digits (0-9) from the MNIST dataset, demonstrating fundamental concepts of neural networks including forward propagation, backpropagation, and gradient descent.
- Custom Neural Network Architecture: Two-layer neural network with configurable parameters
- ReLU Activation: Uses ReLU (Rectified Linear Unit) activation for the hidden layer
- Softmax Output: Softmax activation for multi-class classification
- Backpropagation: Custom implementation of the backpropagation algorithm
- Training Visualization: Tools for visualizing predictions and training results
- MNIST Dataset Support: Designed to work with the MNIST handwritten digits dataset
- Clone the repository:
git clone https://github.com/AmineChr54/neural_network_from_scratch.git
cd neural_network_from_scratch- Install the required dependencies:
pip install -r requirements.txt- numpy - For numerical computations and matrix operations
- pandas - For data loading and manipulation
- matplotlib - For visualization and plotting
- ipykernel - For Jupyter notebook support
This project uses the MNIST dataset. You need to:
- Download the MNIST dataset in CSV format
- Place the training data in
./data/MNIST/mnist_train.csv - The dataset should have labels in the first column and pixel values (0-255) in subsequent columns
You can download the MNIST dataset from sources like:
Open and run the main.ipynb notebook:
# The main workflow includes:
# 1. Load and preprocess MNIST data
# 2. Split into training and test sets
# 3. Initialize network parameters
# 4. Train the model
# 5. Evaluate accuracyThe project includes utilities for plotting:
from plot_utils import plot_number
# Visualize a prediction
plot_number(image_index, guessed_number)Alternatively, use the plotting.ipynb notebook for more visualization options.
neural_network_from_scratch/
├── main.ipynb # Main training notebook
├── plotting.ipynb # Visualization notebook
├── plot_utils.py # Utility functions for plotting
├── requirements.txt # Project dependencies
├── readme.md # This file
└── .gitignore # Git ignore rules
- Input Layer: 784 neurons (28×28 pixel images flattened)
- Hidden Layer: 10 neurons with ReLU activation
- Output Layer: 10 neurons with Softmax activation (one for each digit 0-9)
init_variables(): Initialize weights and biasesforward_prop(): Forward propagation through the networkbackward_prop(): Backpropagation to compute gradientsupdate_variables(): Update weights and biases using gradient descenttrain_model(): Main training loop
The training process includes:
- Random shuffling of data
- Splitting data into training and test sets
- Forward propagation to make predictions
- Backpropagation to compute gradients
- Parameter updates using gradient descent
- Accuracy evaluation at regular intervals
Contributions are welcome! Feel free to:
- Report bugs
- Suggest new features
- Submit pull requests
This project is open source and available for educational purposes.
This project demonstrates:
- Implementation of neural network fundamentals from scratch
- Matrix operations for efficient computation
- Gradient descent optimization
- Activation functions (ReLU, Softmax)
- One-hot encoding for multi-class classification
- Training and evaluation workflows
For questions or feedback, please open an issue on GitHub.
Note: This is an educational project designed to understand the inner workings of neural networks. For production use, consider using established frameworks like TensorFlow or PyTorch.