Skip to content
BIASED

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 QQ to a target distribution PP we still need a number that says how far QQ is from PP. 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 P(X)=110P(\text{X}) = \tfrac1{10}, than on board A, where P(X)=12P(\text{X}) = \tfrac12. So surprise must grow as the probability falls.

A first guess, 1/P(x)1/P(x), ranks the boards correctly but fails on a certain event: a coin that always lands heads gives 1/P(H)=11/P(H) = 1, although heads should not surprise us at all. Taking the logarithm fixes this. The surprise, or self-information, of an outcome is

s(x)=log⁡1P(x)=−log⁡P(x).s(x) = \log\frac{1}{P(x)} = -\log P(x).

It equals 00 when P(x)=1P(x) = 1 and grows without bound as P(x)→0P(x) \to 0.

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. log⁡2\log_2 gives bits and ln⁡\ln gives nats, with 11 bit =ln⁡2≈0.693= \ln 2 \approx 0.693 nats. One fair binary choice carries exactly 11 bit. The base never changes a comparison, only the unit; a plain log⁡\log means either, as long as one base is used throughout.

Entropy: expected surprise

Flip a coin with P(H)=0.9P(H) = 0.9 and P(T)=0.1P(T) = 0.1 many times. Heads carries log⁡210.9≈0.152\log_2\frac{1}{0.9} \approx 0.152 bits of surprise, tails log⁡210≈3.322\log_2 10 \approx 3.322 bits, and each happens in proportion to its probability. On average one flip brings

0.9⋅0.152+0.1⋅3.322≈0.469 bits.0.9 \cdot 0.152 + 0.1 \cdot 3.322 \approx 0.469 \text{ bits}.

This average is the entropy (Shannon, 1948), the expected surprise of X∼PX \sim P:

H(P)=∑xP(x)log⁡1P(x)=−∑xP(x)log⁡P(x).H(P) = \sum_x P(x)\log\frac{1}{P(x)} = -\sum_x P(x)\log P(x).

Outcomes with P(x)=0P(x) = 0 never occur, so their terms are left out; equivalently, 0⋅log⁡10=00 \cdot \log\frac10 = 0, the limit of plog⁡1pp\log\frac1p as p→0p \to 0.

Board B has exactly this coin's distribution, so HB≈0.469H_B \approx 0.469 bits, while the balanced board A has HA=1H_A = 1 bit. Higher entropy means more uncertainty. On ∣X∣|\mathcal X| outcomes no distribution has more than log⁡∣X∣\log|\mathcal X|, and only the uniform one reaches it; the next block shows why.

The catch: to compute H(P)H(P) we must know the real distribution PP. 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, Q=(0.5,0.5)Q = (0.5, 0.5), while in reality it is rigged, P=(0.99,0.01)P = (0.99, 0.01). Ten heads in a row have probability 0.510≈0.0010.5^{10} \approx 0.001 under the belief, about 6.936.93 nats of surprise, but 0.9910≈0.900.99^{10} \approx 0.90 in reality, about 0.100.10 nats. The outcome was ordinary; the surprise came from believing in the wrong model.

Cross-entropy is the average surprise when outcomes come from PP but we believe in QQ:

H(P,Q)=∑xP(x)⏟how often x occurs  log⁡1Q(x)⏟how surprised we are.H(P, Q) = \sum_{x} \underbrace{P(x)}_{\text{how often } x \text{ occurs}}\;\underbrace{\log\frac{1}{Q(x)}}_{\text{how surprised we are}} .

The first argument always generates the outcomes; the second scores them. If Q=PQ = P, cross-entropy is just entropy, H(P,P)=H(P)H(P, P) = H(P). Otherwise a wrong belief can only add surprise — this is Gibbs' inequality:

H(P,Q)≥H(P),with equality only when Q=P.H(P, Q) \ge H(P), \qquad \text{with equality only when } Q = P.

