Extra-curricular · 20 lessons · verified in code

AI Math — the mathematics behind neural networks

Extra-curricular, but a must for anyone drilling into the internals: all the math you need to truly understand what models compute, verified line by line with runnable PyTorch code.

Module 1 · Linear algebra Sources on GitHub


The core idea

If you can put a breakpoint on a number, that number can’t scare you anymore.

No abstract proofs. Every concept comes as a pair: a plain-language explanation and a PyTorch script you can run line by line, even inside a debugger. The path is built for people coming from the software world (debugging, systems, code) who want to rebuild the foundations starting from high-school math. Every symbol is explained in words the first time it appears.

Every check goes through code. There are no quizzes: each lesson has # TODO exercises to complete and a pytest suite that decides when the lesson is done. And the plots are not decoration: every figure on these pages is generated by the lesson scripts, and you can regenerate (and modify) them yourself.

The loss surface as a valley: gradient descent is a ball rolling toward the bottom

How it fits the rest of the site

The C++ ↔ CUDA track answers how machines compute fast. This track answers what they are computing and why it works. The two reinforce each other: when the track hands you a tiled matmul, here you discover why neural networks are almost nothing but matmuls; when the How Machines Learn from Data chapter tells you “the model descends along the gradient”, here you write that gradient by hand and verify it to the decimal.

Suggested pacing:

  • Alongside the main track, one module at a time: 10–12 weeks at 4–5 hours a week.
  • As a focused immersion before the advanced track modules (reduction, matmul tiling), if ∇, Σ and Jacobians still slow you down when reading papers.

The map

Module Topic Lessons Time
01 · Linear algebra Vectors, matrices, matmul, norms, eigenvalues, SVD 4 ~2 weeks
02 · Calculus Derivatives, gradients, chain rule, Jacobians 4 ~2 weeks
03 · Probability Random variables, likelihood, entropy, KL 3 ~1.5 weeks
04 · Optimization Gradient descent, SGD, momentum, Adam 4 ~2 weeks
05 · Neural networks From linear regression to attention, all from scratch 5 ~3 weeks

The modules build on each other — do them in order. A small dataset of 5 houses (square meters, rooms, price) returns in every module under a different light — first as a matrix, then as a loss, then as a likelihood, finally as your first trained model.

The per-lesson workflow

Every lesson folder contains the same five files:

  1. Read EXPLANATION.md — intuition, a plain-words definition, a numeric example worked by hand.
  2. Run python lesson.py — the script redoes the same steps, printing every intermediate value. Even better: open it in a debugger and set breakpoints.
  3. Complete the # TODO functions in exercises.py.
  4. Verify with pytest from the lesson folder: when the tests pass, the lesson is done.
  5. Compare with solutions.py — only after a serious attempt.

Lessons with plots save them as PNGs in figures/: no display needed.

Setup

Python 3.10+ is required. From the ai-math/ folder:

python -m venv .venv
.venv\Scripts\activate      # on Windows
pip install -r requirements.txt

The CPU build of PyTorch is all this path needs:

pip install torch --index-url https://download.pytorch.org/whl/cpu

Reference books

The path is self-contained, but each module cites chapters for going deeper in:


Table of contents


This site uses Just the Docs, a documentation theme for Jekyll.