The training objective
Next-token prediction as a differentiable game: cross-entropy loss, gradients, and the learning-rate dance.
Everything expensive about LLMs — GPUs for weeks, trillions of tokens — boils down to one repeated move: guess the next token, measure how wrong you were, nudge the weights to be less wrong. This lesson makes that move precise.
Start here
Training is next-token prediction played millions of times. The "measure how wrong" is cross-entropy: the negative log of the probability the model assigned to the token that actually came next. Predict confidently and correctly → tiny loss. Miss badly → large loss.
The loss
For one position, the model produced probability for the true next token . The loss is:
The negative log has exactly the shape you want: → loss 0; → loss ≈ 4.6; → loss explodes. Being confidently wrong costs more than being uncertain, and there is no floor on how bad a miss can be — the model is always pushed toward the truth.
Where the formula comes from
It's not arbitrary — it's maximum likelihood, walked backward. A language model is a probability model; "training it" means choosing parameters that make the training data as probable as possible. For one next-token prediction, the probability the model assigns to the true token is ; maximizing that is equivalent to minimizing its negative log:
Writing it over the whole vocabulary with a one-hot vector (all zeros except the true token) gives the textbook form — cross-entropy:
The sum collapses to a single term because is one-hot. (When labels aren't one-hot — label smoothing, soft distillation targets — the sum becomes a weighted average of , which is where those techniques get their effect.) And it's called cross-"entropy" because it compares the data's true entropy against the entropy of encoding it with the model's distribution — cross-entropy = true entropy + KL divergence, so minimizing cross-entropy minimizes the model's mismatch with the data. With natural logs the unit is nats, which is why perplexity is .
Two companion readings worth memorizing:
- Perplexity (for natural-log loss): "the model is effectively choosing among equally likely tokens." Loss 2.0 ⇔ perplexity ≈ 7.4. This is the number people quote when comparing pretraining runs.
- The gradient is soft. has derivative : the worse the model's estimate of the true token, the harder it's pushed. Corrections scale with the miss — no manual per-example reweighting needed.
During pretraining, every position of every sequence is a training example at once: given "The cat sat on the ___", the model is scored on "mat" — and simultaneously on "The" given context, "cat" given "The", and so on. One sequence of 1000 tokens yields ~1000 next-token predictions. This density is why next-token prediction is such a cheap supervision signal: no human labels, the text labels itself.
From loss to updated weights
The nudging is calculus the model never sees explicitly:
- Forward pass — predict next tokens for a batch of sequences.
- Backward pass — compute, for every one of billions of parameters, how much it contributed to the loss (the gradient) — the next lesson opens this up.
- Update — move each parameter a small step against its gradient: . The step size is the learning rate.
Repeat over billions of positions. The optimizer in practice is AdamW, which also tracks per-parameter momentum — details matter less than the shape of the loop.
The learning-rate dance
Watch what the step size does to training:
Training loss vs steps — illustrative curve, not real training
good: smooth power-law decay — most of the progress happens early
- Too high — each update overshoots; loss bounces or diverges. The classic early-training crash.
- Too low — progress is glacial; you pay full compute for little improvement.
- Good — a smooth power-law decay: huge early gains, then slow, steady refinement. Real runs combine a short warmup (ramp from 0) with a long decay (gradually shrinking steps so the model settles into a minimum instead of bouncing around it).
Careful
The loss curve is the single most-watched chart in a pretraining run. It is smooth power-law decay when healthy — if it spikes, the first suspects are the learning rate and a corrupted data batch, in that order.
What next-token prediction does and doesn't buy
Two facts worth holding onto:
- It buys everything at once. Grammar, facts, style, some reasoning — all compressed into "predict the next token" over trillions of varied tokens. There is no separate grammar module.
- It does not align the model. Next-token prediction produces a text-completer, not an assistant. A base model asked "How do I make bread?" might respond with "…and why is bread important in history?" — the statistically plausible continuation, not the helpful one. Fixing that mismatch is post-training, later in this wiki.
Next: the backward pass itself — how the gradient reaches every one of those billions of parameters in a single walk.