---
title: Information theory for learning
heading: Entropy as average surprise, cross-entropy between a true and a predicted distribution, the Kullback–Leibler divergence as their gap, cross-entropy as the loss function for classifiers, and mutual information between inputs and outputs
eyebrow: S14 · Lesson 4 · Information theory for learning
xp: 50
---

::: hook
S8 used entropy to count the bits a source needs. Machine learning uses the same quantities to measure how wrong a model's predicted probabilities are. The loss function behind nearly every classifier, from logistic regression to the largest language models, is the cross-entropy between the true labels and the predicted distribution, and the amount it exceeds the data's own entropy is a divergence that's zero only when the model matches reality.
:::

::: play
- For a binary outcome with true probability p, the cross-entropy $H(p, q)$ of a model that predicts q, against q, and the entropy $H(p)$ (dashed), both in bits.
- Cross-entropy is smallest, equal to the entropy, when q = p. The gap above it is the Kullback–Leibler divergence $D_{KL}(p\|q)$.
- Predicting q near 0 or 1 when it's wrong costs without limit, which is why confident mistakes dominate a classifier's loss.
:::

```sim plot
view: graph
x: q = 0.01..0.99
y: H = 0..4 bits
slider: p = 0.05..0.95 step 0.01 = 0.9
plot: -(p*log(q) + (1 - p)*log(1 - q))/log(2) | samples: 400 | label: cross-entropy H(p, q)
plot: -(p*log(p) + (1 - p)*log(1 - p))/log(2) + 0*q | dashed | color: ch3 | label: entropy H(p)
readout: entropy, bits = -(p*log(p) + (1 - p)*log(1 - p))/log(2) | digits: 3
```

::: predict
## A coin lands heads 90% of the time. A model predicts 80%. How many bits per toss does the model waste compared with the true probabilities?

- [ ] None, since 80% is close
- [x] About 0.053 bits
- [ ] About 0.47 bits
- [ ] 0.1 bits

::: reveal
The true entropy is $H(0.9)$ = 0.469 bits per toss; coding with the model's probabilities costs the cross-entropy $-0.9\log_20.8 - 0.1\log_20.2$ = 0.522 bits. The difference, 0.053 bits, is the KL divergence: small, but positive whenever q ≠ p.
:::
:::

::: nameit
## Entropy

$$H(p) = -\sum_kp_k\log p_k$$

the average surprise of outcomes drawn from p: in bits with $\log_2$, in nats with ln. A uniform distribution over K outcomes has $\log K$, the most for K outcomes.

## Cross-entropy and divergence

Using a model q in place of the true p,

$$H(p, q) = -\sum_kp_k\log q_k = H(p) + D_{KL}(p\|q), \qquad D_{KL}(p\|q) = \sum_kp_k\log\frac{p_k}{q_k} \ge 0$$

with equality only when q = p. KL divergence isn't symmetric.

## The classification loss

For a labeled example the "true distribution" puts all its weight on the correct class, so the cross-entropy is just $-\ln q_\text{correct}$; averaged over a data set it's the negative log-likelihood of lesson 2. Minimizing it fits calibrated probabilities.

## Mutual information

$I(X; Y) = H(Y) - H(Y|X)$ measures how much knowing X reduces uncertainty about Y: zero for independent variables. For a binary symmetric channel with uniform input and error rate ε, $I = 1 - H(\epsilon)$ bits.
:::

::: example
## Work out the coin example's numbers in nats, and the loss for one example where a classifier gives the correct class 0.7.

::: solution
1. **KL divergence in nats.** Multiply by ln 2:
   $$\begin{aligned} D_\text{KL} &= 0.0529 \times \ln 2 \\ &= 0.0367\ \text{nats} \end{aligned}$$
2. **Loss for one example.**
   $$\begin{aligned} L &= -\ln 0.7 \\ &= 0.357\ \text{nats} \end{aligned}$$
3. **Other confidences.** Had it given the correct class 0.99, the loss would be 0.010, and at 0.01, 4.6.

**Answer:** the KL divergence is 0.0367 nats, and the loss is 0.357 nats.
:::
:::

