Tutorials

Code-first machine learning tutorials. Each starts with a working implementation, then explains why it works, with animated visualisations to build intuition before the maths.

Cover Image for RAG for clinical decision support: our BMJ GraphRAG study

RAG for clinical decision support: our BMJ GraphRAG study

Our BMJ study compared RAG and GraphRAG for clinical decision support on 70 simulated kidney disease cases, assessing correctness, patient specificity and clarity.

Cover Image for Double Machine Learning in Python: Coverage Needs Good Fits

Double Machine Learning in Python: Coverage Needs Good Fits

Build double machine learning in Python with cross-fitting and influence-function intervals. Measure a coverage failure and match DoubleML on 401(k) data.

Cover Image for LSTM from Scratch: The Forget Bias Sets the Memory

LSTM from Scratch: The Forget Bias Sets the Memory

Build an LSTM from scratch, check the NumPy cell against PyTorch and measure how forget bias changes recall across 100 sequence steps.

Cover Image for HNSW Python Search: 99% Recall on Held-Out Words

HNSW Python Search: 99% Recall on Held-Out Words

Build HNSW Python vector search, compare it with hnswlib and exact search, and measure recall against distance computations on held-out words.

Cover Image for Kelly Criterion in Python: Half Kelly, Estimated Inputs

Kelly Criterion in Python: Half Kelly, Estimated Inputs

Implement the Kelly criterion in Python. Simulate uncertain returns, compare fractional Kelly and inspect a GARCH allocation with explicit costs.

Cover Image for Word2vec from Scratch: A Small Corpus, Weak Analogies

Word2vec from Scratch: A Small Corpus, Weak Analogies

Build word2vec from scratch in NumPy, check its gradients against finite differences, and measure what one epoch on a small corpus can learn.

Cover Image for GARCH Model in Python: A 36-Day Shock Half-Life

GARCH Model in Python: A 36-Day Shock Half-Life

Fit a GARCH model in Python with NumPy and SciPy, verify it against arch, and compare volatility forecasts with EWMA on pinned S&P 500 returns.

Cover Image for Weight Initialization in PyTorch: Eight Layers, One Output

Weight Initialization in PyTorch: Eight Layers, One Output

PyTorch's default weight initialization gives a plain ReLU network the same output for every input by eight layers deep. One line of He init fixes it.

Cover Image for A Transformer Without Training: 40 Digits, Exactly

A Transformer Without Training: 40 Digits, Exactly

Set every weight by hand in NumPy. A transformer without training adds 40-digit numbers exactly, while a trained model of the same shape fails past 13.

Cover Image for Hamiltonian Monte Carlo: Two Knobs, Both Cliff Edges

Hamiltonian Monte Carlo: Two Knobs, Both Cliff Edges

Hamiltonian Monte Carlo in Python, built from the leapfrog up. It beats random-walk Metropolis 41 to 1 on a correlated target, and an 11% bigger step costs 18x.

Cover Image for Conformal Prediction: 90% Coverage, 29% Where It Counts

Conformal Prediction: 90% Coverage, 29% Where It Counts

Split conformal prediction in Python delivers exactly the coverage you ask for. Measured per class it gives 95% on the common ones and 29% on the rare.

Cover Image for Model Calibration in Python: The Metric Decides the Method

Model Calibration in Python: The Metric Decides the Method

Measure how five model families miscalibrate, then fix it three ways. On 200 calibration rows ECE and log loss pick different methods, and only one of them is right.

Cover Image for Channel Attention in PyTorch: Five Seeds, No Effect

Channel Attention in PyTorch: Five Seeds, No Effect

Squeeze-and-Excitation and ECA in eight lines of NumPy, then the ablation run properly. On a small CNN three seeds report a confident 0.24 point loss at 27 times its standard error, and five seeds rep...

Cover Image for Monte Carlo Tree Search in Python: Beating a Heuristic Without One

Monte Carlo Tree Search in Python: Beating a Heuristic Without One

Build Monte Carlo tree search in Python and measure it against alpha-beta at the same node count. Four phases, one formula, no evaluation function, and it wins.

Cover Image for Neural Network Quantization in Python: The Scale, Not the Bits

Neural Network Quantization in Python: The Scale, Not the Bits

Quantize a trained network in NumPy and measure where accuracy goes. At 2 bits the scale rule is worth 80 accuracy points; five bits of range are worth 0.4.

