- Key idea
- Cross-entropy, the log loss, and perplexity
- Real world
- A weather forecaster is scored on every forecast. Saying 90% rain when it rains costs little; saying 10% rain when it rains costs a lot. Being confidently wrong is punished hardest.
- In an LLM
- Pre-training and fine-tuning both minimise cross-entropy: at every position, the loss is minus the log-probability the model gave to the token that actually came next.
Surprise under the wrong model
Entropy is your average surprise when you know the true probabilities. But you usually don't: you have a model's guess. Cross-entropy is your average surprise when outcomes come from the true distribution P but you measure surprise using your model Q:
If Q matches P, cross-entropy equals entropy. If Q is wrong, it's higher. It can never be lower.
It rains. The forecaster's surprise, :
| Forecast for rain | Score (bits) |
|---|---|
| 90% | 0.15 |
| 50% | 1.00 |
| 10% | 3.32 |
| 1% | 6.64 |
Averaged over a year of forecasts, this is their cross-entropy. The only way to score well is to give honest probabilities: hedging everything at 50% costs 1 bit every day, and overconfidence is disastrous on the days you're wrong.
The training loss of a language model
In training data, each position has one actual next token. The true distribution P is then "100% on that token", and cross-entropy simplifies to a single term:
Text: "The cat sat on the mat". At the last position the model gave mat probability 0.4.
- Loss:
- If the model had said 0.9:
- If it had said 0.01:
Training lowers the average of this number over billions of positions. That is all "next-token prediction" means.
Perplexity is : a loss of 0.92 is a perplexity of 2.5, "as unsure as choosing evenly among 2.5 options". Lower is better.
Try it
Entropy + something
The widget’s stacked bar shows the key identity:
The entropy part is the data's own unpredictability: no model can remove it. The rest is what you lose by having the wrong model, and it has its own name: KL divergence, on the next page.
Where this shows up
- Supervised fine-tuning and distillation minimise cross-entropy: distillation, training an LLM with RL.
- Policy gradient is cross-entropy on the model's own samples, weighted by advantage: policy gradient.
Definitions
For a dataset of tokens with contexts , the language-modelling loss is