Textbook › Part 0 Mathematical Foundations
CHAPTER 0-4

Measuring Surprise — Information Theory

FREE14 min

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 xx occurring with probability p(x)p(x) is

I(x)  =  −log⁡p(x)I(x) \;=\; -\log p(x)
(1)

Why a logarithm is forced by three properties you would want:

  1. p=1p = 1 (certain) carries information 00
  2. smaller pp carries more (monotone decreasing)
  3. being told about two independent outcomes together carries the sum of being told separately

The third does the work. For independent outcomes p(x,y)=p(x)p(y)p(x, y) = p(x)p(y), so II must turn multiplication into addition — and the only continuous functions that do are constant multiples of log⁡\log. The constant is the unit: base 2 gives bits, base ee gives nats. Deep learning implementations use ee almost everywhere (log is the natural log), so the unit there is nats, but this chapter uses bits, where intuition lives. 1 nat=1.442695 bits1\ \text{nat} = 1.442695\ \text{bits}.

2. Entropy — average surprise

Definition. The entropy of a distribution pp is

H(p)  =  Ex∼p[ I(x) ]  =  −∑xp(x)log⁡p(x)H(p) \;=\; \mathbb{E}_{x \sim p}\big[\,I(x)\,\big] \;=\; -\sum_{x} p(x) \log p(x)
(2)

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 HH (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 KK equally likely outcomes, H=log⁡2KH = \log_2 K: 3.3219283.321928 bits for K=10K = 10, 9.9657849.965784 for K=1000K = 1000.

Entropy also means code length. Encoding a stream of symbols drawn from $p$, the average length per symbol cannot be pushed below $H(p)$ bits (Shannon). "Average surprise" and "shortest possible average code length" are the same number — that is what makes this quantity interesting, and the "cross" in the next section is clearest read this way.

3. Cross-entropy — measuring while believing the wrong distribution

Suppose reality is pp but you built your code believing it was qq. Lengths follow qq, giving −log⁡q(x)-\log q(x), while the frequencies that actually arrive follow pp. The average length is

H(p,q)  =  −∑xp(x)log⁡q(x)H(p, q) \;=\; -\sum_{x} p(x) \log q(x)
(3)

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 p=(0.7, 0.2, 0.1)p = (0.7,\,0.2,\,0.1) and q=(0.5, 0.3, 0.2)q = (0.5,\,0.3,\,0.2): H(p)=1.156780H(p) = 1.156780 and H(p,q)=1.279586H(p,q) = 1.279586 bits. The belief cost you the difference.

4. KL divergence — the part that got longer

Definition. Call that excess

DKL(p ∥ q)  =  H(p,q)−H(p)  =  ∑xp(x)log⁡p(x)q(x)D_{\mathrm{KL}}(p \,\|\, q) \;=\; H(p,q) - H(p) \;=\; \sum_{x} p(x) \log \frac{p(x)}{q(x)}
(4)

the KL divergence (relative entropy).

For the numbers above, 1.279586−1.156780=0.1228061.279586 - 1.156780 = 0.122806 bits; computing the right-hand side of equation (4) directly also gives 0.1228060.122806.

DKL≥0D_{\mathrm{KL}} \ge 0, with equality only when p=qp = q. Two lines, using log⁡x≤x−1\log x \le x - 1 for x>0x > 0 (equality at x=1x=1):

−DKL(p∥q)=∑xp(x)log⁡q(x)p(x)  ≤  ∑xp(x)(q(x)p(x)−1)=∑xq(x)−∑xp(x)=1−1=0-D_{\mathrm{KL}}(p\|q) = \sum_x p(x)\log\frac{q(x)}{p(x)} \;\le\; \sum_x p(x)\left(\frac{q(x)}{p(x)} - 1\right) = \sum_x q(x) - \sum_x p(x) = 1 - 1 = 0

so

DKL(p ∥ q)  ≥  0,with equality exactly when p=qD_{\mathrm{KL}}(p \,\|\, q) \;\ge\; 0, \qquad\text{with equality exactly when } p = q
(5)

It is not a distance. The same pair gives different numbers depending on the direction: above, DKL(q∥p)=0.132775D_{\mathrm{KL}}(q\|p) = 0.132775 bits, against 0.1228060.122806. The triangle inequality fails too. Read it not as "how far apart they are" but as "what using qq costs you when reality is pp."

FIG 1Above, the true distribution p; below, the model's q. Drag q's bars and the lower stacked bar — the cross-entropy — splits into orange (H(p)) and gold (KL). Press "set q = p" and the gold vanishes, leaving only orange: that is the floor of the loss. Press "swap p and q" and the KL changes value, which is why it is not a distance

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 p=(1,0,0)p = (1, 0, 0), so H(p)=0H(p) = 0, and equation (4) gives

H(p,q)=H(p)+DKL(p∥q)=DKL(p∥q)H(p,q) = H(p) + D_{\mathrm{KL}}(p\|q) = D_{\mathrm{KL}}(p\|q)

the cross-entropy simply is the KL. With q=(0.5,0.3,0.2)q = (0.5, 0.3, 0.2), H(p,q)=−log⁡20.5=1.000000H(p,q) = -\log_2 0.5 = 1.000000 bit =0.693147= 0.693147 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 mm examples. Put mass 1/m1/m on each and use that empirical distribution p^\hat{p} in place of pp:

H(p^,q)  =  −1m∑i=1mlog⁡q(yi∣xi)H(\hat{p}, q) \;=\; -\frac{1}{m}\sum_{i=1}^{m} \log q(y_i \mid x_i)
(6)

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 (y=(1,0,1,1,0)y=(1,0,1,1,0), p=(0.9,0.2,0.7,0.6,0.3)p=(0.9,0.2,0.7,0.6,0.3)) is 0.310535920.31053592 nats =0.448009= 0.448009 bits.

One formula, three doors: maximum likelihood (Chapter 0-3), code length (this chapter) and minimising KL (this section). They all arrive at the same place, so any one door suffices. The value of knowing three is that when you get stuck you can look again from a different one.

A common misreading: "the loss won't approach zero, so the model must be too weak"

Equation (5) says H(p,q)≥H(p)H(p,q) \ge H(p) for every qq, with equality only at q=pq = p:

min⁡q  H(p,q)  =  H(p)\min_{q} \; H(p, q) \;=\; H(p)
(7)

The floor of the loss is H(p)H(p), and it has nothing to do with the model. One-hot labels give H(p)=0H(p) = 0 and a floor at zero, but whenever the same input can carry different answers — label noise, genuinely ambiguous cases, measurement spread — H(p)>0H(p) > 0 lifts the floor off the ground.

Measure it. Suppose for some input the truth is P(y=1∣x)=0.9P(y=1 \mid x) = 0.9: one time in ten the label is 0.

model output qq 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 0.3250830.325083 nats =0.468996= 0.468996 bits, and that is the floor. Sweeping qq numerically puts the minimum at q=0.90000q = 0.90000 with value 0.3250830.325083 — 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 q=0.999q = 0.999 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.

Having a floor and being at the floor are different things. A loss can stop falling for three reasons: ① it reached the floor $H(p)$, ② optimisation is stuck (Chapter 4), ③ the model's form is not rich enough. Telling them apart needs the training loss placed next to an estimate of $H(p)$. Do not conclude "it stopped, so this is the floor."

Exercises

The answers are folded away. Work them first.

Exercise 1. With p=(0.7, 0.2, 0.1)p = (0.7,\,0.2,\,0.1) and q=(0.5, 0.3, 0.2)q = (0.5,\,0.3,\,0.2): (a) what does the gap between H(p)H(p) and H(p,q)H(p,q) represent? (b) Does DKL(q∥p)D_{\mathrm{KL}}(q\|p) equal DKL(p∥q)D_{\mathrm{KL}}(p\|q)? (c) Give two reasons KL cannot be called a distance.

Show the answer

(a) The cost of believing qq — the extra code length — which is DKL(p∥q)=0.122806D_{\mathrm{KL}}(p\|q) = 0.122806 bits, i.e. 1.279586−1.1567801.279586 - 1.156780.

(b) No. DKL(q∥p)=0.132775D_{\mathrm{KL}}(q\|p) = 0.132775 bits, a different number from 0.1228060.122806. The distribution you average over changes from pp to qq, 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 q=(0.5, 0.3, 0.2)q = (0.5,\,0.3,\,0.2). (a) The cross-entropy in bits and nats. (b) What are H(p)H(p) and DKL(p∥q)D_{\mathrm{KL}}(p\|q) here? (c) What happens to the loss as q1q_1 rises to 0.7, 0.9, 0.990.7,\,0.9,\,0.99?

Show the answer

(a) p=(1,0,0)p = (1,0,0), so H(p,q)=−log⁡20.5=1.000000H(p,q) = -\log_2 0.5 = 1.000000 bit =0.693147= 0.693147 nats. Only the probability placed on the correct class matters; how the other two split is irrelevant — those terms are multiplied by p2=p3=0p_2 = p_3 = 0.

(b) H(p)=0H(p) = 0 and DKL(p∥q)=1.000000D_{\mathrm{KL}}(p\|q) = 1.000000 bit. With one-hot labels the cross-entropy and the KL are the same number.

(c) −log⁡20.7=0.514573-\log_2 0.7 = 0.514573, −log⁡20.9=0.152003-\log_2 0.9 = 0.152003, −log⁡20.99=0.014500-\log_2 0.99 = 0.014500 bits. Pushing the correct class toward 1 drives it toward zero. Here the floor is zero (H(p)=0H(p)=0), 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 P(y=1∣x)=0.8P(y=1\mid x) = 0.8. (a) The smallest achievable cross-entropy (nats). (b) The accuracy there. (c) What happens if the model outputs q=1.0q = 1.0?

Show the answer

(a) H(p)=−(0.8ln⁡0.8+0.2ln⁡0.2)=0.500402H(p) = -(0.8\ln 0.8 + 0.2\ln 0.2) = 0.500402 nats (=0.721928= 0.721928 bits), reached at q=0.8q = 0.8.

(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 y=0y = 0 you pay −log⁡(1−q)=−log⁡0-\log(1 - q) = -\log 0. Implementations clamp qq at 1−ε1-\varepsilon 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 XX reduce the uncertainty in YY" 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 H(p)H(p), 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 H(p,q)=H(p)+DKLH(p,q) = H(p) + D_{\mathrm{KL}} holds, both directions of the KL, the one-hot case, the floor at P(y=1)=0.9P(y=1)=0.9 against a numerical sweep of qq, and that 20,000 random pairs produced no negative DKLD_{\mathrm{KL}}).

Going deeper

Comments

Sign in to comment