Entropy and KL Divergence
Information is surprise: an outcome of probability carries bits. Entropy is the average surprise, cross-entropy is the average surprise when you expect the wrong probabilities, and KL divergence is the extra cost of being wrong, which is never negative and isn't the same in both directions.
What the symbols mean
- Information
How surprising outcome x is, in bits.
- Probability
How likely x was before it happened.
What the symbols mean
- Entropy
The average surprise of the distribution p, in bits.
- True probability
How often outcome i really happens.
What the symbols mean
- Cross-entropy
The average surprise when outcomes come from p but you expect q.
- Model probability
What the model (or the code) believes about outcome i.
What the symbols mean
- KL divergence
The extra bits per outcome you pay for believing q when the truth is p.
Explanation
Entropy is simply a measure of how unpredictable something is: the average number of yes/no questions you'd need to pin down the outcome. It starts from one idea, that information is surprise. Learning that something likely happened tells you almost nothing; learning that something rare happened tells you a lot. The formula makes that exact: a fair coin landing heads () is 1 bit, one particular card out of 8 () is 3 bits, and something certain () is 0 bits. Entropy is the average of that surprise, cross-entropy is the average surprise when you expect the wrong probabilities, and KL divergence is the difference between them. (The name was borrowed from physics, where entropy also measures disorder.) Four formulas, but they're all the same move: average of a probability.
Why a logarithm? Two independent events should carry the information of both together. If I tell you a coin came up heads and a die came up 4, you've learned 1 bit plus bits. Probabilities of independent events multiply, , and the logarithm is the function that turns multiplying into adding. Base 2 makes the unit a bit, one yes/no question, the same bits that error-detecting codes count. With 8 equally likely outcomes, three yes/no questions ("is it in the first half?") always find the answer, and .
Entropy is the average surprise. Weight each outcome's surprise by how often it happens:
For a coin with heads probability , that's . A fair coin gives exactly 1 bit. A coin with gives only bits, because you can mostly guess the outcome in advance, and a coin that always lands heads gives 0. Among outcomes, the most unpredictable distribution is the uniform one, with . (An outcome with contributes nothing, since as .) Shannon proved that entropy is also the fewest bits per symbol any code can use on average: it's the limit of compression.
Cross-entropy: surprise under the wrong beliefs. Suppose outcomes really come from , but you expect (you built your code, or your model, around ). Outcome then surprises you by , and it happens with frequency , so on average
When the true label is a single class (so puts all its weight there), this is just , the classification loss from Softmax and Logits. Machine learning uses instead of , which measures in nats instead of bits. The two differ only by the factor , so nothing about the minimum changes.
KL divergence: the extra cost. The penalty for believing when the truth is is the cross-entropy minus the best you could possibly do:
Since is fixed by the data, minimizing cross-entropy over a model's is the same as minimizing KL. That ties this lesson to the last one: maximum likelihood, cross-entropy and KL all point the model at the same place.
Why KL is never negative. The key fact is for every , with equality only at (the line is tangent to at , and bends down below it). Apply it with , using natural logs:
(The sums run over the outcomes with , so there is at most 1.) So , with equality only when . You can never beat the truth on average.
A common mistake. Because KL is never negative and is 0 only when , it's tempting to call it a distance between distributions. It isn't one, because it's not symmetric. weights the log-ratios by , and weights them by , so the two usually differ (the second worked example below gets bits one way and the other). The direction matters most when a model gives probability 0 to something that actually happens: then and the KL blows up. That's why classifiers never output exactly 0, and why people sometimes soften their training labels a little ("label smoothing").
See it
Worked examples
A weather station reports one of three conditions: sun with probability , cloud with and rain with . Find the entropy of its reports in bits.
- 1
Entropy is the average surprise, so first find the surprise of each outcome, then average them, weighted by how often each one happens.
A coin is fair, , but a model believes . Compute and in bits.
- 1
Start with . The weights in front are the true probabilities .
Practice
Type your answer and check it. Solve them all to complete the lesson.
An event has probability . How many bits of information does seeing it carry?
A sensor reports "normal" 90% of the time and "alarm" 10% of the time. Find the entropy of its output in bits, to 4 decimal places.
A system generates passwords of 4 lowercase letters, each chosen uniformly and independently from the 26 letters. How many bits of entropy does a password have? Give 2 decimal places.
A classifier outputs , and the true class is the second one. Find the cross-entropy loss in nats (natural log), to 4 decimal places.
Three outcomes truly occur with , but a model predicts the uniform . Find in bits, to 4 decimal places.
Which statement about KL divergence is true?
The entropy of a coin is . Find the slope , in bits per unit of .
A factory sensor sends one of four status codes: OK with probability , WARN with , and HOT and JAM with each. A fixed-length code uses 2 bits per message. How many bits per message, on average, does the best possible code use?
The hardest one here. Get it, and you've got this lesson.
A dataset's labels are distributed over three classes, and a lazy model always predicts . Find and in bits, to 4 decimal places.
Where it's used
Cross-entropy in nats is the training loss of essentially every classifier and language model. KL divergence shows up wherever two distributions have to stay close: variational autoencoders add a KL term that pulls their internal codes toward a standard normal, and when a chat model is fine-tuned with human feedback, a KL penalty keeps it from drifting too far from the original model. Entropy itself sets the limit for compression formats, and decision trees choose their splits by how much they reduce it.