S14 · Lesson 4 · Information theory for learning
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
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.
- For a binary outcome with true probability p, the cross-entropy of a model that predicts q, against q, and the entropy (dashed), both in bits.
- Cross-entropy is smallest, equal to the entropy, when q = p. The gap above it is the Kullback–Leibler divergence .
- Predicting q near 0 or 1 when it's wrong costs without limit, which is why confident mistakes dominate a classifier's loss.
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?
The true entropy is = 0.469 bits per toss; coding with the model's probabilities costs the cross-entropy = 0.522 bits. The difference, 0.053 bits, is the KL divergence: small, but positive whenever q ≠ p.
Name it
Entropy
the average surprise of outcomes drawn from p: in bits with , in nats with ln. A uniform distribution over K outcomes has , the most for K outcomes.
Cross-entropy and divergence
Using a model q in place of the true p,
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 ; averaged over a data set it's the negative log-likelihood of lesson 2. Minimizing it fits calibrated probabilities.
Mutual information
measures how much knowing X reduces uncertainty about Y: zero for independent variables. For a binary symmetric channel with uniform input and error rate ε, bits.
Worked 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.
Show the solution
- KL divergence in nats. Multiply by ln 2:
- Loss for one example.
- 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.
Worked example
A ten-class classifier outputs uniform probabilities. What's its cross-entropy loss, and the entropy of a uniform ten-way choice?
Show the solution
- Loss. Each correct class gets 0.1, so on every example
- Entropy. That's the same as , the entropy of the uniform distribution, which in bits is
- 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).
Worked 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?
Show the solution
- Class entropy. It's 1 bit.
- 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
- Mutual information.
- 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
A binary event has probability 0.1. What's its entropy?
Check
A classifier assigns probability 0.3 to the correct class. What's the cross-entropy loss for this example?
Check
The true probability is 0.6; a model says 0.5. What's the KL divergence ?
Check
What's the entropy of a uniform choice among 10 outcomes?
Check
A binary symmetric channel flips bits with probability 0.1. With uniform input, what's the mutual information between input and output?
Check
On three examples a classifier gives the correct class probabilities 0.9, 0.8 and 0.5. What's its average cross-entropy loss?
Check
A model approximates a Gaussian p with mean 0 and standard deviation 1 by a Gaussian q with mean 1.5 and standard deviation 2.
(a)
What is ?
(b)
And ?
Check
A binary channel flips each bit with probability e; its input is 1 with probability p.
Step 1
What is the probability that the output is 1?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
What is the mutual information between input and output, in bits?
Explain why
Why do classifiers minimize cross-entropy instead of the fraction of examples they get wrong?
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
Keep practicing
10 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.