A simple yet powerful feedforward neural network built from scratch using only NumPy. This project trains on the MNIST dataset (handwritten digits) and can also classify custom 28×28 images.
✅ Goal: Classify handwritten digits (0–9) using a neural network built from scratch.
✅ Dataset: MNIST (60,000 training, 10,000 test images).
✅ Testing: Custom 28×28 JPEG digit images stored in the image/
directory.
🖥️ Neural Network Architecture:
- Input Layer: 784 neurons (one per pixel in a 28×28 image).
- Hidden Layer: 64 neurons (configurable) using ReLU activation.
- Output Layer: 10 neurons (one per digit, 0-9) using softmax activation.
- Loss Function: Cross-Entropy Loss.
- Optimizer: Mini-batch Gradient Descent.
NEURAL_NETWORK/
├── data/
│ ├── test_images/ # Custom test images
│ ├── mnist_test.csv # MNIST test dataset
│ └── mnist_train.csv # MNIST training dataset
├── model/
│ └── nn_mnist_model.pkl # Saved trained model
├── neural_network/
│ ├── nn_core/
│ │ ├── backpropagation.py
│ │ ├── forward_propagation.py
│ │ ├── params_update.py
│ │ └── train_loop.py
│ ├── data_preprocessing.py
│ ├── nn_class.py
│ ├── nn_testing.py
│ ├── nn_training.py
│ └── utils.py
├── notebook/
│ └── nn.ipynb # Jupyter Notebook
├── .gitignore
└── README.md
git clone https://github.com/isatyamks/neural_network.git
cd neural_network
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
pip install numpy pandas matplotlib
(Add Pillow
if working with images.)
✅ Ensure MNIST dataset (mnist_train.csv
, mnist_test.csv
) is in the data/
folder.
✅ Run the training script:
python train.py
📌 What this does:
- Loads MNIST data.
- Initializes and trains the neural network.
- Evaluates accuracy on the MNIST test set.
- Saves the trained model to
model/nn_mnist_model.pkl
. - Plots training and validation loss.
Once trained, the script will automatically evaluate the model on mnist_test.csv
and print accuracy.
Want to test on your own handwritten digits?
✅ Save 28×28 JPEG images in image/
.
✅ Run the script:
python testing.py
📌 What this does:
- Loads the trained model (
nn_mnist_model.pkl
). - Reads each image from
image/
, converts to a NumPy array (28×28). - Normalizes, flattens, and predicts the digit class.
💡 Modify testing.py
to display the image alongside its predicted label!
Layer | Details |
---|---|
Input | 28×28 = 784 neurons |
Hidden | 64 neurons, ReLU activation |
Output | 10 neurons, Softmax activation |
1️⃣ Compute Weighted Sum: [ Z = W \times X + b ] 2️⃣ Apply Activation Function:
- Hidden Layer: ReLU (max(0, Z))
- Output Layer: Softmax (probability distribution over 10 digits)
1️⃣ Compute Loss (Cross-Entropy for classification). 2️⃣ Calculate Gradients using the Chain Rule. 3️⃣ Update Weights & Biases (Mini-batch Gradient Descent): [ W = W - \alpha \times \text{gradient} ]
✅ Gradient Descent minimizes loss by adjusting weights using params_update.py
.
✅ Adjust learning rate and batch size for better performance.
🔹 Neural Network Class: nn_class.py
🔹 Training Script: nn_training.py
🔹 Testing Script: nn_testing.py
🔹 Data Preprocessing: data_preprocessing.py
🔹 Core Modules (Forward, Backward, Update): nn_core/
This project is open-source under the MIT License. Feel free to use, modify, and improve it!
🚀 Happy Learning & Coding! 💡