S144 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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 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 DKL(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.

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?

Name it

Entropy

H(p)=−∑kpklog⁡pk

the average surprise of outcomes drawn from p: in bits with log2⁡, 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)=−∑kpklog⁡qk=H(p)+DKL(p‖q),DKL(p‖q)=∑kpklog⁡pkqk≥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⁡qcorrect; 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(ϵ) 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
  1. KL divergence in nats. Multiply by ln 2: DKL=0.0529×ln⁡2=0.0367nats
  2. Loss for one example. L=−ln⁡0.7=0.357nats
  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.

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
  1. Loss. Each correct class gets 0.1, so on every example L=−ln⁡0.1=2.303nats
  2. Entropy. That's the same as ln⁡10, the entropy of the uniform distribution, which in bits is log2⁡10=3.32bits
  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).

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
  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.722bits
  3. Mutual information. I=1−0.722=0.278bits
  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

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 DKL(p‖q)?

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 DKL(p‖q)?

(b)

And DKL(q‖p)?

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°.

Explain why

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

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.