All courses

What training does

The weights start random. The model is shown real text and, at every position, predicts the next token. The loss measures how little probability it gave to the token that actually came next. A gradient tells, for every weight at once, which small nudge would lower that loss, and an optimizer applies the nudges. This repeats over batches of text, trillions of tokens in total, on many GPUs. This phase is pretraining, and its product is a base model.

▶ Watch this lesson Start the whole course

What the animation says

Random numbers, fluent text?

A freshly built language model is billions of numbers, called weights, all set at random. Ask it to continue a sentence, and out comes gibberish. Nobody writes grammar rules or facts into those weights. Yet after training, the same model writes fluent text and answers questions. So what exactly is training pushing it to get better at?

One number: the loss

Training pushes one skill: predicting the next token of real text. At every position, the model gives each token in its vocabulary a probability. After “the cat”, the token that really came next is “sat”. The loss is minus the log of the probability given to that true token. From a random start at a quarter, the loss is 1.39. At 0.6, it's 0.51. At 0.99 it's nearly zero. Confidently wrong, at 0.01, it jumps to 4.61. Now the model favours “mat”. Tap the bar the loss reads.

Nudge every weight

For a batch, the loss is averaged over every position in it. The gradient then tells every weight, all at once, which small nudge would lower that average. Each weight steps a little against its gradient. The step size is called the learning rate. Step too far and you overshoot, which is why real optimizers refine this step. After the nudge, “sat” gets more probability. Probabilities add to one, so the others shrink.

A tiny model, trained

Here's the whole loop, small enough to read. The toy document repeats zero, one, two, three, then zero, one, two, four, twenty times over. Each row of W holds one token's scores for what comes next, so this model only sees the current token. Every step, scores become probabilities, the loss is averaged, and every weight gets nudged. The loss starts near 1.61, an even guess among five tokens. Where does it end up?

The floor, and the scale

The loss settles near 0.17, not zero. After token two, the next is three or four, half the time each. The best this model can say is fifty-fifty. That costs log two, about 0.69, at 40 of the 159 positions. Averaged, that's 0.17. A model that looked further back would see three and four alternate. Only true randomness in text can't be learned. Run this same loop over trillions of tokens on many GPUs: that's pretraining. What comes out is a base model.

How do random numbers learn to write?

A freshly built language model is billions of weights set to random values. Its outputs are gibberish. Nobody writes grammar rules or facts into it, yet afterwards it writes fluent text and answers questions. What process turns random weights into that, and what exactly is it being pushed to get better at?

Predict the next token, get nudged, repeat

Show the model real text. At **every** position it outputs a probability for each token in the vocabulary as the next one. The **loss** is high when the token that actually came next got little probability. The **gradient** gives, for every weight at once, the small nudge that would lower that loss. An **optimizer** applies the nudges. Repeat over batches, trillions of tokens, many GPUs: this is **pretraining**. Its product is a **base model**.

The loss and the update

At position $t$ the loss is $$\ell_t = -\log p_\theta(x_{t+1}\mid x_{\le t})$$ where $p_\theta(x_{t+1}\mid x_{\le t})$ is the probability the model, with weights $\theta$, gave the true next token. $\mathcal{L}$ is the mean of $\ell_t$ over all positions in the batch (natural log, so in nats). Update: $$\theta \leftarrow \theta - \eta\,\nabla_\theta \mathcal{L}$$ $\nabla_\theta\mathcal{L}$ holds one partial derivative per weight. Stepping against it lowers $\mathcal{L}$ if the step size $\eta$ (the learning rate) is small enough. Real optimizers refine this step.

Loss numbers for one position

Vocabulary of 4 tokens, text “the cat sat”. At “the cat” the target is “sat”. - Random start, ≈0.25 each: $-\ln 0.25 = 1.39$ - After some training, $p=0.6$: $-\ln 0.6 = 0.51$ - Well trained, $p=0.99$: $0.01$ - Confidently wrong, $p=0.01$: $4.61$ Only the target's probability enters the loss. Probabilities sum to 1, so raising “sat” lowers the others.

A tiny model trained on a toy document

Each row of `W` holds one token's scores for what comes next, so this model sees only the current token. The loss starts near $\ln 5 \approx 1.61$ and falls toward ≈0.17, not 0. After token 2 the next token is 3 or 4 half the time each. The best this model can do there is 0.5 each, costing $\ln 2$ at 40 of the 159 positions: $40\cdot 0.69/159 \approx 0.17$. A model that looked further back could spot that 3 and 4 alternate. Only randomness truly in the text is irreducible.
import numpy as np
rng=np.random.default_rng(0)
V=5; text=np.array([0,1,2,3,0,1,2,4]*20)
W=rng.normal(0,0.1,(V,V))              # random start
x,y=text[:-1],text[1:]                  # every position predicts the next
for step in range(201):
    z=W[x]; p=np.exp(z)/np.exp(z).sum(1,keepdims=True)  # scores -> probabilities
    loss=-np.log(p[np.arange(len(y)),y]).mean()
    g=p.copy(); g[np.arange(len(y)),y]-=1   # dloss/dscores: p, minus 1 at target
    G=np.zeros_like(W); np.add.at(G,x,g/len(y))  # gradient for every weight
    W-=5.0*G                                # nudge all weights
    if step%50==0: print(step,round(loss,3))

Remember this

Training is one repeated loop. Predict the next token at every position of real text. Score the prediction by $-\log$ of the probability given to the true token. Nudge all weights along the negative gradient. Doing this over trillions of tokens is pretraining, and it yields a base model.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.