This project implements a neural network to classify the MNIST dataset (handwritten digits) without using any premade models or libraries like pytorch. The code is written in Python and organized in a Jupyter Notebook for easy understanding and execution. Each part of the code is thoroughly explained with comments above it to guide you through the logic and implementation.
- Download the MNIST dataset file
mnist.npzfrom this link. - Place the downloaded
mnist.npzfile in the same directory as the Jupyter Notebook (neural network.ipynb).
Ensure you have the following Python libraries installed:
numpymatplotlib- (Optional)
Jupyter NotebookorJupyterLabfor running the notebook.
Install them using:
pip install numpy matplotlib notebook- Open a terminal or command prompt.
- Navigate to the folder where the notebook and the MNIST dataset are located.
- Start the Jupyter Notebook server:
jupyter notebook- Open neural network.ipynb in your browser.
The notebook is divided into several sections, and each section is explained with comments above the code for better understanding. Here's the structure of the notebook:
- Data Loading and Preprocessing
- What it does: Loads the MNIST dataset, flattens and normalizes the images, and one-hot encodes the labels.
- What you need to do: Ensure mnist.npz is in the correct location.
- Neural Network Functions
- Functions like
initialize_parameters,forward_propagation,backward_propagation, andupdate_parametersare implemented to train the network step-by-step. - Each function has a detailed explanation of how it works and its role in the training process.
- Functions like
- Training the Model
- The
train_modelfunction orchestrates the entire training process. - You can adjust hyperparameters like:
epochs: Number of iterations over the training data.learning_rate: Step size for gradient descent.mini_batch_size: Number of examples per batch.keep_prob: Dropout probability to prevent overfitting.lambda_: Regularization strength.
- Default values are set to reasonable defaults, but you can experiment with them.
- The
- Testing the Model
- The
test_modelfunction evaluates the trained network on the test data. - Accuracy is printed as a percentage
- The
- Step-by-Step Implementation:
- The notebook breaks down the neural network training process, including gradient descent, backpropagation, and parameter updates.
- Customizable Architecture:
- You can modify the number of layers and neurons in
layer_dimsto explore different architectures.
- You can modify the number of layers and neurons in
- Hyperparameter Tuning:
- All key hyperparameters(e.g. learning rate, epochs, mini-batch size) are adjustable for experimentation.
- Detailed Explanations:
- Every part of the code includes comments and explanations to help beginners understand the concepts behind the implementation.
project_folder/
├── mnist.npz # MNIST dataset file (to be downloaded and placed here)
├── neural network.ipynb # Jupyter Notebook with neural network implementation
└── README.md # This fileIf you have suggestions or improvements, feel free to fork the repository, make changes, and submit a pull request.
This project uses the MNIST dataset, a widely recognized dataset for handwritten digit recognition. Thanks to Yann LeCun and his collaborators for making it available.
This project is open source and available for every one