Entropy, cross-entropy and KL
From surprise, −log P, to entropy, cross-entropy and the KL divergence — and why minimizing cross-entropy is maximum likelihood.
Surprise
The topic What generative modeling is left us one quantity computable from samples alone, the average log-likelihood, and Multivariate Gaussians taught us to sample noise and compute a log-density. To fit a model distribution to a target distribution we still need a number that says how far is from . The standard first choice is the Kullback–Leibler divergence. It is built from a simpler idea: how surprised we are by an outcome.
Take two boards with ten marks each. Board A has 5 noughts and 5 crosses; board B has 9 noughts and 1 cross. Drawing an X is clearly more surprising on board B, where , than on board A, where . So surprise must grow as the probability falls.
A first guess, , ranks the boards correctly but fails on a certain event: a coin that always lands heads gives , although heads should not surprise us at all. Taking the logarithm fixes this. The surprise, or self-information, of an outcome is
It equals when and grows without bound as .
The logarithm also makes surprise additive. For independent events the probability of a sequence is a product, and the logarithm of a product is a sum, so the surprise of a sequence is the sum of the surprises of its parts.
Units. gives bits and gives nats, with bit nats. One fair binary choice carries exactly bit. The base never changes a comparison, only the unit; a plain means either, as long as one base is used throughout.
Entropy: expected surprise
Flip a coin with and many times. Heads carries bits of surprise, tails bits, and each happens in proportion to its probability. On average one flip brings
This average is the entropy (Shannon, 1948), the expected surprise of :
Outcomes with never occur, so their terms are left out; equivalently, , the limit of as .
Board B has exactly this coin's distribution, so bits, while the balanced board A has bit. Higher entropy means more uncertainty. On outcomes no distribution has more than , and only the uniform one reaches it; the next block shows why.
The catch: to compute we must know the real distribution . When we fit a generative model to data, we never do.
Cross-entropy: surprise under a wrong belief
Suppose our model believes a coin is fair, , while in reality it is rigged, . Ten heads in a row have probability under the belief, about nats of surprise, but in reality, about nats. The outcome was ordinary; the surprise came from believing in the wrong model.
Cross-entropy is the average surprise when outcomes come from but we believe in :
The first argument always generates the outcomes; the second scores them. If , cross-entropy is just entropy, . Otherwise a wrong belief can only add surprise — this is Gibbs' inequality:
With a uniform , every outcome costs , so for any — and Gibbs' inequality gives the bound from the previous block.
Cross-entropy is not symmetric. For one flip of the two coins, in nats,
The KL divergence
Cross-entropy mixes two sources of surprise: the surprise the real distribution produces anyway, , and the extra surprise caused by the wrong model. Subtracting the first isolates the second:
The KL divergence (Kullback and Leibler, 1951) is the extra surprise from using as the model instead of . For the two coins above, in nats, .
The sum on the right is the general definition. A term with contributes . A term with but is , and then so is the whole divergence: KL is finite only when gives positive probability to every outcome to which does.
Non-negative, but not a distance
KL is non-negative. If at some with , the divergence is and there is nothing to prove. Otherwise sum over the outcomes with and apply Jensen's inequality to the concave logarithm, with weights :
The first inequality is tight only if is the same constant for all such , the second only if puts no mass elsewhere; together they force . So with equality only when , and adding to both sides gives Gibbs' inequality.
But KL is not a distance. It is not symmetric: for the two coins nats, against the other way. It also breaks the triangle inequality. Reading O as heads and X as tails, board A compared directly with the rigged coin gives about bits, but going through board B gives only bits. That is why it is called a divergence.
Densities. For continuous distributions with densities and the sums become integrals. A density can exceed , so can be negative, and so can the differential entropy . For , , and averaging with gives ; for it is nats. The KL keeps its meaning: by the same Jensen argument with integrals in place of sums, it is still non-negative and zero only when almost everywhere.
Why we minimize cross-entropy
For the true data distribution and a model , the forward KL splits as
Changing the model cannot change the entropy of the data, and we do not need its value — for images, estimating is itself a hard problem. So minimizing the KL and minimizing the cross-entropy have the same solution, and the cross-entropy is the part we can estimate from samples :
Minimizing the average negative log-likelihood is maximum-likelihood estimation. For discrete data it is exactly the cross-entropy with the empirical distribution (mass on each ): is the share of samples equal to , so adds once for each sample. As grows the average converges to by the law of large numbers. "Minimize ", "minimize " and "maximize likelihood" are three names for one training objective. Classifiers and language models apply the same argument to the conditional distribution of the label or the next token.
Later model families differ mostly in how they cope with being hard to compute: VAEs bound it, normalizing flows compute it exactly, GANs avoid it, and diffusion models bound it again.
The Jensen–Shannon divergence
The Jensen–Shannon divergence (Lin, 1991) uses the same building block but is symmetric and bounded. It compares both distributions with their mixture :
Since wherever or , both terms are always finite. And since , every logarithm is at most , which gives
with the maximum reached exactly when and do not overlap. For the two coins, nats.
JS returns in the topic GANs as a two-player game: with an optimal discriminator the original GAN objective equals , where is the data distribution and the generator's (Goodfellow et al., 2014). The ceiling matters there: what JS does for distributions that do not overlap at all turns out to decide whether GAN training makes progress.
We now know what to minimize: the average negative log-likelihood, which is the forward KL from to . But KL is asymmetric, and its two directions pull a model in different ways. What does each one give when a simple model is fitted to a target with two modes? The next topic, Fitting distributions with KL, runs that fit.
Check your understanding
The same questions as in Pulse. Answers are shared between modes.
Answers are saved on the server and tied to this browser until you sign in.