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.
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.
Predict the next token, get nudged, repeat
The loss and the update
Loss numbers for one position
A tiny model trained on a toy document
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
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.
Bytewise
All courses