In this lesson
Training, the machine that fixes its own numbers
Course
From text to chatbot
Course order, not one forward pass.
I · From text to representations
II · Inside the Transformer
III · Prediction and learning
- 07GenerationdecodingNot started
- 08Trainingbuild weightsNot started
IV · From model to chatbot
Loss, gradient, small step, repeat. Free exactly two numbers, watch gradient descent drag a row of the map, and see chapter 7's absurd bet flip.
By the end, you can
- Explain training as loss measurement, gradient computation, and repeated small parameter updates.
- Explain why learned geometry serves prediction rather than human intuition.
- Compare exact counting, backed-off counting, and a shared neural representation on an unseen context.
Before you start: Logits, temperature, and the token that comes out
Nobody wrote the numbers in a real language model. Not the embedding table, not the Q, K, V projections, not the MLP - not one of the billions. Chapter 7 ended with the proof that this matters: our lovingly hand-built tower, given “eat an”, bet 46% on cat. This chapter shows the mechanism that writes the numbers: measure how wrong you are, compute which direction makes you less wrong, take a small step, repeat. By the end, the same bet lands on orange - and you will have watched every step of the repair.
Observe what gets to move
A parameter is one adjustable number inside the machine; this toy has dozens, real models have billions. Real training adjusts all of them, which is impossible to watch, so this lab freezes the tower and frees exactly two numbers: the coordinates of orange’s row in the chapter 3 embedding table - the very row you once dragged by hand. The context vector h for “eat an” stays fixed; the only question is where the machine will drag that one row, and it answers with three moves: a loss that scores the current bet, a gradient that points uphill, and a small step the other way.
Predict the shape of learning
The loss starts at 6.8 - the price of giving the right answer a 0.1% chance.
Your bet
At learning rate 0.1, what will the loss curve look like as you train?
Nobody grades this. Commit, and let the instrument settle it.
The whole tower is frozen except two numbers: orange’s row in the chapter 3 table. Watch gradient descent drag it.
The model’s bet
| Word | Share |
|---|---|
| cat | 46.1 % |
| dog | 31.8 % |
| queen | 11.0 % |
- Loss −log p(target)
- 6.819
- p(target)
- 0.1 %
The model still bets wrong. Keep training.
Loss curve
The map, during training
Seven rows stay fixed; orange’s row (in orange) is dragged step by step. The trail shows its path.
cos(orange, cat) -0.272
Chapter 1 promise
Generalization lab
Same training corpus, new context. All three methods must bet on “peel an,” a row none of them saw.
Visible training corpus
eat an → orange × 6draw an → apple × 3spot an → dog × 2drive a → car × 4ride a → bicycle × 4pet a → cat × 3
Held-out test
peel an → orange ?never shown during trainingExact counting
Looks up the whole row.
No row. No bet.Backed-off counting
Drops the first word and counts after “an.”
- p(orange)
- 54.5 %
- rank
- #1
- examples used
- 11
Tiny neural model
Shares one 2D map across contexts.
- p(orange)
- 96.5 %
- rank
- #1
- examples used
- 22
Exact counting has no row. Backoff recovers a weaker clue. Near “eat,” the shared representation ranks orange first.
Live missions
- Flip the bet to orange
- Make transfer lose its useful clue
Real computation, handcrafted 2D map. The softmax classifier learns its weights by gradient descent on the 22 visible examples. A real model learns its representation too, across thousands of dimensions.
Inspect the gradient
| Context vector h (frozen) | [3.661, 1.974] |
|---|---|
| Pull strength (1 − p) | 0.999 |
| Current gradient | [-3.657, -1.972] |
| Current orange row | [-0.680, 0.440] |
gradient = −(1 − p(target)) × h; update: row ← row − rate × gradient
The lower half pays off chapter 1’s promise with a context deliberately kept out of training: “peel an.” In chapter 1 that very phrase sat in the corpus, where “peel” led to “an” with 100% certainty; here its pairs were pulled from training on purpose. All three methods saw the same 22 context-and-next-word examples, and none of them ever saw “peel an.” Exact counting is speechless because it has no row for the whole phrase. A patched counter called backoff gives up on peel and falls back to the weaker statistics after bare an. The tiny neural model does something neither can: its handcrafted vector for peel sits near eat, so everything learned from “eat an → orange” flows through the same weights to the new context. One function, reused across neighboring representations - that is what exact counting never had.
Move peel away from eat. The network weights do not change, but the prediction does. That is the useful bias and the danger in one gesture: representation geometry decides which examples can lend evidence to which new contexts.
Manipulate the descent
Train step by step and watch three displays move together: the loss falls, p(orange) climbs, and on the map, the orange row physically slides - gradient descent is a drag operation, executed by calculus. Rate 0.1 is the curve your Predict bet was about: train ten steps there and the observatory stamps your bet confirmed or contradicted against the shape it actually drew. The bet flips at step 4. Now reset to rate 0.01: same destination, thirty-eight steps of crawling. Rate 1 crosses in a single stride. For these three rates, bigger is simply faster.
Then push the stride to rate 5 and watch it break. The step is so oversized it leaps clean past the bottom, lands up the far wall, and bounces back - the loss saws up and down, the bet blinks on and off, and it never settles. That is the lesson too big a stride overshoots. Read it as an illustration, not this lab’s real gradient: our two-parameter bowl has a floor that runs off to infinity, so the true descent here can never overshoot. The oscillation is drawn in to show you the failure that only bites at scale - the aside below says why.
One last mode: switch from Watch it descend to Beat the optimizer. Now you grab orange’s row and drag it yourself, hunting the lowest loss by hand. Then unleash gradient descent and race your path against its. Match its final loss without the calculus and the mission is yours - proof, felt in the fingers, of exactly how much work the gradient was doing for you.
Challenge Three missions. One: after the bet flips, keep training and watch the pull strength (1 − p) in the inspector. Explain why training slows itself down as the model gets it right. Two: look at where the orange row ends up on the map - right next to cat. Check the neighbors it left behind. Something about the chapter 3 map has been quietly sacrificed; name it. Three: switch to Beat the optimizer, drag orange by hand until your best loss matches or beats the one gradient descent reaches, then Copy link and share the state of your hand-tuned win.
Compare your answer
One: the pull is proportional to (1 − p), the slice of probability still missing from the truth. As p climbs toward 1 that slice shrinks toward 0, so each step nudges less and the model brakes itself as it gets the bet right. Two: orange was dragged into cat’s corner to win this one context, and in doing so it abandoned the neighbors that gave it meaning in chapter 3, apple and the other fruit. The map’s honest geometry was sacrificed to a single prediction, which is overfitting in miniature.
Explain the three-step loop
The loss is cross-entropy: −log p(target). Read that expression as a surprise meter, not an algebra test: it stays near zero when the truth received a big share and blows up as that share approaches zero. It is brutal on confident mistakes - betting 0.1% on the truth costs about 7, betting 90% costs 0.1 - which is exactly the pressure that makes a model calibrate: line up its stated confidence with how often it is actually right - during pretraining, at least, since the post-training you meet next can pull that calibration back out of line.
The gradient, for this softmax head, has a closed form worth reading aloud:
gradient = −(1 − p(target)) × h
Math pause: a gradient as a slope arrow
You do not need to derive the formula to read it. A gradient is an arrow pointing toward faster increase in loss. Training subtracts that arrow, so it walks downhill. Here the minus sign points the target row toward h, while (1 − p) sets the pull: large after a surprise, small after a confident success.
Pull the target’s row toward the context vector, with a force proportional to how surprised you were. When p(target) is tiny, the pull is fierce; as the model gets it right, the pull fades to nothing. Training is self-extinguishing - the first challenge mission shows it live. And the update is just row ← row − rate × gradient, the whole ritual of modern AI in one line.
- Measureloss = −log p(orange)
- Differentiategradient = −(1 − p) × h
- Step and repeatrow ← row − rate × gradient
Now scale the picture up honestly. A real model computes this same gradient for every parameter simultaneously - embedding rows, Q, K, V, MLP weights, all of them - using backpropagation, which is the chain rule applied through the whole tower. It does so on billions of sentences, not one.
Those sentences come from training datasets assembled from sources such as public web pages, books, code, licensed collections, and human-written examples; the exact mixture depends on the model. Training turns statistical pressure from that material into weights, carrying useful patterns alongside gaps and biases from both the data and its selection.
And your second challenge found the cost of our shortcut: to win one bet, the machine dragged orange into cat’s neighborhood - their cosine went from −0.27 to 0.998. The map now serves prediction and horrifies a lexicographer. That is the deepest lesson this toy can teach: learned geometry answers to the loss, not to human taxonomy. Real models balance billions of sentences, so their geometry ends up useful for many bets at once - but it answers to the same master.
What the bowl cannot show. Two free parameters make a convex bowl whose floor runs to infinity, so the true gradient here never overshoots - the rate-5 path you watched oscillate is drawn in by hand, not this lab’s real descent. A billion-parameter loss landscape is nothing like a bowl, and there oversized steps genuinely destroy training runs - that claim rests on the optimization literature, not on this lab. Also: training inserted no fact and no understanding. It made one number go down. Grammar, arithmetic, “knowledge,” and the biases of the data all enter a real model the same way: as whatever helps that number go down.
Reflect on what generalization needs
Checkpoint
All three methods trained on the same corpus, and none saw “peel an.” Why does only the neural toy rank orange first?
Checkpoint
After training, orange's row sits right beside cat and far from the fruit it used to neighbor. What does that reveal about where the learned map's shape comes from?
Connect the repaired machine to the real ones
The toy now completes “eat an” with orange. Every internal model stage has been built, and the one mystery chapter 7 left standing - where good numbers come from - is now a mechanism too: wrongness, measured; direction, computed; steps, repeated. Scale this to billions of parameters and oceans of text and you get what practitioners call a base model: a magnificent continuer of text, and nothing else. It completes; it is not explicitly optimized to answer, refuse, or follow instructions reliably. The final four lessons trace the remaining distance from that raw continuer to the chatbot you use.
Sources and scope
- Rumelhart, Hinton & Williams (1986) introduces backpropagation - the chain-rule machinery that computes these same gradients through every layer of a real network.
- Bengio, Ducharme, Vincent & Jauvin (2003) trains embedding rows exactly this way - by gradient descent on next-token cross-entropy - at real scale.
- This lab differentiates two free parameters analytically; no backpropagation through the tower is performed, and the convex-bowl behavior of the learning rates does not generalize to real loss landscapes.
- Content and claims reviewed on July 18, 2026.