Create README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
tags:
|
| 4 |
+
- image-classification
|
| 5 |
+
- mnist
|
| 6 |
+
- numpy
|
| 7 |
+
- from-scratch
|
| 8 |
+
- educational
|
| 9 |
+
datasets:
|
| 10 |
+
- mnist
|
| 11 |
+
metrics:
|
| 12 |
+
- accuracy
|
| 13 |
+
---
|
| 14 |
+
|
| 15 |
+
# Four-Layer Neural Network from Scratch (NumPy)
|
| 16 |
+
|
| 17 |
+
A fully-connected network built with **only NumPy**: hand-derived backpropagation and mini-batch SGD, no deep-learning framework. It reaches **97.83% test accuracy** on MNIST and trains in about 88 seconds on a Colab CPU.
|
| 18 |
+
|
| 19 |
+
## Model Details
|
| 20 |
+
|
| 21 |
+
- **Author:** Vishwajeet Adkine
|
| 22 |
+
- **Architecture:** `784 → 128 → 64 → 32 → 10` (four weight layers, 111,146 parameters)
|
| 23 |
+
- **Activations / loss:** ReLU hidden layers, softmax output, cross-entropy with optional L2
|
| 24 |
+
- **Initialisation:** He (normal, variance 2 / fan-in)
|
| 25 |
+
- **Implementation:** NumPy `NeuralNetwork` class with `forward`, `backward`, `step` and `fit`; backprop verified against numerical gradients before training
|
| 26 |
+
|
| 27 |
+
## Intended Use
|
| 28 |
+
|
| 29 |
+
Learning and demonstration: seeing exactly how forward propagation, backpropagation and SGD work, without framework abstractions. **Not intended** for production use.
|
| 30 |
+
|
| 31 |
+
## Training
|
| 32 |
+
|
| 33 |
+
| | |
|
| 34 |
+
|---|---|
|
| 35 |
+
| Data | MNIST (90% train / 10% validation split of the training set, official test set held out) |
|
| 36 |
+
| Preprocessing | pixels scaled to [0, 1], flattened to 784 |
|
| 37 |
+
| Optimiser | mini-batch SGD, batch size 128 |
|
| 38 |
+
| Learning rate | 0.1 with ×0.95 decay per epoch |
|
| 39 |
+
| Regularisation | L2, λ = 1e-4 |
|
| 40 |
+
| Epochs | 30 (default notebook config) |
|
| 41 |
+
| Hardware | Colab CPU |
|
| 42 |
+
|
| 43 |
+
## Results
|
| 44 |
+
|
| 45 |
+
| Metric | Value |
|
| 46 |
+
|--------|-------|
|
| 47 |
+
| Test accuracy | **97.83%** |
|
| 48 |
+
| Test loss | 0.0824 |
|
| 49 |
+
| Training time | 88.2 s (Colab CPU) |
|
| 50 |
+
|
| 51 |
+
## Limitations
|
| 52 |
+
|
| 53 |
+
- Fully-connected layers ignore image structure, so accuracy is below convolutional models (the CIFAR-10 CNN project in this series covers that step).
|
| 54 |
+
- Plain SGD with a fixed decay: no momentum, Adam, dropout or batch normalisation.
|
| 55 |
+
- Evaluated on MNIST only, which is an easy, clean benchmark. Do not expect this accuracy on other digit images (different handwriting, backgrounds or scanning).
|
| 56 |
+
- No robustness or calibration analysis.
|
| 57 |
+
|
| 58 |
+
## How to Use
|
| 59 |
+
|
| 60 |
+
```python
|
| 61 |
+
net = load_weights("nn_weights.npz") # helper defined in the notebook
|
| 62 |
+
pred = net.predict(X_test[:5])
|
| 63 |
+
```
|
| 64 |
+
|
| 65 |
+
## Licence
|
| 66 |
+
|
| 67 |
+
Code and weights: MIT. MNIST is by LeCun, Cortes and Burges.
|