Skip to content

SheikhMazin/Neural-Network

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

4 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿง  MNIST Neural Network (from Scratch)

A fully-connected neural network built entirely from scratch using NumPy โ€” no TensorFlow, PyTorch, or Keras.
This project trains a simple feedforward model to classify handwritten digits from the MNIST dataset.

๐Ÿš€ Overview

This project demonstrates the core mechanics of a neural network implemented manually:

  • Matrix-based forward and backward propagation
  • ReLU activation and Cross-Entropy Loss
  • Mini-batch training with a custom DataLoader
  • Weight initialization, gradient updates, and learning rate tuning
  • Model saving/loading and prediction visualization with Matplotlib
    It was developed as a learning exercise to understand how deep learning works under the hood.

โš™๏ธ Requirements

To install dependencies:

pip install -r requirements.txt

๐Ÿ“ฆ Dataset

This project uses the MNIST dataset from Kaggle:
๐Ÿ‘‰ Kaggle MNIST Dataset
Download the .idx files from there and place them in the data/ folder.

Place the following files inside the data/ folder:

  • train-images.idx3-ubyte
  • train-labels.idx1-ubyte
  • t10k-images.idx3-ubyte
  • t10k-labels.idx1-ubyte

๐Ÿง  Model Architecture

The default network used in main.py:

Layer Type Input โ†’ Output
1 Dense 784 โ†’ 256
2 ReLU -
3 Dense 256 โ†’ 128
4 ReLU -
5 Dense 128 โ†’ 10

Each neuron learns via stochastic gradient descent using cross-entropy loss.

๐Ÿ‹๏ธโ€โ™‚๏ธ Training

To train a new model:

python main.py

Then choose:

Train or Not (y/n): y
  • Trains the model for 12 epochs (by default)
  • Saves weights as mnist_model_1.npz
  • Prints per-epoch losses and accuracies

Example output:

Final Train Accuracy: 91.87%
Final Test Accuracy : 90.42%

๐Ÿ” Testing / Evaluation

To test a pre-trained model:

python main.py

Then choose:

Train or Not (y/n): n

This loads your saved weights, evaluates accuracy on the MNIST test set, and displays 25 random digit predictions (5ร—5 grid) with predicted and true labels.

๐Ÿ“Š Results

With basic tuning (lr=0.005, batch_size=128, epochs=12):

  • Training Accuracy: ~92%
  • Test Accuracy: ~90% Each run may vary slightly due to random initialization.

๐Ÿ™Œ Acknowledgements

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages