The next-token cross-entropy objective
Write 𝓛 = −(1/T) Σ log p_θ(x_{t+1} | x_{≤t}) and explain it as maximum likelihood over a corpus. State the loss of a uniform guess, log V, as a reference point.
Builds on: Language modeling as next-token prediction, Maximum likelihood as cross-entropy minimization, Softmax, logits and the log-sum-exp trick, What training does
What the animation says
Only the true token counts
At every position, the model spreads probability over its whole vocabulary. But the text gives us just one answer: the token that really came next. So we look up only that token's probability, and turn it into a penalty: minus its log. Confident and right costs almost nothing. Hedging costs a little. Confident and wrong costs without limit. Here are three predictions. Tap the one that pays the biggest penalty.
Many penalties, one average
Penalties from every position now need to become one number. A whole text's probability is the product of its per-token probabilities. The log of a product is a sum of logs. So the summed penalties are exactly minus the log of the whole text's probability. Divide by the number of positions. That average is the loss. Pushing the loss down pushes the corpus's probability up. That is maximum likelihood.
Worked: the cat sat .
Take a four-token vocabulary and one sentence: the cat sat, full stop. After 'the', the model gave 'cat' one half. Penalty: zero point six nine three. After 'the cat', it gave 'sat' a quarter. Penalty: one point three eight six. After 'the cat sat', the full stop got zero point eight. Penalty: zero point two two three. The penalties add up to two point three zero two nats. So what is the loss?
Compared with a blind guess
It's the average: two point three zero two divided by three, which is zero point seven six seven nats. Is that good? Compare a model that knows nothing and gives every token one quarter. Then each position costs the log of four, one point three eight six. Every term is the same. Averaging equal terms gives that same term, however long the text. A blind guess scores the log of the vocabulary size. Our model sits well below it. It has learned something.
From logits, computed stably
Real models hand us raw scores called logits: one row per position, one column per token. The true next tokens are marked: column one, then two, then three. This line subtracts each row's largest score before exponentiating, so big scores can't overflow. Out come log-probabilities. This last line picks each true token's log-probability and averages the negatives. That gives about zero point seven seven four, against one point three eight six for guessing. Which position costs the most? Tap its row.
What to remember
So the loss is the average surprise at the token that really came next. Minimizing it is maximum likelihood on the corpus. The line to beat is the log of the vocabulary size. For fifty thousand tokens, that's about ten point eight nats. The floor is the entropy of the text itself. Averaged over real text, no model's loss can go below it.
Average surprise at what actually came
The objective and why it is maximum likelihood
Four-token vocabulary, one sentence
Loss from logits, computed stably
import numpy as np
V = 4
z = np.array([[2.0, 1.0, 0.1, -1.0],
[0.5, 0.2, 1.5, 0.0],
[0.0, 0.3, 0.1, 2.5]]) # logits z_t, [T, V]
y = np.array([1, 2, 3]) # actual x_{t+1}
m = z.max(axis=1, keepdims=True)
logp = z - m - np.log(np.exp(z - m).sum(axis=1, keepdims=True))
loss = -logp[np.arange(len(y)), y].mean()
print(loss, np.log(V)) # ~0.774 vs ~1.386 (uniform guess)
What to remember
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