With a uniform QQ, every outcome costs log⁡∣X∣\log|\mathcal X|, so H(P,Q)=log⁡∣X∣H(P, Q) = \log|\mathcal X| for any PP — and Gibbs' inequality gives the bound H(P)≤log⁡∣X∣H(P) \le \log|\mathcal X| from the previous block.

Cross-entropy is not symmetric. For one flip of the two coins, in nats,

H(P,Q)=0.99ln⁡2+0.01ln⁡2≈0.69,H(Q,P)=0.5ln⁡10.99+0.5ln⁡10.01≈2.31.H(P, Q) = 0.99\ln 2 + 0.01\ln 2 \approx 0.69, \qquad H(Q, P) = 0.5\ln\frac{1}{0.99} + 0.5\ln\frac{1}{0.01} \approx 2.31 .

The KL divergence

Cross-entropy mixes two sources of surprise: the surprise the real distribution produces anyway, H(P)H(P), and the extra surprise caused by the wrong model. Subtracting the first isolates the second:

H(P,Q)−H(P)=∑xP(x)log⁡1Q(x)−∑xP(x)log⁡1P(x)=∑x∈XP(x)log⁡P(x)Q(x)=DKL(P ∥ Q).H(P, Q) - H(P) = \sum_x P(x)\log\frac{1}{Q(x)} - \sum_x P(x)\log\frac{1}{P(x)} = \sum_{x \in \mathcal X} P(x)\log\frac{P(x)}{Q(x)} = D_{\mathrm{KL}}(P \,\|\, Q).

The KL divergence (Kullback and Leibler, 1951) is the extra surprise from using QQ as the model instead of PP. For the two coins above, in nats, DKL(P ∥ Q)≈0.6931−0.0560=0.6371D_{\mathrm{KL}}(P \,\|\, Q) \approx 0.6931 - 0.0560 = 0.6371.

The sum on the right is the general definition. A term with P(x)=0P(x) = 0 contributes 00. A term with P(x)>0P(x) > 0 but Q(x)=0Q(x) = 0 is +∞+\infty, and then so is the whole divergence: KL is finite only when QQ gives positive probability to every outcome to which PP does.

Non-negative, but not a distance

KL is non-negative. If Q(x)=0Q(x) = 0 at some xx with P(x)>0P(x) > 0, the divergence is +∞+\infty and there is nothing to prove. Otherwise sum over the outcomes with P(x)>0P(x) > 0 and apply Jensen's inequality to the concave logarithm, with weights P(x)P(x):

−DKL(P ∥ Q)=∑P(x)>0P(x)log⁡Q(x)P(x)≤log⁡∑P(x)>0Q(x)≤log⁡1=0.-D_{\mathrm{KL}}(P \,\|\, Q) = \sum_{P(x) > 0} P(x)\log\frac{Q(x)}{P(x)} \le \log \sum_{P(x) > 0} Q(x) \le \log 1 = 0 .

The first inequality is tight only if Q(x)/P(x)Q(x)/P(x) is the same constant for all such xx, the second only if QQ puts no mass elsewhere; together they force Q=PQ = P. So DKL(P ∥ Q)≥0D_{\mathrm{KL}}(P \,\|\, Q) \ge 0 with equality only when Q=PQ = P, and adding H(P)H(P) to both sides gives Gibbs' inequality.

But KL is not a distance. It is not symmetric: for the two coins DKL(Q ∥ P)≈2.3076−0.6931=1.6145D_{\mathrm{KL}}(Q \,\|\, P) \approx 2.3076 - 0.6931 = 1.6145 nats, against 0.63710.6371 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 2.3292.329 bits, but going through board B gives only 0.737+0.208=0.9450.737 + 0.208 = 0.945 bits. That is why it is called a divergence.