::: example
## A ten-class classifier outputs uniform probabilities. What's its cross-entropy loss, and the entropy of a uniform ten-way choice?

::: solution
1. **Loss.** Each correct class gets 0.1, so on every example
   $$\begin{aligned} L &= -\ln 0.1 \\ &= 2.303\ \text{nats} \end{aligned}$$
2. **Entropy.** That's the same as $\ln 10$, the entropy of the uniform distribution, which in bits is
   $$\log_2 10 = 3.32\ \text{bits}$$
3. **Sanity check.** A trained network's loss starting near 2.30 and falling is a common sanity check.

**Answer:** the loss is 2.303 nats on every example, equal to the entropy of a uniform ten-way choice (ln 10 nats, or 3.32 bits).
:::
:::

::: example
## A binary feature is on for 80% of faulty units and 20% of good ones, and half the units are faulty. How much information does it carry about the class?

::: solution
1. **Class entropy.** It's 1 bit.
2. **Entropy left given the feature.** The feature is on half the time; when it's on, the unit is faulty with probability 0.8, and when it's off, 0.2. Either way the remaining entropy is
   $$H(0.8) = 0.722\ \text{bits}$$
3. **Mutual information.**
   $$\begin{aligned} I &= 1 - 0.722 \\ &= 0.278\ \text{bits} \end{aligned}$$
4. **Where it's used.** Mutual information ranks features for selection, and it's exactly the information gain a decision tree uses (lesson 44) when the split is on this feature.

**Answer:** the feature carries 0.278 bits of information about the class.
:::
:::

::: check
given: p = 0.01 | 0.1 | 0.3 | 0.5
answer: -(p*log(p) + (1 - p)*log(1 - p))/log(2)
unit: bits
tolerance: 0.5%

## A binary event has probability {p}. What's its entropy?

::: right
Right. $H(p)$ = {answer} bits.
:::

::: misses
- [-(p*ln(p) + (1 - p)*ln(1 - p))] That's in nats; convert to bits.
- [any] Not yet. $-p\log_2p - (1 - p)\log_2(1 - p)$.
:::
:::

::: check
given: q = 0.05 | 0.3 | 0.7 | 0.95
answer: -ln(q)
unit: nats
tolerance: 0.5%

## A classifier assigns probability {q} to the correct class. What's the cross-entropy loss for this example?

::: right
Right. −ln q = {answer} nats.
:::

::: misses
- [-log(q)] Use the natural logarithm for nats.
- [any] Not yet. Minus the log of the correct class's probability.
:::
:::

::: check
given: p = 0.6 | 0.9; q = 0.5 | 0.75 | 0.95
answer: p*ln(p/q) + (1 - p)*ln((1 - p)/(1 - q))
unit: nats
tolerance: 0.5%

## The true probability is {p}; a model says {q}. What's the KL divergence $D_{KL}(p\|q)$?

::: right
Right. {answer} nats.
:::

::: misses
- [q*ln(q/p) + (1 - q)*ln((1 - q)/(1 - p))] That's $D_{KL}(q\|p)$; the divergence isn't symmetric.
- [any] Not yet. $\sum p\ln(p/q)$.
:::
:::

::: check
given: K = 2 | 10 | 1000
answer: log(K)/log(2)
unit: bits
tolerance: 0.5%

## What's the entropy of a uniform choice among {K} outcomes?

::: right
Right. $\log_2K$ = {answer} bits.
:::

::: misses
- [any] Not yet. Each outcome has probability 1/K.
:::
:::

::: check
given: e = 0.01 | 0.1 | 0.25
answer: 1 + (e*log(e) + (1 - e)*log(1 - e))/log(2)
unit: bits
tolerance: 0.5%

## A binary symmetric channel flips bits with probability {e}. With uniform input, what's the mutual information between input and output?

::: right
Right. $1 - H(\epsilon)$ = {answer} bits.
:::

::: misses
- [1 - e] The loss is the entropy of the errors, not their rate.
- [any] Not yet. $I = H(Y) - H(Y|X)$.
:::
:::

