Measuring Surprise — Information Theory
Chapter 0-3 showed that feeding a Bernoulli distribution into maximum likelihood produces cross-entropy. As a formula it was finished there. What is left is the name. Why "entropy"? Why "cross"?
Recovering a name would be trivia on its own, but this chapter buys something else: how far the loss can fall. Chapter 0-2 showed that a minimum need not exist. Here we see the opposite case — a floor above zero. Its height is exactly the entropy.
1. Information — the size of a surprise
"The sun will rise tomorrow" tells you nothing. "It will snow tomorrow" tells you something. The less likely it was, the more there is in being told. Write that as a number.
Definition. The information (self-information) of an outcome occurring with probability is
Why a logarithm is forced by three properties you would want:
- (certain) carries information
- smaller carries more (monotone decreasing)
- being told about two independent outcomes together carries the sum of being told separately
The third does the work. For independent outcomes , so must turn multiplication into addition — and the only continuous functions that do are constant multiples of . The constant is the unit: base 2 gives bits, base gives nats. Deep learning implementations use almost everywhere (log is the natural log), so the unit there is nats, but this chapter uses bits, where intuition lives. .
2. Entropy — average surprise
Definition. The entropy of a distribution is
Equation (2) is the expectation of equation (1) (Chapter 0-3, equation (3)): draw one sample from this distribution, and how surprised are you on average?
Worked numbers. One coin.
| probability of heads | (bits) |
|---|---|
| 0.5 | 1.000000 |
| 0.9 | 0.468996 |
| 0.99 | 0.080793 |
| 1.0 | 0.000000 |
A fair coin is exactly 1 bit — that is what defines the bit: one yes/no question's worth. Bias it and the number falls; make it certain and it is zero, because there is nothing to be surprised by.
The uniform distribution is the maximum. With equally likely outcomes, : bits for , for .
3. Cross-entropy — measuring while believing the wrong distribution
Suppose reality is but you built your code believing it was . Lengths follow , giving , while the frequencies that actually arrive follow . The average length is
That is cross-entropy. The distribution you average over and the distribution that sets the lengths disagree — they cross — hence the name. It is the same expression as equation (7) of Chapter 0-3; that one came from maximum likelihood, this one from code lengths.
Worked numbers. For and : and bits. The belief cost you the difference.
4. KL divergence — the part that got longer
Definition. Call that excess
the KL divergence (relative entropy).
For the numbers above, bits; computing the right-hand side of equation (4) directly also gives .
, with equality only when . Two lines, using for (equality at ):
so
It is not a distance. The same pair gives different numbers depending on the direction: above, bits, against . The triangle inequality fails too. Read it not as "how far apart they are" but as "what using costs you when reality is ."
5. The join with training — why this is the loss being minimised
When the label is one-hot. One correct class out of three means , so , and equation (4) gives
the cross-entropy simply is the KL. With , bit nats, and the KL is the same number. So "lower the cross-entropy" reads directly as "move the model's distribution toward the true one." That recovers the name.
When the label is the data itself. In real training the true per-class probabilities are unknown; all you have is examples. Put mass on each and use that empirical distribution in place of :
The right-hand side is equation (6) of Chapter 0-3 — the mean negative log-likelihood itself. "Minimise cross-entropy against the empirical distribution" and "do maximum likelihood" are one operation under two names. The worked example from that chapter (, ) is nats bits.
A common misreading: "the loss won't approach zero, so the model must be too weak"
Equation (5) says for every , with equality only at :
The floor of the loss is , and it has nothing to do with the model. One-hot labels give and a floor at zero, but whenever the same input can carry different answers — label noise, genuinely ambiguous cases, measurement spread — lifts the floor off the ground.
Measure it. Suppose for some input the truth is : one time in ten the label is 0.
| model output | cross-entropy (nats) | accuracy |
|---|---|---|
| 0.9 (the true value) | 0.325083 | 90% |
| 0.95 | 0.345737 | 90% |
| 0.99 | 0.469562 | 90% |
| 0.999 | 0.691676 | 90% |
The best reachable value is nats bits, and that is the floor. Sweeping numerically puts the minimum at with value — the theory's number exactly.
The last three rows are what bites in practice. Raising confidence moves accuracy by not one point and makes the loss worse. At the accuracy is still 90% while the loss has more than doubled. "Push it to be more confident and the loss will drop" is backwards: being more certain than the truth is punished. That is the entrance to calibration, and when a loss stops falling, the first thing to question is not model capacity but how high the floor was to begin with.
Exercises
The answers are folded away. Work them first.
Exercise 1. With and : (a) what does the gap between and represent? (b) Does equal ? (c) Give two reasons KL cannot be called a distance.
Show the answer
(a) The cost of believing — the extra code length — which is bits, i.e. .
(b) No. bits, a different number from . The distribution you average over changes from to , so which disagreements get weighted heavily changes too.
(c) ① It is not symmetric (see (b)) ② it does not satisfy the triangle inequality. A metric requires both. KL only gives "non-negative, and zero exactly when equal." We use it anyway because it connects to maximum likelihood and can be computed.
Exercise 2. Three-class classification, the true class is class 1, the model outputs . (a) The cross-entropy in bits and nats. (b) What are and here? (c) What happens to the loss as rises to ?
Show the answer
(a) , so bit nats. Only the probability placed on the correct class matters; how the other two split is irrelevant — those terms are multiplied by .
(b) and bit. With one-hot labels the cross-entropy and the KL are the same number.
(c) , , bits. Pushing the correct class toward 1 drives it toward zero. Here the floor is zero (), so it can always be pushed lower — exactly the "no minimum exists" shape from Chapter 0-2.
Exercise 3. For some input the truth is . (a) The smallest achievable cross-entropy (nats). (b) The accuracy there. (c) What happens if the model outputs ?
Show the answer
(a) nats ( bits), reached at .
(b) 80%. Always answering 1 is optimal and misses two times in ten. Both the loss and the accuracy are set by the data, not the model.
(c) The loss diverges. On the rounds where you pay . Implementations clamp at so it is not literally infinite, but that one example produces a huge value and dominates the gradient. It is the limit of "wrong, and confident about it" — the same phenomenon as Exercise 3 of Chapter 1 in Part III.
What this chapter does not say
- Mutual information and conditional entropy are not covered. "How much does knowing reduce the uncertainty in " is writable with this chapter's tools, but the places it gets used (feature selection, representation learning) come later, so it goes there.
- Entropy of a continuous distribution (differential entropy) is a different object. Turn equation (2) into an integral and it can go negative, and the "average code length" reading breaks. Everything here is discrete.
- Shannon's coding theorem is used, not proved. Its content is both halves: you cannot go below , and you can get arbitrarily close to it.
- Calibration — whether a model's stated probabilities match observed frequencies — is not measured or corrected here. This chapter only shows the shape: overconfidence is punished by the loss.
- Every number in the text was computed and checked (the coin and uniform entropies, that holds, both directions of the KL, the one-hot case, the floor at against a numerical sweep of , and that 20,000 random pairs produced no negative ).
Going deeper
- Information theory and AI — where the cross-entropy loss came from
- Entropy and cross-entropy — where loss functions come from
- KL divergence from scratch — measuring the gap between two distributions
- Mutual information — measuring "knowing"
- Entropy coding from scratch — from Huffman to arithmetic coding
- Compression = prediction = intelligence — LLMs through information theory
Comments
Sign in to comment