Contents
05KL divergence: the extra surprise
5.1Definition and the key identity
The newcomer’s 4.62 bits minus the locals’ 0.99 leaves 3.63 bits a day: the price of wrong expectations.
Definition 3 (Kullback–Leibler divergence).
The second form follows from \log\frac{p}{q}=\log\frac1q-\log\frac1p. Rearranged, it is the identity at the heart of this tutorial:
For the newcomer, 4.62=0.99+3.63. KL is also called the relative entropy or the information gain (Murphy, 2022, §6.2).
5.2KL is never negative
Theorem 1 (Information inequality). D_{\mathrm{KL}}(p\,\|\,q)\ge0, with equality if and only if q=p.
The logarithm is concave, so Jensen’s inequality gives \E[\log Z]\le\log\E[Z]. Apply it with Z=q(x)/p(x) and x\sim p:
Since \log is strictly concave, equality in Jensen’s inequality needs q(x)/p(x) to be constant, and the sums then force q=p (Murphy, 2022, Theorem 6.2.1), (Cover and Thomas, 2006, §2.6).
Two consequences. First, by (5), H_{\mathrm{ce}}(p,q)\ge H(p): on average nobody beats the locals, and the entropy of the data is a floor no model can go below. Second, with u the uniform distribution on K outcomes, D_{\mathrm{KL}}(p\,\|\,u)=\log_2K-H(p)\ge0, which proves H(p)\le\log_2K.
5.3KL is not a distance
A distance must be symmetric and satisfy the triangle inequality. KL does neither, which is why it is called a divergence.
- Not symmetric. The Cupertino native in Miami pays D_{\mathrm{KL}}(p_{\text{Miami}}\,\|\,p_{\text{Cupertino}})=3.63 extra bits a day; the Miami native in Cupertino pays D_{\mathrm{KL}}(p_{\text{Cupertino}}\,\|\,p_{\text{Miami}})=1.15. Expecting dry weather where it rains is costlier than expecting rain where it is dry, because the rare outcome is weighted by how often it really happens.
- No triangle inequality. For coins with heads probabilities 0.1, 0.5 and 0.9: D_{\mathrm{KL}}(0.1\,\|\,0.9)=2.54 bits, but going through the fair coin, D_{\mathrm{KL}}(0.1\,\|\,0.5)+D_{\mathrm{KL}}(0.5\,\|\,0.9)=0.53+0.74=1.27, which is smaller.
5.4Zero probabilities
If q(x)=0 for an outcome with p(x)>0, the term p(x)\log\frac{p(x)}{0} is infinite. Anyone sure that it never rains in Cupertino in July is infinitely surprised on the one day in 310 that it does. This is why a model should never predict an exact zero for something that can happen, and why softmax and sigmoid outputs, which are always positive, are well suited to cross-entropy training. The reverse is harmless: outcomes with p(x)=0 contribute nothing.
5.5Forward versus reverse KL
When a simple model q must fit a complicated p, the direction matters (Murphy, 2022, §6.2.6), (Bishop, 2006, §10.1). Take the 272 eruption lengths of the Old Faithful geyser (Bishop, 2006, Appendix A): they form two humps, near 2 and 4.3 minutes. Fit a single Gaussian q.
- Forward KL, D_{\mathrm{KL}}(p\,\|\,q)=\E_{p}\bigl[\log\frac pq\bigr], averages over what really happens. Wherever p>0 and q\approx0 the cost explodes, so q must cover every hump: it is zero-avoiding or mode-covering. Minimizing it on data is maximum likelihood (Section 7), which for a Gaussian gives the sample mean and standard deviation, \mu=3.49 and \sigma=1.14 minutes: a wide curve over both humps, with most of its mass on the empty valley between them.
- Reverse KL, D_{\mathrm{KL}}(q\,\|\,p)=\E_{q}\bigl[\log\frac qp\bigr], averages over what the model imagines. Wherever q>0 but p\approx0 the cost explodes, so q avoids the valley and hugs one hump: it is zero-forcing or mode-seeking. Gradient descent finds \mu=4.28, \sigma=0.43, the larger hump.
Each fit wins on its own criterion: against the two-hump density, the forward fit has forward KL 0.76 bits and the reverse fit 5.99; the reverse fit has reverse KL 0.62 bits and the forward fit 1.68. Variational inference, including the variational autoencoder (Kingma and Welling, 2014), minimizes reverse KL because it needs expectations only under the simple q; this is why its approximations tend to be too narrow (Bishop, 2006, §10.1.2).
Pause and think. In classification with one-hot labels, the “true” distribution of each example puts probability 1 on its label. What is its entropy, and how do cross-entropy and KL compare?
Show the answer
The entropy of a one-hot distribution is 0, so for each example the cross-entropy loss equals the KL divergence from the label to the prediction. With soft labels (label smoothing, or a teacher’s probabilities in knowledge distillation) the entropy is positive and the two differ by that constant.