Cross-entropy and KL divergence
Derive H(p,q) = H(p) + KL(p‖q). Explain why minimizing cross-entropy against data is the same as minimizing KL, and why KL is asymmetric.
Builds on: Surprisal and entropy, What training does
What the animation says
The loss has a floor
Training loss is the model's average surprise at the real next token. A token the model rated likely costs little. A long shot costs a lot. Take the simplest text there is: a fair coin, heads or tails, half and half. Even a perfect model, saying fifty-fifty, pays about zero point six nine on every flip. That is the coin's own unpredictability. No model gets under it. Now a model bets eighty percent heads on this same fair coin. What happens to its average loss?
Paying with the wrong odds
It goes above. Here's the bill. Half the flips are heads. The model gave heads point eight, so each costs only point two two. The other half are tails, which the model rated just point two. Each of those costs one point six one. Average them and you get point nine two. That's the cross-entropy: reality, scored with the model's odds. Split it. Point six nine is the coin's entropy, the floor. The extra point two two is the KL divergence, the model's mismatch.
Floor plus mismatch
That split isn't a quirk of the coin. It always works. Write the cross-entropy as a sum. Then add and subtract log p inside it. It falls apart into two pieces: the entropy of the data, plus the KL divergence. KL is never negative, and it's zero only when the model matches the truth exactly. The entropy belongs to the text, so training can't touch it. The expected loss can never drop below it. Here the entropy is two point four, and the loss drops from three point one to two point seven. Tap the part that shrank.
KL has a direction
KL compares two distributions, but it isn't symmetric. On our coin, averaging over the model instead of the truth gives point one nine, not point two two. Now meet a model that nearly rules out tails. It says ninety-nine point nine percent heads. But tails really happen half the time. Which direction of KL blows up?
What training punishes
Truth to model comes to two point seven six. The other way is just point six nine. Averaged over real flips, every tail costs the log of point five over point zero zero one. That's about six nats each. Averaged over the model, tails get weight point zero zero one, so they barely count. Training averages over real text, so it minimizes KL from data to model. Calling real text nearly impossible is the costliest mistake. So the model learns to cover everything the data does.
Scoring reality with the wrong odds
Deriving H(p,q) = H(p) + KL(p‖q)
Two outcomes, actual numbers
Why KL is asymmetric
import numpy as np
H = lambda a: -(a*np.log(a)).sum()
CE = lambda a, b: -(a*np.log(b)).sum()
KL = lambda a, b: (a*np.log(a/b)).sum()
p = np.array([0.5, 0.5]); q = np.array([0.8, 0.2])
print(H(p), CE(p, q), KL(p, q)) # 0.693 0.916 0.223
print(KL(q, p)) # 0.193, not 0.223
q2 = np.array([0.999, 0.001]) # model nearly rules out a real outcome
print(KL(p, q2), KL(q2, p)) # 2.76 vs 0.69
Remember
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.
Bytewise
All courses