daniel rotkopf

← all projects

A Neural Network Framework, From Scratch

Backpropagation in NumPy — then MRI tumor classification on top of it

Final project, 11th grade · 2024 · Python · updated 2y ago

Before using a deep learning library, I wrote one. DL3.py implements the full training loop in NumPy with no TensorFlow anywhere.

The framework is the real project. DL3.py defines layers and models from first principles: forward propagation, backpropagation with analytic gradients, and a training loop — all in NumPy.

It supports He and Xavier initialization, five activations (ReLU, leaky ReLU, tanh, sigmoid, softmax) each paired with its own derivative, numerically trimmed variants to avoid overflow in sigmoid and softmax, categorical cross-entropy loss, and weight persistence to HDF5 so a trained model can be reloaded and used later.

The application built on it classifies brain MRI scans into four categories — glioma, meningioma, pituitary tumor, and no tumor. Images are downsampled to 32x32 RGB, flattened to 3072 features, and normalized to a range centered on zero.

The network is a four-layer fully connected classifier — 3072 to 1024 to 512 to 256 to 4 — with per-layer learning rates, reaching 80.53% test accuracy on a stratified split.

# highlights

  • Forward and backward passes implemented by hand
  • He / Xavier init, 5 activations with derivatives
  • Overflow-safe trimmed sigmoid and softmax
  • 80.53% test accuracy across 4 classes

# built with

  • Python
  • NumPy
  • h5py
  • scikit-learn
  • Pillow
  • Matplotlib