Cooking AGI

Bilingual ML / LLM notes: 285 notes in 11 topics, each built around an example you can work, run and revisit.

Start here

  1. 1Linear to neural
  2. 2Tokenization
  3. 3Multi-head attention
  4. 4One training step
  5. 5KV cache & inference

Browse by topic

Example 01

One training step, by hand

Inputs 1 and 2, targets 2 and 4, starting at weight = 1. Drag the line or the point on the curve to see the errors change, then take one step down the gradient.

Read the lesson →

prediction = weight × inputweight = 1

InputTargetPredictionSquared error
1211
2424

loss = mean of the squared errors(1 + 4) / 2 = 2.5

gradient = mean of 2 × (prediction − target) × input(−2 − 8) / 2 = −5

new weight = old weight − learning rate × gradient

Learning rate 0.11 − 0.1 × (−5) = 1.5, loss 2.5 → 0.625. Loss went down: the weight fits the training data better.

Learning rate 11 − 1 × (−5) = 6, loss 2.5 → 40. The step is too large: it overshoots, and loss goes up.

The numbers are the worked example in Starting out; the gradient step is derived in why the weight moves this way.

The same model, another way:Optimizers: one step of SGD and of Adam →Training loop: this step in PyTorch →

Recently updated

  1. Prefix caching in hybrid models
  2. KV cache & inferencenow with a visual lab, and the same step in code