Skip to content
All projects

Personal project · public or synthetic data

ML from scratch

Fourteen models written from the maths, from linear regression to a Transformer, a BPE tokenizer and an image captioner, each with a one-page explainer and checked against references.

Key results

models, each with a one-page explainer
14
tests, run in CI
62
BLEU-4, image captioning on Flickr8k (test)
23.1
more tokens for French with an English-trained BPE
69 %

Fourteen models, from the maths

Linear and logistic regression, k-NN, naive Bayes, CART, k-means and PCA in NumPy; autoencoder, VAE, LeNet and ResNet-18 in PyTorch; a small EN→FR Transformer with greedy and beam-search decoding. Every update rule is written out.

A byte-level BPE tokenizer, the kind used by GPT-2: merges learned from the 256 bytes, with incremental pair counts, so a novel trains in seconds of pure Python.

Image captioning: a frozen ResNet-50 turns a photo into 7×7 region vectors, and a Transformer decoder writes the caption through cross-attention, using the BPE tokenizer above. BLEU is written from the paper.

Try it: caption a photo

Pick a photo or use your own. The model trained on Flickr8k writes a caption, then shows where it looked for each word. Click a word.

Sample photos

Runs in your browser with ONNX Runtime: the project's model exported to ONNX, weights stored in 16 bits, computed in 32. On 200 test photos, 196 captions are identical to PyTorch's and BLEU-4 is the same (21.4). Your photo never leaves your device.

Sample photos: Unsplash via Wikimedia Commons, CC0

Try it: the BPE tokenizer

Type any text. Two tokenizers of 4,096 tokens cut it into pieces: one trained on an English novel, one on a French one. Each colour is one token.

Try:

Loading the tokenizers…

Runs in your browser: a TypeScript port of the project's Python tokenizer, checked against it token for token on 254 texts. Nothing is sent anywhere.

Checked first, then measured

Writing an algorithm from the maths only counts if it gives the right answer: 62 tests compare each classical model with scikit-learn under explicit tolerances, check BLEU against NLTK, and make each deep model learn a tiny dataset perfectly.

Captioning, 1,000 test photos of Flickr8k: BLEU-4 23.1 with beam search, against 9.6 for copying the caption of the most similar training photo. A human caption scores 21.0 against the four others; the model scores 19.2 against the same four.

BPE trained on one English novel: no unknown word on an unseen book, where a word-level vocabulary of the same size misses 13.5 % of the words; and 69 % more tokens for the same French text, the price of a language absent from training.

Eight test photos with the caption written by the model and one human caption, e.g. "two dogs are running through the snow".
Image captioning: the first eight test photos, without selection.Figure produced by the project code
BLEU scores: constant caption 2.4, nearest training photo 9.6, model greedy 19.9, beam 23.1, human 21.0.
BLEU on the 1,000 test photos, against baselines and human captions.Figure produced by the project code
Bytes per token against vocabulary size: English training text up to 4.2, unseen English 3.5, French 2.1.
BPE: compression against vocabulary size.Figure produced by the project code

Teaching code

Teaching code, not a library: small models, small datasets, pure-Python tokenizer. The captioner was trained on 6,000 photos, so it miscounts and sometimes writes fluent nonsense; the project page shows these failures next to the successes.