A neural network from scratch in Cython

A small neural network library written without PyTorch or TensorFlow, in Cython with some C++, used from Python with numpy arrays. I built it on my own for the Machine Learning exam at the University of Pisa.

I wanted to understand backpropagation from the inside, with no library hiding it, and at the same time go below Python: C types, manually allocated memory, memoryview. It is not meant for production: it is an exam exercise, the engine of the course project.

What it contains

  1. 01

    Multilayer perceptron

    A class with an arbitrary layer structure, a per-layer activation function, learning rate, momentum and regularization. Training by backpropagation, optional validation set, train and predict.

  2. 02

    An incremental variant

    A Cascade Correlation: it starts with only inputs and outputs and adds hidden neurons one at a time, picking the candidate most correlated with the residual error and freezing its weights. Left unfinished in 2022, I completed it in 2026 with the help of Claude Code.

  3. 03

    Tests

    A demo script with the XOR problem, with the error curve and the decision map, the MONK-1 dataset and the course dataset (the data belong to the lecturer, not to me).

Animation: the network grows one hidden neuron at a time, its prediction sharpens until it separates the two spirals and the error falls
Cascade Correlation on the two spirals (200 points): 8 hidden neurons added one at a time, final error 0.003, 100% of points classified. Cython code completed in 2026.
  • What I did

    All the code, on my own: about 940 lines of Cython and demo scripts. Optimizer: gradient descent with momentum, nothing else. Fully connected layers only.

  • Limits

    The README is two lines, there are no automated tests (only a demo script) and I did not compare speed with other libraries, so I claim no advantage. Idle since February 2022.

Stack: Python, Cython (C++), numpy, matplotlib, Makefile and setup.py.