insideout Get Pro
Learn
AI engineering · Module 04

Transformers

Build the architecture behind every modern language model.

Start here · Lesson 01

Context changes meaning

One fixed vector per word can't follow what a word means in context.

Start lesson

LessonsAll lessons

  1. 01 Context changes meaningOne fixed vector per word can't follow what a word means in context. Free
  2. 02 Attention as a soft lookupQueries, keys and values let each token gather what it needs from the others. Free
  3. 03 Why the scores are scaledDividing by the square root of the key size keeps softmax from saturating. Free
  4. 04 Masking future tokensA language model may only look backwards, and the mask enforces it. Pro
  5. 05 Heads in parallelSeveral smaller attention heads, each tracking a different relationship. Pro
  6. 06 What trained heads learnPrevious-token, induction and other patterns found in real models. Pro
  7. 07 Encoding word order and RoPEAttention ignores order unless positions are added, or rotated in. Pro
  8. 08 Norm, attention, MLP, residualThe four parts of one transformer block, and why each is there. Pro
  9. 09 Logits and softmax over the vocabularyThe last layer scores every token, and softmax turns scores into probabilities. Pro
  10. 10 Autoregressive generationPick a token, append it, run the model again. Pro
  11. 11 Turning logits into choicesGreedy picks, and how temperature sharpens or flattens the odds. Pro
  12. 12 Cutting the tail with top-k and top-pSample only from the likeliest tokens to avoid rare nonsense. Pro
  13. 13 Seeds, reproducibility and picking settingsGetting the same output twice, and which settings suit which task. Pro
  14. 14 What a model's probabilities are worthWhen a confident model is right, and how to check its calibration. Pro
  15. 15 Mixture of expertsA router sends each token to a few experts, so most weights sit idle. Pro
  16. 16 Linear attention and hybrid modelsMost layers keep a fixed-size state, and a few keep full attention. Pro
  17. 17 Training a small GPT from zeroTrain and sample a tiny GPT in the browser, with every line in view. Pro

Build it yourself · on your own machine

A small GPT on Shakespeare

A character-level GPT you write line by line in PyTorch, trained on your laptop and sampled with your own generation loop.
  1. Download Tiny Shakespeare (about 1.1 million characters, in Karpathy's char-rnn repository), give each character an ID, split it 90/10, and train a bigram baseline.
  2. Write one causal attention head yourself, with queries, keys, values, the square-root scaling and the mask, and check it against PyTorch's scaled_dot_product_attention.
  3. Build the block (layer norm, multi-head attention, MLP and residual connections), stack four with learned position embeddings, and train with AdamW.
  4. Train again with the position embeddings taken out, and compare the two validation losses.
  5. Write the generation loop with temperature, top-k and top-p, and sample 500 characters at three settings from a fixed seed.

How you'll know it worksValidation loss under 2.0 (the bigram sits near 2.5), your attention head within 1e-5 of PyTorch's, and the same seed giving the same 500 characters twice.

Python and PyTorch, on a laptop CPU or Apple's mps device. A model of about 800,000 parameters trains in under half an hour. Uses lessons 02 to 17.