One example, start to finish. The model reads four tokens and has to fill the gap:
wheredidthecat?
Our corpus: the only text in the whole course. Module 1 cleans and tokenizes it,
module 6 trains on it, module 7 tests the trained model on it.
Where did the cat sat?
The cat sat on the mat.
the Dog sat on the mat .
WHERE did the dog sat ?
Tiny sizes, so every number fits on screen: 10 tokens in the vocabulary, 3 numbers per token,
one attention head. Every number is computed in code. Read the modules in order.
The big picture
Part A · The forward pass: from text to a guess (used by training and inference)
Clean the raw text, chop it into tokens, and give every distinct token an ID number.
Hover or tap a token to see it light up everywhere it appears. Tap empty space to reset.
1Our corpus: four messy sentences
This tiny text is the only data in the whole course. Module 6 trains on it and module 7 tests on it. Cleaning first makes “The”, “the” and “WHERE”/“where” the same.
Also called normalisation.
2Chop it into tokens
A token is one piece the model reads. Here a token is a word or a punctuation mark.
Also called tokenization. This toy one splits by words.
3Give every distinct token an ID
Collect each different token once. That list is the vocabulary: 10 tokens here.
Vocabulary size V = 10.
4Our sentence as ID numbers
From now on the model never sees letters, only these numbers.
Token IDs, length 4: [9, 3, 8, 2] → target 7.
5One sentence, five questions
Every token in the text is a “guess what comes next” question. We follow question 4.
This is next-token prediction.
6Real models: pieces of words
Splitting isn't a fixed rule like “every 4 letters”. It is learned from text: start from single letters (or bytes), merge the most frequent neighbouring pair into a new token, and repeat until the vocabulary reaches a chosen size. The four merges above are exactly that, run on our corpus.
Merges computed on our four sentences. Method: byte-pair encoding (BPE). The learned merge list is the tokenizer.
7One word, more than one token
Pieces that often appear together earn their own token, so a word can be one token or several. “playing” becomes “play” + “ing”.
These are sub-word tokens. Each piece gets its own ID, just like the words in step 4.
Training doesn't care what a token is
Whole words or word pieces, the model always does the same job: guess the ID of the next token.
The loss in module 6 is the same too: −log p(the true next token), also called
cross-entropy.
Caveat: the formula is identical, but loss numbers from different tokenizers aren't directly comparable,
because they count different units. A word split into 3 pieces is 3 predictions; as one whole-word token it is 1.
So “average loss per token” measures something different for each tokenizer. To compare, divide the total loss
over the same text by its number of characters or bytes (e.g. bits per byte).
Our text is now numbers: “where did the cat” → [9, 3, 8, 2], and the right answer “sat” is 7. The sentence isn't perfect English. That's fine: a model learns whatever text it's fed.
Every word makes a question, a name tag and a message by multiplying with three matrices.
Hover or tap a word to follow its row. Tap empty space to reset.
blue frame: learned weights (training changes them)green frame: numbers worked out for this sentence
1The idea, with readable numbers
Every word gets three things: a question (what am I looking for?), a name tag (what do I have?) and a message (what do I pass on?). These numbers are made up by hand.
question = query, name tag = key, message = value. Match = dot product q·k.
2How one row is computed
Cat's question comes from cat's row of X times a table W_Q. Each slot: multiply pairs, add them up.
qcat = xcat · WQ (1×3 · 3×3 = 1×3). That is a matrix multiplication.
3Three tables, three jobs
Do that for every word with three different tables, and you get all questions, name tags and messages.
Q = X·WQ, K = X·WK, V = X·WV. Each 4 × 3. The W tables are learned.
Each word now has a question, a name tag and a message: Q, K, V, each 4 × 3. Next, cat uses its question to look back at the others.
Three small moves turn any four numbers into four shares that add up to 100%.
This is softmax(scores / √d), with d = 3. The result is cat's attention weights.
3Mix the messages
Cat takes that share of each word's message, adds them up, and adds the result to its own row.
z = shares · V; h = x + z. Keeping x is the residual connection.
4Every word at once
The same three moves happen for every row in one go, giving a new 4 × 3 table.
H = X + softmax(mask(Q·Kᵀ / √3)) · V. One attention head.
Real models stack many of these
Our model has one attention block with one “head”. Real models run dozens of heads side by side and stack
dozens of blocks, each with a few extra steps in between. The idea inside every block is the same as here.
See the big picture on the course map
Cat's row now carries a bit of every earlier word: h = [−0.21, 0.81, 0.91]. Only this last row is needed to guess the next word.
Turn cat's final vector into a score for every token, then into chances that add up to 1.
Hover or tap a token to follow its column, score and chance. Tap empty space to reset.
blue frame: learned weights (training changes them)green frame: numbers worked out for this sentence
1A score for every token
One more table, U, has a column for each of the 10 tokens. Cat's row times each column gives that token's score.
scores = h · U (1×3 · 3×10 = 1×10). The scores are called logits.
2Scores become chances
Scores can be negative and don't add up to anything. Softmax makes them positive chances that add up to 100%.
p = softmax(scores) — the same two moves as in module 4. A probability distribution over the vocabulary.
3Choosing means picking from chances
The model never just “knows” the word. It gives chances, and then one token is picked from them.
Way 1 = greedy (argmax). Way 2 = sampling. Dice rolls here are fixed in code.
The model's answer is a list of 10 chances. Right now “sat” only gets 8.3% and “mat” gets 41%. That's the end of the forward pass. Next: learning from this mistake.
Measure how wrong the guess was, find which way each number should move, nudge, repeat.
Hover or tap a token to follow it. In step 5, drag the slider or tap − / +.
blue frame: learned weights (training changes them)green frame: numbers worked out for this sentence
1The training data: our corpus
Training uses nothing but the four sentences cleaned in module 1: the same tokens and the same IDs. They hold 22 “guess the next token” questions, each with a known right answer.
Each question is a (context, next token) pair. This is self-supervised: the text supplies its own answers.
2The training loop
Training is four moves, repeated. Only the last move changes the model.
forward pass → loss → gradients (backpropagation) → gradient descent step. This next-token training is the only kind of training in this course; other methods are out of scope.
3How wrong was the guess?
One number scores each guess: small when the right token got a big chance, big when it got a small one.
loss = −log p(right token). Called cross-entropy loss.
4Which way should each score move?
Subtract the right answer (1 for “sat”, 0 for the rest) from the chances. Negative → raise that score. Positive → lower it.
∂loss/∂scores = p − y, where y is the one-hot right answer.
5One nudge, worked by hand
The push travels back to the table U: each number moves by (cat's row × push), times a small step size.
∂loss/∂U = hᵀ(p − y); U ← U − η · ∂loss/∂U. The same idea reaches E, P and the W tables, one step further back each time (chain rule).
6Now repeat, for every question
Drag the slider. Each step nudges every table using all 22 questions at once. The loss falls and “sat” wins.
150 steps of plain gradient descent, η = 0.3. All 26 frames are computed in Python, not in your browser.
After training, the same forward pass gives “sat” 99% (it started at 8.3%). The tables now hold what our four-sentence corpus taught them.
Same forward pass with frozen weights: pick a word from the chances, append it, go again.
Hover or tap a token to follow it. In step 2, drag the temperature slider or tap − / +.
blue frame: learned weights (training changes them)green frame: numbers worked out for this sentence
1Same forward pass, trained tables
Exactly modules 1 to 5 again, with the tables from training. Now “sat” wins by far.
Inference = forward pass only. No loss, no gradients, no weight updates.
2Temperature: how bold to be
Before rolling the dice, divide every score by the temperature. Low = almost always the top token. High = more variety.
p = softmax(scores / T). Every frame is computed in Python with the same 20 fixed dice rolls.
3Append and repeat
The picked token is added to the input and the whole forward pass runs again, one token at a time.
This loop is called autoregressive generation.
4Back to our corpus
The test: start each sentence from module 1's corpus and let the trained model finish it on its own.
Greedy generation, computed in Python with the trained tables from module 6.
5Training vs inference
Both run the same forward pass. Only training compares with a right answer and changes the tables.
The full journey: text → tokens → vectors → question, name tag, message → attention → chances → “sat”. Training made the tables; inference just uses them.