Entropy, Cross-Entropy and KL: Why Your Classification Loss Is a Divergence
Published:
Entropy
Define the surprise of an outcome as $-\log p(x)$: certain outcomes carry none, rare ones carry a lot, and surprises of independent events add because logs turn products into sums. Entropy is the expected surprise:
With $\log_2$ the units are bits. A fair coin has $H = 1$ bit; a coin with $p=0.9$ has $H = -(0.9\log_2 0.9 + 0.1\log_2 0.1) = 0.469$ bits, because most of the time you already knew the answer. Entropy is maximised by the uniform distribution and is zero for a point mass. Shannon’s source coding theorem makes this operational: $H(p)$ is the minimum expected code length, in bits per symbol, for data drawn from $p$.
Cross-entropy and KL
Suppose you build an optimal code for $q$ but the data actually come from $p$. Your expected code length is the cross-entropy
The KL divergence is the excess — the bits you waste for using the wrong code. By Jensen’s inequality it is non-negative, and it is zero exactly when $p=q$.
This decomposition explains the classification loss. Take $p$ to be the one-hot label distribution and $q$ the model’s softmax output. Then $H(p) = 0$ — the label is certain — so cross-entropy equals the KL divergence, and it reduces to \(-\log q(y_{\text{true}})\): the negative log-likelihood of the correct class. Training a classifier by cross-entropy is maximum likelihood with a categorical noise model, and it is simultaneously KL minimisation. The three descriptions are one computation.
More generally, maximum likelihood over a dataset of $n$ points is
since the two differ only by \(H(p_{\text{data}})\), a constant in $\theta$. Fitting by likelihood is fitting by forward KL against the empirical distribution.
KL is not a metric
Two failures. It is not symmetric, and it does not satisfy the triangle inequality. Take $p = (0.5, 0.5)$ and $q = (0.9, 0.1)$ on two outcomes:
Different numbers for the same pair. The asymmetry has a clear source: \(D_{\mathrm{KL}}(p\|q)\) averages $\log(p/q)$ under $p$, so it is enormous — infinite, in fact — wherever $p$ puts mass and $q$ puts none, and completely indifferent to regions where $q$ has mass but $p$ does not. Call \(D_{\mathrm{KL}}(p\|q)\) a “distance” in an interview and expect to be asked which direction you meant.
Mutual information
Mutual information is the KL between a joint distribution and the product of its marginals:
It measures how many bits knowing $Y$ saves you when describing $X$. It is symmetric, non-negative, and zero exactly under independence — so, unlike correlation, it detects any dependence, including the parabola counterexample from expectation and variance. The cost is that estimating it from samples in high dimensions is hard, which is why the ML literature is full of variational lower bounds on $I$ rather than direct estimates.
Forward versus reverse KL
Fit a simple \(q_\theta\) to a complicated $p$. The direction you choose changes the answer.
Forward, \(D_{\mathrm{KL}}(p\|q)\) — the expectation is over $p$, so any region where $p>0$ but $q\approx0$ contributes a huge $\log(p/q)$. The fit is zero-avoiding: it spreads $q$ to cover every mode, including the empty space between them. This is the maximum-likelihood direction.
Reverse, \(D_{\mathrm{KL}}(q\|p)\) — the expectation is over $q$, so regions where $q\approx0$ cost nothing regardless of $p$. The penalty falls on placing $q$ mass where $p$ has none. The fit is zero-forcing and mode-seeking: it collapses onto one mode and ignores the rest. This is the direction used by variational inference, because the ELBO is derived from \(D_{\mathrm{KL}}(q\|p)\) — and it is why variational posteriors are famously over-confident and under-dispersed.
Where this goes next
The Gaussian’s maximum-entropy property, and the families whose entropies have closed forms, are in common distributions. Estimation and model selection built on likelihood — AIC, likelihood ratios — appear in statistics basics.
Recap
- \(H(p)\) is expected surprise and the optimal expected code length; a \(p=0.9\) coin carries \(0.469\) bits.
- \(H(p,q) = H(p) + D_{\mathrm{KL}}(p\|q)\); with one-hot labels \(H(p)=0\), so cross-entropy is exactly the KL and exactly the negative log-likelihood.
- Maximum likelihood minimises forward KL against the empirical distribution.
- KL is non-negative and zero only at equality, but asymmetric and not a metric: for \(p=(0.5,0.5)\), \(q=(0.9,0.1)\) the two directions give \(0.511\) and \(0.368\) nats.
- Forward KL is mode-covering, reverse KL is mode-seeking; variational inference uses the reverse and inherits its over-confidence.
References
- Shannon, C. E. A Mathematical Theory of Communication. Bell System Technical Journal 27(3), 379–423, 1948.
- Cover, T. M. & Thomas, J. A. Elements of Information Theory, 2nd ed. Wiley, 2006.
- Minka, T. Divergence measures and message passing. Microsoft Research Technical Report MSR-TR-2005-173, 2005.
- Blei, D. M., Kucukelbir, A. & McAuliffe, J. D. Variational Inference: A Review for Statisticians. JASA 112(518), 859–877, 2017.
- Arjovsky, M., Chintala, S. & Bottou, L. Wasserstein GAN. ICML 2017.
