A flexible, lightweight Deep Learning framework built entirely from scratch using Python and NumPy. No TensorFlow, no PyTorch. Just raw mathematics.
This project focuses on the internal mechanics of Artificial Intelligence, answering fundamental questions such as:
- What actually is a neuron?
- How should weights and biases be organized in matrix form?
- How does the network learn via backpropagation?
- How does the gradient descent algorithm minimize error?
This project serves two main purposes:
- Educational Deep Dive: If you are trying to learn the math behind Neural Networks, this is the perfect starting point. The code relies solely on linear algebra and calculus, exposing the "black box" logic often hidden by high-level libraries.
- Lightweight Implementation: If you need a simple neural network for a project and don't want the overhead of installing heavy-duty libraries like TensorFlow or PyTorch, this
NeuralNetworkclass provides a dependency-free solution.
To overcome the limitations of hard-coded tutorials, I developed a modular NeuralNetwork class (found in NeuralNet.py and app.ipynb) that allows for rapid experimentation.
You can easily customize:
- Network Topology: The number of layers and neurons per layer.
- Activations: Sigmoid, ReLU, Tanh.
- Hyperparameters:
- Learning Rate (with optional exponential decay)
- Number of Epochs
- Batch Size
To validate the engine, the network was tested on the MNIST handwritten digits dataset (the "Hello World" of AI).
- Architecture:
[784 Input] -> [16 Hidden] -> [16 Hidden] -> [10 Output] - Result: The network achieves ~97% accuracy on test data.
- Demo: See
app_mnist.ipynbfor the full training loop on image data.
git clone https://github.com/Alireza2317/numpy_neural_network
cd numpy-neural-network
# Create virtual env (Recommended)
python -m venv venv
source venv/bin/activate
# Install dependencies
pip install -r requirements.txtThe core logic resides in NeuralNet.py. You can use it in your own scripts like this:
from NeuralNet import NeuralNetwork, Activation
# 1. Define Architecture: Input(2) -> Hidden(4) -> Output(1)
nn = NeuralNetwork(structure=[2, 4, 1], activation=Activation.Sigmoid)
# or, if different activations per layer is required:
# nn = NeuralNetwork(structure=[2, 4, 1], activation=[Activation.Sigmoid, Activation.Relu])
# 2. Train
# X: Input data, y: Target labels
nn.train(X, y, epochs=1000, learning_rate=0.1)
# 3. Predict
predictions = nn.predict(X_test).
├── NeuralNet.py # 🧠 The Core Class: Backprop & Forward pass logic
├── app_mnist.ipynb # Implementation of the 784-16-16-10 network on MNIST
├── app.ipynb # Flexible network playground
├── main.py # Script for testing structure
└── data/ # Dataset storage
Feel free to open a PR if you want to add new activation functions or optimizers!