Cover Image for Diffusion Models from Scratch: Fifty Steps, If You Keep the Noise

Diffusion Models from Scratch: Fifty Steps, If You Keep the Noise

Build a diffusion model from scratch in NumPy with hand-derived gradients. Measured over three seeds: DDIM's deterministic sampler is not worse on average, it is unreliable.

Cover Image for Variational Autoencoders in NumPy: Three Digits, No KL Term

Variational Autoencoders in NumPy: Three Digits, No KL Term

Build a variational autoencoder from scratch in NumPy with hand-derived gradients. Measure what the KL term buys: without it the model can only generate 3 of 10 digits.

Cover Image for Kolmogorov-Arnold Networks in Python: A Curve on Every Edge

Kolmogorov-Arnold Networks in Python: A Curve on Every Edge

Build a Kolmogorov-Arnold network in Python with NumPy. Learned B-spline functions on the edges, and edges that recover a formula's terms at R-squared 0.999999.

Cover Image for Four Multi-Agent Orchestration Patterns in Python

Four Multi-Agent Orchestration Patterns in Python

Multi-agent orchestration in Python, measured: sequential, supervisor, fan-out and debate over one job, with token spend, wall-clock and accuracy for each.

Cover Image for Writing an MCP Server in Python Without the SDK

Writing an MCP Server in Python Without the SDK

Build an MCP server in Python with the standard library alone: JSON-RPC over stdio, six methods, and a session the official SDK client accepts unchanged.

Cover Image for Poincaré Embeddings in Python: Three Dimensions Beat Five Hundred

Poincaré Embeddings in Python: Three Dimensions Beat Five Hundred

Poincaré embeddings in Python from scratch: three hyperbolic dimensions beat five hundred Euclidean ones on the WordNet mammals tree, reproducing Nickel and Kiela, with the rim costs measured.

Cover Image for Prompt Injection Against Tool-Using Agents

Prompt Injection Against Tool-Using Agents

A worked prompt injection example: eight attacks on a tool-using agent in Python, six defences measured, and the one that closes the hole.

Cover Image for BM25 vs Embeddings: Why Hybrid Retrieval Wins

BM25 vs Embeddings: Why Hybrid Retrieval Wins

BM25 vs embeddings on 300 labelled queries. Build both in NumPy, measure where each one fails, and merge them with reciprocal rank fusion.

Cover Image for Mechanistic Interpretability: One Neuron, Two Features

Mechanistic Interpretability: One Neuron, Two Features

Mechanistic interpretability on a small MNIST CNN: activation maximisation, dataset examples and ablation disagree about the same neuron.

Cover Image for Structured Output from LLMs: Constrained JSON Decoding

Structured Output from LLMs: Constrained JSON Decoding

Three ways to get valid JSON from an LLM, measured on the same 16 prompts: retry until it parses, repair the string, or mask the logits with a grammar.

Cover Image for Multi-Task Learning in PyTorch: When Gradients Conflict

Multi-Task Learning in PyTorch: When Gradients Conflict

Multi-task learning in PyTorch: measure gradient conflict between two tasks, implement PCGrad in ten lines, and find out when projecting gradients pays.

Cover Image for Building a ReAct Agent in Python: Reason, Act, Observe

Building a ReAct Agent in Python: Reason, Act, Observe

Build a ReAct agent from scratch in Python. The reason-act-observe loop in 31 lines, its three failure modes, and what the scratchpad costs you per step.

Cover Image for GRPO from Scratch: Group-Relative Advantage Without a Critic

GRPO from Scratch: Group-Relative Advantage Without a Critic

Build GRPO from scratch in Python. The group-relative advantage that replaced PPO's critic in DeepSeek-R1, measured on a bandit, a maze and a verifier.

Cover Image for The Universal Approximation Theorem in NumPy

The Universal Approximation Theorem in NumPy

Why one hidden layer can fit any curve. Build a universal approximator in Python, sweep the width, watch the error collapse, and see why bumps tile anything.

Cover Image for Reward Hacking in Reinforcement Learning

Reward Hacking in Reinforcement Learning

Watch a Q-learning agent game a naive reward: it farms a bonus tile forever and never reaches the goal. Then two fixes and a detector, from scratch in Python.

Stay in the loop

Get notified when new tutorials and deep-dives are published. No spam, unsubscribe anytime.

Blog FAQ

Questions about these tutorials and write-ups