::: check
given: a = 0.9 | 0.6; b = 0.8 | 0.3; c = 0.95 | 0.5
answer: -(ln(a) + ln(b) + ln(c))/3
unit: nats
tolerance: 0.5%

## On three examples a classifier gives the correct class probabilities {a}, {b} and {c}. What's its average cross-entropy loss?

::: right
Right. The mean of −ln q: {answer} nats.
:::

::: misses
- [-ln((a + b + c)/3)] Average the logs, not the probabilities.
- [any] Not yet. Average $-\ln q$.
:::
:::

::: check
kind: parts
given: a = 0 | 1; b = 1.5 | 2 | 3; s = 0.5 | 1; u = 1.5 | 2

## A model approximates a Gaussian p with mean {a} and standard deviation {s} by a Gaussian q with mean {b} and standard deviation {u}.

::: part
answer: ln(u/s) + (s^2 + (a - b)^2)/(2*u^2) - 0.5
unit: nats
tolerance: 0.5%

### What is $D_{KL}(p\|q)$?

::: misses
- [any] $\ln(\sigma_q/\sigma_p) + [\sigma_p^2 + (\mu_p - \mu_q)^2]/(2\sigma_q^2) - \frac12$.
:::
:::

::: part
answer: ln(s/u) + (u^2 + (a - b)^2)/(2*s^2) - 0.5
unit: nats
tolerance: 0.5%

### And $D_{KL}(q\|p)$?

::: misses
- [ln(u/s) + (s^2 + (a - b)^2)/(2*u^2) - 0.5] KL divergence isn't symmetric; swap the roles.
- [any] The same formula with p and q exchanged.
:::
:::
:::

::: check
kind: derivation
vars: e = 0.01..0.45, p = 0.05..0.95

## A binary channel flips each bit with probability e; its input is 1 with probability p.

::: step
answer: p*(1 - e) + (1 - p)*e

### What is the probability that the output is 1?
:::

::: step
answer: -(p*(1 - e) + (1 - p)*e)*log(p*(1 - e) + (1 - p)*e)/log(2) - (1 - (p*(1 - e) + (1 - p)*e))*log(1 - (p*(1 - e) + (1 - p)*e))/log(2) + e*log(e)/log(2) + (1 - e)*log(1 - e)/log(2)

### What is the mutual information between input and output, in bits?
:::

::: right
Right. It's the output's entropy less the channel's own uncertainty, H(e), and peaks at p = 0.5.
:::
:::

::: explain
## Why do classifiers minimize cross-entropy instead of the fraction of examples they get wrong?

::: model
The error rate counts only whether the most probable class was right, so it's flat almost everywhere as a function of the model's parameters and jumps at decision boundaries: it gives no gradient to follow and no reward for becoming more confident on correct answers or less confident on wrong ones. Cross-entropy is smooth and depends on the whole predicted distribution: it falls steadily as the probability of the correct class rises and penalizes confident mistakes heavily. Its minimum is at the true conditional probabilities, so a model trained on it learns calibrated probabilities, from which any decision rule (lesson 3) can then be applied.
:::
:::

::: practice

::: check
given: p = 0.02 | 0.05 | 0.1 | 0.2 | 0.3 | 0.45
answer: -(p*log(p) + (1 - p)*log(1 - p))/log(2)
unit: bits
tolerance: 0.5%

## Binary entropy at p = {p}?

::: misses
- [any] $H(p)$ in bits.
:::
:::

::: check
given: q = 0.001 | 0.01 | 0.1 | 0.5 | 0.9 | 0.999
answer: -ln(q)
unit: nats
tolerance: 0.5%

## Loss for correct-class probability {q}?

::: misses
- [any] $-\ln q$.
:::
:::

::: check
given: p = 0.2 | 0.8; q = 0.1 | 0.9
answer: p*ln(p/q) + (1 - p)*ln((1 - p)/(1 - q))
unit: nats
tolerance: 0.5%

## $D_{KL}(p\|q)$: p = {p}, q = {q}?

::: misses
- [any] $p\ln(p/q) + (1 - p)\ln((1 - p)/(1 - q))$.
:::
:::
:::
