"Handwritten Digit Recognition: Training and Evaluating a Neural Network (numpy, no TF/Keras) "
README.md
# Digit Recognition Neural Network
This project implements a neural network to recognize handwritten digits from the MNIST dataset. The model is trained using a basic neural network architecture with one hidden layer and evaluated on both training and development sets.
## Project Overview
The goal of this project is to build a neural network that can accurately classify images of handwritten digits (0-9). The implementation includes data preprocessing, forward and backward propagation, and evaluation of the model's performance.
## Files and Functions
- `init_params()`: Initializes the weights and biases of the neural network.
- `ReLU(Z)`: Applies the ReLU activation function.
- `softmax(Z)`: Applies the softmax activation function to get probabilities.
- `forward_prop(W1, b1, W2, b2, X)`: Performs forward propagation to compute activations.
- `ReLU_deriv(Z)`: Computes the derivative of the ReLU function.
- `one_hot(Y)`: Converts labels to one-hot encoded format.
- `backward_prop(Z1, A1, Z2, A2, W1, W2, X, Y)`: Computes gradients using backward propagation.
- `update_params(W1, b1, W2, b2, dW1, db1, dW2, db2, alpha)`: Updates weights and biases.
- `get_predictions(A2)`: Retrieves the predicted class from the output.
- `get_accuracy(predictions, Y)`: Computes the accuracy of predictions.
- `gradient_descent(X, Y, alpha, iterations)`: Trains the model using gradient descent.
- `make_predictions(X, W1, b1, W2, b2)`: Makes predictions for a given input.
- `test_prediction(index, W1, b1, W2, b2)`: Tests the model's prediction on a specific index and displays the result.
## Setup
1. **Install Dependencies**
Make sure you have the following Python packages installed:
- `numpy`
- `pandas`
- `matplotlib`
You can install them using pip:
```bash
pip install numpy pandas matplotlib
-
Download Dataset
Ensure the MNIST dataset is available in CSV format and located at
/kaggle/input/digit-recognizer/train.csv
.
-
Load Data Load the MNIST dataset using Pandas:
import pandas as pd data = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
-
Preprocess Data Convert the data to a NumPy array, shuffle, and split into training and development sets.
-
Train the Model Initialize parameters and train the model:
W1, b1, W2, b2 = gradient_descent(X_train, Y_train, 0.10, 500)
-
Evaluate the Model Make predictions and evaluate accuracy:
dev_predictions = make_predictions(X_dev, W1, b1, W2, b2) print(get_accuracy(dev_predictions, Y_dev))
-
Test Predictions Test and visualize predictions for specific images:
test_prediction(0, W1, b1, W2, b2)
For any questions or inquiries, please contact Naved Shaikh at Navedshaikh77920@gmail.com.
Happy coding!
Feel free to adjust any sections or add specific details about your project, such as example results or additional setup instructions.