Minimal neural network layers and a tiny training loop implemented from first principles using NumPy/SciPy. Includes two runnable examples on MNIST:
- mnist.py — a simple fully connected network (MLP) for digit classification.
- mnist_conv.py — a tiny convolutional network demo on a 0-vs-1 subset.
- Core layer abstractions: Dense, Convolutional, Reshape, Activations (Tanh, Sigmoid, Softmax)
- Losses: Mean Squared Error (MSE), Binary Cross-Entropy (BCE)
- Micro training loop with forward/backward pass and SGD
- Small, readable code intended for learning and experimentation
- activation.py — base Activation layer
- activations.py — Tanh, Sigmoid, Softmax implementations
- dense.py — fully connected (affine) layer
- convolutional.py — 2D convolution layer using scipy.signal
- reshape.py — reshape layer for bridging conv ↔ dense
- losses.py — MSE and BCE losses and their derivatives
- network.py — predict() and train() utilities
- mnist.py — MLP example on MNIST
- mnist_conv.py — small CNN example (0 vs 1)
- Python 3.8+
- NumPy
- SciPy
- Keras (for loading the MNIST dataset via
keras.datasets)- Any supported backend is fine; if you prefer TensorFlow, install it as well.
Using a virtual environment is recommended.
Windows (PowerShell):
py -3 -m venv .venv
. .\.venv\Scripts\Activate.ps1
python -m pip install --upgrade pip
pip install numpy scipy keras # optionally: pip install tensorflow
macOS/Linux (bash):
python3 -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pip
pip install numpy scipy keras # optionally: pip install tensorflow
If you prefer pinning versions, create a requirements.txt and install with pip install -r requirements.txt.
- Fully connected network:
python mnist.py
- Convolutional example (0 vs 1 subset):
python mnist_conv.py
Both scripts will:
- Download MNIST via
keras.datasets(first run only). - Preprocess the data.
- Build a small network as a Python list of layers.
- Train with SGD and print an error metric per epoch.
- Print a few predictions at the end.
Note: These examples are intentionally small for educational purposes and may trade off accuracy/efficiency for clarity.
- Layers expose
forward(x)andbackward(grad, lr); gradients are computed analytically per layer. network.train(...)performs forward → loss → backward across the list of layers and applies SGD updates in-place.- Activation and loss derivatives are implemented explicitly to keep the math visible.
- Add a new activation: subclass
Activationand provide the function and its derivative. - Add a new layer: subclass
Layer(seelayer.py) and implementforward/backward. - Swap losses or activations in the example scripts to observe training behavior changes.
- SciPy is required for the convolution layer (
scipy.signal). Ensurepip show scipylists an installed version. - If
from keras.datasets import mnistfails, install a backend (e.g.,pip install tensorflow) or ensure thekeraspackage is installed. - Training is CPU-only and intentionally simple; expect it to be slow for large epoch counts.
See LICENSE for details.