TinyLanguageBuilder / The Tiny Textbook

Chapter 4: Learning

The model starts as random numbers and ends up writing sentences. Nobody tells it any grammar. All it ever gets is a score for how surprised it was by the right answer, and a rule for nudging its numbers to be a little less surprised next time. This chapter shows both, first on a toy and then on the real model.

1. Surprise is the score

Training shows the model some text and asks, at every position: what comes next? The model answers with a probability for every token in its vocabulary. We then look at the probability p it gave to the token that really came next, and turn it into a penalty called the loss:

loss = −ln(p)

If the model was sure and right (p near 1) the loss is near 0. If it gave the right token only a one in a thousand chance, the loss is large. A model that knows nothing spreads its probability evenly, so with a vocabulary of 512 tokens it scores ln 512 ≈ 6.24 everywhere. That number is the starting line.

Probability given to the right tokenLoss
0.9 (confident, right)0.11
0.5 (a coin flip)0.69
0.12.30
1 in 512 (pure guessing)6.24
0.0001 (confident, wrong)9.21

The loss punishes confident mistakes hard, which is exactly what you want. Training is the search for numbers that make the average loss over lots of text as small as possible.

2. Rolling downhill

How do you improve a number when you cannot see the whole landscape? You feel the slope where you stand and step downhill. That slope is the gradient, and the step rule is gradient descent:

new w = w − learning rate × slope

Here is the smallest possible model: one number w, and a loss that is lowest at w = 3. You choose where it starts and the learning rate, the size of each step.

Try a tiny learning rate (slow but safe), then about 0.9 (it overshoots and zigzags, yet still arrives), then anything above 1.0 (each step lands further away than the last: it diverges). Real training has the same trap, which is why the learning rate is the setting people tune most. The real model has about 60,000 numbers instead of one, and the gradient gives the slope for all of them at once. Working that out efficiently is backpropagation, and it is what the Rust code in this project does by hand.

3. Train the real model

Loading the engine…

The thin line is noisy because each step uses a different small batch of text. The held-out line is the honest one: if it stopped falling while the training line kept dropping, the model would be memorising rather than learning. Early on the loss drops quickly (the model first learns that spaces and common words are frequent), then it slows to a crawl, which is typical.

4. What the model expected

Pick a moment in training, then see how surprised that version of the model is by a sentence. Each token below is shaded by its loss: pale means the model saw it coming, solid means it was a shock.

    Click any token to see what the model guessed would come after it, with the real next token marked. At step 0 the guesses are junk with nearly equal chances. After training they are plausible words, and the right one is often near the top. A model this size is still wrong a lot, and that is fine: a good guess is only a probability.

    5. What to take from this, and what not to

    One number drives everything. Pre-training, fine-tuning and preference tuning all use this same loop: measure the loss, compute the gradient, take a small step. They differ only in which loss they measure.

    Lower loss is not always better behaviour. The loss only measures how well the model predicts text like its training data. A model can predict well and still say untrue or unhelpful things.

    The curve flattens. A tiny model has a limit on what it can capture, set by its size and the amount of text it saw. More steps past that point buy little. Bigger models and more data are what move the floor.

    Next chapter: how the model turns those probabilities into words, and why a setting called temperature changes whether it plays safe or gets creative.