Densities. For continuous distributions with densities pp and qq the sums become integrals. A density can exceed 11, so −log⁡p(x)-\log p(x) can be negative, and so can the differential entropy H(p)=−∫plog⁡p dxH(p) = -\int p\log p\,dx. For N(0,σ2)\mathcal N(0, \sigma^2), −ln⁡p(x)=12ln⁡(2πσ2)+x2/(2σ2)-\ln p(x) = \tfrac12\ln(2\pi\sigma^2) + x^2/(2\sigma^2), and averaging with E[x2]=σ2\mathbb E[x^2] = \sigma^2 gives H=12ln⁡(2πe σ2)H = \tfrac12\ln(2\pi e\,\sigma^2); for σ=0.1\sigma = 0.1 it is 12ln⁡(2πe⋅0.01)≈−0.88\tfrac12\ln(2\pi e \cdot 0.01) \approx -0.88 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 p=qp = q almost everywhere.

Why we minimize cross-entropy

For the true data distribution PP and a model QθQ_\theta, the forward KL splits as

DKL(P ∥ Qθ)⏟minimize w.r.t. θ=H(P,Qθ)⏟minimize w.r.t. θ−H(P)⏟constant.\underbrace{D_{\mathrm{KL}}(P \,\|\, Q_\theta)}_{\text{minimize w.r.t. } \theta} = \underbrace{H(P, Q_\theta)}_{\text{minimize w.r.t. } \theta} - \underbrace{H(P)}_{\text{constant}} .

Changing the model cannot change the entropy of the data, and we do not need its value — for images, estimating H(P)H(P) 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 x1,…,xN∼Px_1, \dots, x_N \sim P:

H(P,Qθ)=Ex∼P[−log⁡Qθ(x)]≈−1N∑i=1Nlog⁡Qθ(xi).H(P, Q_\theta) = \mathbb E_{x \sim P}\big[-\log Q_\theta(x)\big] \approx -\frac1N \sum_{i=1}^N \log Q_\theta(x_i).

Minimizing the average negative log-likelihood is maximum-likelihood estimation. For discrete data it is exactly the cross-entropy H(P^,Qθ)H(\hat P, Q_\theta) with the empirical distribution P^\hat P (mass 1/N1/N on each xix_i): P^(x)\hat P(x) is the share of samples equal to xx, so ∑xP^(x)(−log⁡Qθ(x))\sum_x \hat P(x)\big(-\log Q_\theta(x)\big) adds −log⁡Qθ(xi)/N-\log Q_\theta(x_i)/N once for each sample. As NN grows the average converges to H(P,Qθ)H(P, Q_\theta) by the law of large numbers. "Minimize DKL(P ∥ Qθ)D_{\mathrm{KL}}(P \,\|\, Q_\theta)", "minimize H(P,Qθ)H(P, Q_\theta)" 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 log⁡Qθ(x)\log Q_\theta(x) 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 M=12(P+Q)M = \tfrac12(P + Q):

DJS(P ∥ Q)=12DKL(P ∥ M)+12DKL(Q ∥ M).D_{\mathrm{JS}}(P \,\|\, Q) = \tfrac12 D_{\mathrm{KL}}(P \,\|\, M) + \tfrac12 D_{\mathrm{KL}}(Q \,\|\, M).

Since M(x)>0M(x) > 0 wherever P(x)>0P(x) > 0 or Q(x)>0Q(x) > 0, both terms are always finite. And since P(x)≤2M(x)P(x) \le 2M(x), every logarithm log⁡P(x)M(x)\log\frac{P(x)}{M(x)} is at most log⁡2\log 2, which gives

0≤DJS(P ∥ Q)≤ln⁡2 nats=1 bit,0 \le D_{\mathrm{JS}}(P \,\|\, Q) \le \ln 2 \text{ nats} = 1 \text{ bit},

with the maximum reached exactly when PP and QQ do not overlap. For the two coins, DJS≈0.193D_{\mathrm{JS}} \approx 0.193 nats.

JS returns in the topic GANs as a two-player game: with an optimal discriminator the original GAN objective equals 2 DJS(P ∥ Q)−2ln⁡22\,D_{\mathrm{JS}}(P \,\|\, Q) - 2\ln 2, where PP is the data distribution and QQ 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 PP to QθQ_\theta. 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.

Full notes

Answers are saved on the server and tied to this browser until you sign in.