Probability and Statistics
Information theory: entropy and cross-entropy
Measuring surprise and the loss that trains LLMs
When a friend says "the sun rose this morning," you learn nothing - you already knew it. When they say "it snowed in the desert," you sit up. Information theory turns that gut feeling into arithmetic: the surprise of an event is , measured in bits. Average that surprise over a whole distribution and you get entropy. Average it while using the wrong probabilities and you get cross-entropy - which, it turns out, is the exact number an LLM is trained to push down, token after token.
The core idea
Surprise, measured in bits
The information content (or "surprise") of an outcome with probability is
The minus sign is only there because probabilities are , so their logs are negative - surprise should be a positive number. Two properties make this the only sensible choice. First, it is monotone: smaller means larger surprise, and a sure thing () has surprise . Second, it is additive for independent events - the surprise of two coin flips is bits, not because we multiplied but because turns the product of probabilities into a sum of surprises.
Now average that surprise over the whole distribution, weighting each outcome by how often it actually happens. That average is Shannon entropy:
Entropy is the irreducible uncertainty of a source: the average number of yes/no questions you must ask to pin down its outcome, and - by Shannon's coding theorem - the shortest average code length in bits that any scheme can achieve. A predictable source (one likely outcome) has low entropy; a uniform source has the most, bits for equally likely options.
Cross-entropy and KL: paying for a wrong map
Here is the twist that matters for machine learning. Suppose reality follows a distribution , but you build your code - your model - as if the distribution were . You still get charged the real surprise every time occurs, but occurs at the true rate . Your average bill is cross-entropy:
It is the average surprise of the truth, scored with the model's probabilities. Cross-entropy is never smaller than the true entropy, and the gap between them has a name - the Kullback-Leibler divergence, the number of extra bits you waste by using instead of :
Why the floor is H(p), and why it is a floor
Drag the model's guesses
Below is a miniature language model. The prompt is "The cat sat on the ___" and the vocabulary has four tokens. The dashed ticks are the true next-token distribution (what the corpus actually does); the solid bars are the model's guess , which you can drag up and down. As you make resemble , the cross-entropy loss slides toward its floor . Starve the true winner of probability - make the model confident and wrong - and watch the loss bolt toward infinity. Hit "Draw the actual token" to sample from reality and see the surprise you paid on that one draw.
Bars start uniform (1/4 each). Drag one up - the others shrink to keep the guess a valid distribution summing to 1.
Why this is the language-model loss
Cross-entropy, and the one-hot special case, in a few lines:
# p: the true distribution, q: the model's guess (both sum to 1)
import numpy as np
def cross_entropy(p, q):
return -np.sum(p * np.log2(q)) # bits; use np.log for nats (frameworks' default)
# The LM case: the target is one-hot on the real next token.
loss = -np.log2(q[true_token]) # surprise of the actual token, in bits
ppl = 2 ** loss # perplexity: the "effective number of choices"PyTorch's cross_entropy uses the natural log, so its numbers are in nats rather than bits - but the story is identical, and perplexity is just instead of . Base changes the unit, not the meaning.
Check yourself
Cross-entropy H(p,q) reaches its minimum value exactly when:
A model that is confident and wrong - near-zero q on the true token - makes the loss:
What extra quantity does cross-entropy add on top of entropy, and when does it vanish?
The decomposition is the single most important identity in this lesson. Try to state it, then check.
Lock it in
- Surprise of an event is bits - rare events carry more information.
- Entropy is the average surprise - the irreducible uncertainty of a source.
- Cross-entropy is the average surprise when you score the truth with the wrong model.
- KL divergence is the gap: the extra bits you waste. Minimizing cross-entropy is the same as minimizing KL.
- The LLM training loss is cross-entropy over the vocabulary - surprise of the correct token, summed across the corpus.
Primary source
Closing the loop