KL Divergence and Cross-Entropy

On this page

Entropy answers “How much information does this distribution carry on average?” Now ask: if the data stay the same but our expectations are wrong, what extra cost do we pay?

Entropy is the source’s own average information. Cross-entropy averages information lengths assigned by another distribution. KL divergence is the difference.

What does using the wrong distribution cost?

Let be the data distribution and the model. The model assigns outcome an ideal information length : outcomes it considers common receive shorter lengths.

The data still occur according to , so the average uses as its weights:

This is cross-entropy. When model and source agree, .

The quantity may be fractional: it is an ideal information length. Actual symbol-by-symbol binary prefix codewords have integer lengths. Suitable long-block coding of an IID source can approach the corresponding cross-entropy per symbol; it is not the exact length of an arbitrary finite code. See the Stanford data compression notes.

Preparing the visual
Same data, different coding expectations

Keep p(A)=0.75 fixed and change only q(A). Bars show ideal information lengths under q; dashed markers show the lengths under p. The average splits into constant source entropy and extra KL cost.

Relative entropy / KL divergence is

, with equality exactly when the distributions agree. This is an average extra cost. An individual outcome can be cheaper under , giving a negative summand. Nonnegative total KL does not mean every term is nonnegative.

If but , the model assigns infinite information length to a possible outcome. Cross-entropy and this direction of KL are then infinite. Terms with contribute zero by the limiting convention.

Why does reversing the direction change the answer?

averages under . Reversing it to changes the weights to and takes the reciprocal inside the logarithm.

Below, and stay fixed. Only the direction changes. Areas above zero are positive, and those below are negative; their signed sum is KL.

Preparing the visual
Reversing KL also changes the averaging weights

Switch the direction for the same fixed pair of distributions. Rectangle widths are averaging weights, and signed heights are log-ratios. Summing signed areas gives approximately 0.74 or 0.53 bits.

The results are approximately and bits. These are two different expectations, not a rounding discrepancy. KL lacks the symmetry required of a metric and does not generally satisfy the triangle inequality.

The support condition also has a direction: mass under where is zero makes diverge. Reverse the direction and the relevant region is where has mass but is zero. For continuous distributions, replace sums by integrals and consider regions carrying nonzero probability mass, rather than an isolated point.

Why is cross-entropy a classification loss?

For a fixed target and a tunable model ,

is constant with respect to model parameters. Minimizing cross-entropy therefore minimizes this direction of KL.

For a labeled observation , a one-hot empirical target usually represents the observed label. This does not imply that the true conditional distribution is deterministic. Its cross-entropy is

Using the product of conditional likelihoods for the given observations,

Minimizing empirical cross-entropy thus maximizes the corresponding conditional likelihood. Population cross-entropy takes an expectation under the true data distribution; the training-set average estimates it empirically.

Implementations commonly use natural logarithms and measure loss in nats. This article uses base-2 logarithms and bits. They differ by a positive constant factor, with the same minimizer. Autoregressive token-level negative log-likelihood uses the same chain factorization; see Information Theory in LLMs.

Forward and reverse KL: one model family, two objectives

Keep fixed and fit . Call forward KL and reverse KL. If the model family can represent , both objectives can reach zero at . Their differences become particularly visible when the family is constrained.

First, compare two typical shapes in a static sketch. With well-separated target modes and a single-Gaussian approximation, forward KL can spread across both sides, while reverse KL can concentrate on one side. These curves illustrate shapes rather than exact numerical fits.

KL(p ‖ q) KL(q ‖ p) x x p q

The dashed gray curve is the target ; each solid colored curve is an approximation . Both panels use the same scales. These are typical shapes for well-separated modes, not a universal outcome for every restricted model.

Here is a concrete constraint: the target is an equally weighted mixture of two unit-variance Gaussians, while the approximation must be a single Gaussian.

Only the separation between component means changes. Nearby components can form a single peak in the mixture. Each panel refits for its own objective, using identical plotting scales.

Preparing the visual
Separate the peaks and watch a constrained model adapt

The target p is an equal mixture of two unit-variance Gaussians; q must be a single Gaussian. Change the separation to refit q under forward and reverse KL, on identical axes.

The forward-KL Gaussian optimum matches the target’s moments: and . It broadens as the components separate. Reverse KL averages under ; when the peaks are far enough apart, approaching one side can reduce the mass placed in the low-density middle.

This is a conditional fitting tendency, not a universal “forward covers, reverse seeks” theorem. Separation, mixture weights, the model family and optimization all matter. The reverse fit here uses multistart numerical optimization. The symmetric left-side solution is equally good; displaying the right branch does not privilege it. See Bishop’s PRML, Section 10.1.

Variational inference often minimizes KL from an approximate posterior to the true posterior. In RLHF, commonly regularizes policy drift from a reference, alongside rewards and other objectives. It is not simply the two-peak fitting problem shown here.

Jensen–Shannon divergence: a common reference

Set and define

The mixture is nonzero wherever either distribution has mass. JS therefore avoids the infinity of directly comparing distributions with disjoint supports using KL. In base 2, bit. It is symmetric, but the metric is , not JS itself.

For example, and give infinite KL in both directions. Their mixture is ; each KL to the mixture is bit, so JS is bit.

The original GAN connection has an important condition. With an optimal discriminator for a fixed generator, substituting it into the original minimax objective gives the generator value . This JS uses natural logarithms; the discriminator itself is not minimizing JS. The commonly used non-saturating generator loss is a different expression. See Goodfellow et al., Sections 3 and 4.1.

Comparison and references

QuantityAveraged underInterpretation
The source’s average self-information
Average ideal information length assigned by
Average extra cost relative to
A different expectation after exchanging source and reference
Both sides, equallyAverage KL to their common mixture