Neural Network Library in NumPy
A small deep learning library written in NumPy, and the brain scan classifier I built with it.
Architecture
Layer widths: 3072, 1024, 512, 256, 4. Output classes: glioma, meningioma, pituitary, no tumour.
The square on the left stands in for an MRI scan, and the bright patch on it is the lesion. Drag it somewhere else and you have handed the network a different scan: the pulse runs left to right through the layers — 3072 → 1024 → 512 → 256 → 4 — and the bars on the right redraw with a score for each class. Where the patch sits is the only input, and the layer widths are the real ones. The mapping from patch to score is a stand-in for illustration, not the trained weights; those are in the repository.
DL3.py is the actual project. It defines layers and models from scratch: forward propagation, backpropagation with analytic gradients, and the training loop, all in NumPy with no TensorFlow anywhere.
It supports He and Xavier initialisation, five activation functions each paired with its own derivative, numerically trimmed sigmoid and softmax to stop overflow, categorical cross-entropy, and saving weights to HDF5 so a trained model can be reloaded later.
The classifier built on it sorts brain MRI scans into glioma, meningioma, pituitary tumour, or no tumour. Images are downsampled to 32x32 RGB and flattened to 3072 features.
The network is four fully connected layers, 3072 to 1024 to 512 to 256 to 4, with a different learning rate per layer. It reaches 80.53% on a held-out split.
Highlights
- Backpropagation written by hand, not autodiff
- He and Xavier init, five activations with derivatives
- Overflow-safe sigmoid and softmax
- 80.53% across four classes
Built with
- Python
- NumPy
- h5py
- scikit-learn
- Pillow
- Matplotlib