Practice / Probability for ML

Entropy, cross-entropy and KL divergence

Ten problems on entropy, cross-entropy and the Kullback–Leibler divergence: the Bernoulli entropy and its maximum, why the uniform distribution maximises entropy, bits, nats and perplexity, cross-entropy as entropy plus KL, label smoothing, a proof that KL is non-negative, forward and reverse KL, the KL between two Gaussians and the VAE term with its gradients, with worked solutions and the mistakes that swap the arguments or drop a factor.

Before you start

Entropy, cross-entropy and the KL divergence are three views of one quantity: the average number of nats it costs to describe outcomes from one distribution using another. Cross-entropy is the loss a classifier is trained on, the KL divergence is the term a variational autoencoder adds to it, and entropy is the floor neither can go below. These ten problems compute each by hand, prove the one inequality everything rests on, show that the two orders of KL prefer different fits, and derive the closed forms for Gaussians that a VAE uses. The five mistakes at the end are the ones that produce a plausible number: KL with its arguments swapped, a zero probability treated as harmless, a Gaussian log term without its 12\tfrac12, a VAE gradient taken in the wrong variable, and a label-smoothed loss expected to reach zero.

  • pp and qq are distributions over KK outcomes i=1,…,Ki = 1, \dots, K: every pi≥0p_i \ge 0 and ∑ipi=1\sum_i p_i = 1, and the same for qq. log⁡\log is the natural log, and quantities measured with it are in nats; log⁡2\log_2 gives bits.
  • The entropy is H(p)=−∑ipilog⁡piH(p) = -\sum_i p_i\log p_i, the cross-entropy is H(p,q)=−∑ipilog⁡qiH(p, q) = -\sum_i p_i\log q_i, and the Kullback–Leibler divergence is KL⁡(p ∥ q)=∑ipilog⁡(pi/qi)\operatorname{KL}(p\,\|\,q) = \sum_i p_i\log(p_i/q_i).
  • Zero probabilities follow the usual conventions: a term with pi=0p_i = 0 is 00 in all three sums (0log⁡0=00\log 0 = 0, the limit of tlog⁡tt\log t as t→0+t \to 0^+), and a term with pi>0p_i > 0 and qi=0q_i = 0 makes H(p,q)H(p, q) and KL⁡(p ∥ q)\operatorname{KL}(p\,\|\,q) equal to +∞+\infty.
  • uu is the uniform distribution, ui=1/Ku_i = 1/K. yy is a one-hot target, 11 at the true class cc and 00 elsewhere, and 1\mathbf{1} is the all-ones vector.
  • N(μ,σ2)\mathcal{N}(\mu, \sigma^2) is the Gaussian with mean μ\mu, variance σ2>0\sigma^2 > 0 and density 12πσ2e−(x−μ)2/(2σ2)\frac{1}{\sqrt{2\pi\sigma^2}}e^{-(x - \mu)^2/(2\sigma^2)}. For densities the sums become integrals: KL⁡(p ∥ q)=∫p(x)log⁡p(x)q(x) dx\operatorname{KL}(p\,\|\,q) = \int p(x)\log\frac{p(x)}{q(x)}\,dx. Ep[f]\mathbb{E}_p[f] is the expectation of ff under pp.
  • The perplexity of a distribution or a model is eHe^{H} with HH in nats, equivalently 2H2^{H} with HH in bits: the number of equally likely outcomes that would have the same entropy.
  • The softmax page derives the gradient of cross-entropy with respect to the logits, s−ys - y, including soft targets; this page works with the distributions themselves.

Builds on: Maximum likelihood estimation: derivations by hand

Problems

  1. ·

    The entropy of a coin with probability pp of heads is H(p)=−plog⁡p−(1−p)log⁡(1−p)H(p) = -p\log p - (1 - p)\log(1 - p) for 0<p<10 < p < 1. Compute H′(p)H'(p), find the pp that maximises HH, and give the maximum in nats and in bits.

  2. ··

    Over KK outcomes, use a Lagrange multiplier to find the distribution pp that maximises H(p)H(p). Then show that KL⁡(p ∥ u)=log⁡K−H(p)\operatorname{KL}(p\,\|\,u) = \log K - H(p), and use it to confirm that the stationary point is the global maximum.

  3. ·

    Let p=(12,14,18,18)p = (\tfrac12, \tfrac14, \tfrac18, \tfrac18). Compute H(p)H(p) in bits and in nats, and its perplexity. Then: a language model's mean negative log-likelihood on a test set is 2.32.3 nats per token. What is its perplexity?

  4. ··

    Show that H(p,q)=H(p)+KL⁡(p ∥ q)H(p, q) = H(p) + \operatorname{KL}(p\,\|\,q). Then take label smoothing with KK classes and 0<ε<10 < \varepsilon < 1: the target is p=(1−ε)y+εK1p = (1 - \varepsilon)y + \tfrac{\varepsilon}{K}\mathbf{1}. Write H(p,q)H(p, q) in terms of log⁡qc\log q_c and ∑ilog⁡qi\sum_i\log q_i, and give its minimum over qq.

  5. ···

    Use the inequality log⁡t≤t−1\log t \le t - 1 for t>0t > 0 to prove that KL⁡(p ∥ q)≥0\operatorname{KL}(p\,\|\,q) \ge 0, with equality only when q=pq = p.

  6. ·

    Let p=(12,12)p = (\tfrac12, \tfrac12) and q=(910,110)q = (\tfrac{9}{10}, \tfrac{1}{10}). Compute KL⁡(p ∥ q)\operatorname{KL}(p\,\|\,q) and KL⁡(q ∥ p)\operatorname{KL}(q\,\|\,p).

  7. ··

    Let p=(12,0,12)p = (\tfrac12, 0, \tfrac12), a distribution with two modes, and consider two approximations: qA=(1,0,0)q_A = (1, 0, 0), which keeps one mode, and qB=(13,13,13)q_B = (\tfrac13, \tfrac13, \tfrac13), which covers everything. Compute KL⁡(p ∥ q)\operatorname{KL}(p\,\|\,q) and KL⁡(q ∥ p)\operatorname{KL}(q\,\|\,p) for both, and say which approximation each direction prefers.

  8. ···

    Let p=N(μ1,σ12)p = \mathcal{N}(\mu_1, \sigma_1^2) and q=N(μ2,σ22)q = \mathcal{N}(\mu_2, \sigma_2^2). Derive

    KL⁡(p ∥ q)=log⁡σ2σ1+σ12+(μ1−μ2)22σ22−12.\operatorname{KL}(p\,\|\,q) = \log\frac{\sigma_2}{\sigma_1} + \frac{\sigma_1^2 + (\mu_1 - \mu_2)^2}{2\sigma_2^2} - \frac12 .
  9. ···

    A VAE encoder outputs μ∈Rd\mu \in \mathbb{R}^d and s∈Rds \in \mathbb{R}^d with sj=log⁡σj2s_j = \log\sigma_j^2, defining q=N(μ,diag⁡(σ2))q = \mathcal{N}(\mu, \operatorname{diag}(\sigma^2)); the prior is N(0,I)\mathcal{N}(0, I). Show that

    KL⁡(q ∥ N(0,I))=12∑j=1d(μj2+esj−sj−1),\operatorname{KL}\big(q\,\|\,\mathcal{N}(0, I)\big) = \frac12\sum_{j=1}^{d}\big(\mu_j^2 + e^{s_j} - s_j - 1\big),

    and compute its gradients with respect to μ\mu and ss.

  10. ··

    Data x1,…,xNx_1, \dots, x_N take values in {1,…,K}\{1, \dots, K\}; outcome ii occurs nin_i times, and p^i=ni/N\hat p_i = n_i/N is the empirical distribution. A model assigns probabilities qq. Show that the mean negative log-likelihood equals H(p^,q)H(\hat p, q), and use it to find the qq that maximises the likelihood and the smallest mean negative log-likelihood.

Worked solutions

Problem 1

The entropy of a coin with probability pp of heads is H(p)=−plog⁡p−(1−p)log⁡(1−p)H(p) = -p\log p - (1 - p)\log(1 - p) for 0<p<10 < p < 1. Compute H′(p)H'(p), find the pp that maximises HH, and give the maximum in nats and in bits.

  1. ddp[−plog⁡p]=−log⁡p−1\dfrac{d}{dp}\big[-p\log p\big] = -\log p - 1.Product rule: pp differentiates to 11, and p⋅1p=1p\cdot\frac1p = 1.
  2. ddp[−(1−p)log⁡(1−p)]=log⁡(1−p)+1\dfrac{d}{dp}\big[-(1 - p)\log(1 - p)\big] = \log(1 - p) + 1.The same derivative at 1−p1 - p, times the inner derivative −1-1, which flips both signs.
  3. H′(p)=log⁡(1−p)−log⁡p=log⁡1−ppH'(p) = \log(1 - p) - \log p = \log\dfrac{1 - p}{p}.Add steps 1 and 2; the −1-1 and +1+1 cancel.
  4. H′(p)>0H'(p) > 0 for p<12p < \tfrac12, H′(p)=0H'(p) = 0 at p=12p = \tfrac12, and H′(p)<0H'(p) < 0 for p>12p > \tfrac12.log⁡t>0\log t > 0 exactly when t>1t > 1, and (1−p)/p>1(1 - p)/p > 1 exactly when p<12p < \tfrac12. So HH rises and then falls, and its one stationary point is the maximum.
  5. H(12)=−12log⁡12−12log⁡12=log⁡2H(\tfrac12) = -\tfrac12\log\tfrac12 - \tfrac12\log\tfrac12 = \log 2.Both terms are equal.
  6. H′(p)=log⁡1−ppH'(p) = \log\dfrac{1 - p}{p}; the maximum is at p=12p = \tfrac12, where H=ln⁡2≈0.693H = \ln 2 \approx 0.693 nats =1= 1 bitDividing by ln⁡2\ln 2 converts nats to bits, because log⁡2t=ln⁡t/ln⁡2\log_2 t = \ln t/\ln 2. A fair coin is the most uncertain coin, and one flip carries exactly one bit.

Problem 2

Over KK outcomes, use a Lagrange multiplier to find the distribution pp that maximises H(p)H(p). Then show that KL⁡(p ∥ u)=log⁡K−H(p)\operatorname{KL}(p\,\|\,u) = \log K - H(p), and use it to confirm that the stationary point is the global maximum.

  1. L(p,λ)=−∑ipilog⁡pi+λ(∑ipi−1)\mathcal{L}(p, \lambda) = -\sum_i p_i\log p_i + \lambda\big(\sum_i p_i - 1\big).The constraint ∑ipi=1\sum_i p_i = 1 enters with a multiplier. The constraints pi≥0p_i \ge 0 can be left out: ∂H/∂pi=−log⁡pi−1→+∞\partial H/\partial p_i = -\log p_i - 1 \to +\infty as pi→0+p_i \to 0^+, so a maximiser never sits on that boundary.
  2. ∂L/∂pi=−log⁡pi−1+λ=0\partial\mathcal{L}/\partial p_i = -\log p_i - 1 + \lambda = 0, so pi=eλ−1p_i = e^{\lambda - 1}.Only the ii-th term of each sum contains pip_i. The right-hand side is the same for every ii, so all the pip_i are equal.
  3. ∑ipi=Keλ−1=1\sum_i p_i = Ke^{\lambda - 1} = 1, so pi=1/Kp_i = 1/K, and H(u)=−∑i1Klog⁡1K=log⁡KH(u) = -\sum_i \tfrac1K\log\tfrac1K = \log K.The constraint fixes the common value.
  4. KL⁡(p ∥ u)=∑ipilog⁡(Kpi)=log⁡K∑ipi+∑ipilog⁡pi=log⁡K−H(p)\operatorname{KL}(p\,\|\,u) = \sum_i p_i\log(Kp_i) = \log K\sum_i p_i + \sum_i p_i\log p_i = \log K - H(p).pi/ui=Kpip_i/u_i = Kp_i, the log of a product is a sum of logs, and ∑ipi=1\sum_i p_i = 1.
  5. p=up = u, pi=1/Kp_i = 1/K, with maximum H(u)=log⁡KH(u) = \log K; for every pp, H(p)=log⁡K−KL⁡(p ∥ u)≤log⁡KH(p) = \log K - \operatorname{KL}(p\,\|\,u) \le \log KKL⁡≥0\operatorname{KL} \ge 0 with equality only when the two distributions are equal (Problem 5), so step 4 bounds every entropy by log⁡K\log K, with equality only at p=up = u. No second-derivative test is needed.

Problem 3

Let p=(12,14,18,18)p = (\tfrac12, \tfrac14, \tfrac18, \tfrac18). Compute H(p)H(p) in bits and in nats, and its perplexity. Then: a language model's mean negative log-likelihood on a test set is 2.32.3 nats per token. What is its perplexity?

  1. H(p)=12⋅1+14⋅2+18⋅3+18⋅3=74H(p) = \tfrac12\cdot 1 + \tfrac14\cdot 2 + \tfrac18\cdot 3 + \tfrac18\cdot 3 = \tfrac74 bits.Each probability is a power of 22, and −log⁡22−k=k-\log_2 2^{-k} = k.
  2. H(p)=74ln⁡2≈1.213H(p) = \tfrac74\ln 2 \approx 1.213 nats.ln⁡t=ln⁡2⋅log⁡2t\ln t = \ln 2\cdot\log_2 t, so every term, and the sum, scales by ln⁡2\ln 2.
  3. Perplexity =27/4=e(7/4)ln⁡2≈3.364= 2^{7/4} = e^{(7/4)\ln 2} \approx 3.364.The base of the exponential must match the unit of HH: 22 for bits, ee for nats. Both give the same number. It lies between 22 and 44: the distribution is as uncertain as a fair choice among about 3.43.4 outcomes.
  4. The mean negative log-likelihood is the cross-entropy between the test data and the model, in nats, so the perplexity is e2.3e^{2.3}.Problem 10 shows the mean negative log-likelihood is a cross-entropy; the unit is nats because the loss used ln⁡\ln.
  5. H(p)=74H(p) = \tfrac74 bits =74ln⁡2≈1.213= \tfrac74\ln 2 \approx 1.213 nats; perplexity 27/4≈3.3642^{7/4} \approx 3.364; a mean NLL of 2.32.3 nats gives perplexity e2.3≈9.97e^{2.3} \approx 9.97The model is, on average, as unsure of the next token as a uniform choice among about ten.

Problem 4

Show that H(p,q)=H(p)+KL⁡(p ∥ q)H(p, q) = H(p) + \operatorname{KL}(p\,\|\,q). Then take label smoothing with KK classes and 0<ε<10 < \varepsilon < 1: the target is p=(1−ε)y+εK1p = (1 - \varepsilon)y + \tfrac{\varepsilon}{K}\mathbf{1}. Write H(p,q)H(p, q) in terms of log⁡qc\log q_c and ∑ilog⁡qi\sum_i\log q_i, and give its minimum over qq.

  1. KL⁡(p ∥ q)=∑ipilog⁡pi−∑ipilog⁡qi=−H(p)+H(p,q)\operatorname{KL}(p\,\|\,q) = \sum_i p_i\log p_i - \sum_i p_i\log q_i = -H(p) + H(p, q).log⁡(pi/qi)=log⁡pi−log⁡qi\log(p_i/q_i) = \log p_i - \log q_i, and each sum is one of the definitions.
  2. H(p,q)=H(p)+KL⁡(p ∥ q)H(p, q) = H(p) + \operatorname{KL}(p\,\|\,q).Rearrange step 1.
  3. H(p)H(p) does not depend on qq, so minimising H(p,q)H(p, q) over qq is minimising KL⁡(p ∥ q)\operatorname{KL}(p\,\|\,q), and the minimum is H(p)H(p), at q=pq = p.KL⁡(p ∥ q)≥0\operatorname{KL}(p\,\|\,q) \ge 0, with equality only at q=pq = p (Problem 5).
  4. H(p,q)=−∑i[(1−ε)yi+εK]log⁡qi=−(1−ε)log⁡qc−εK∑ilog⁡qiH(p, q) = -\sum_i\big[(1 - \varepsilon)y_i + \tfrac{\varepsilon}{K}\big]\log q_i = -(1 - \varepsilon)\log q_c - \tfrac{\varepsilon}{K}\sum_i\log q_i.Cross-entropy is linear in its first argument, and the one-hot yy picks out the term i=ci = c.
  5. H(p,q)=H(p)+KL⁡(p ∥ q)H(p, q) = H(p) + \operatorname{KL}(p\,\|\,q); with label smoothing H(p,q)=−(1−ε)log⁡qc−εK∑ilog⁡qiH(p, q) = -(1 - \varepsilon)\log q_c - \tfrac{\varepsilon}{K}\sum_i\log q_i, minimised at q=pq = p with value H(p)>0H(p) > 0The second term penalises any class whose probability goes to 00, which is how smoothing stops the logits growing without bound. Because pp is not one-hot, H(p)>0H(p) > 0: the loss has a floor above zero (the last mistake below).

Problem 5

Use the inequality log⁡t≤t−1\log t \le t - 1 for t>0t > 0 to prove that KL⁡(p ∥ q)≥0\operatorname{KL}(p\,\|\,q) \ge 0, with equality only when q=pq = p.

  1. log⁡t≤t−1\log t \le t - 1 for every t>0t > 0, with equality only at t=1t = 1.f(t)=t−1−log⁡tf(t) = t - 1 - \log t has f′(t)=1−1/tf'(t) = 1 - 1/t, negative for t<1t < 1 and positive for t>1t > 1, so its minimum is f(1)=0f(1) = 0.
  2. Let S={i:pi>0}S = \{i : p_i > 0\}. If qi=0q_i = 0 for some i∈Si \in S, then KL⁡(p ∥ q)=+∞>0\operatorname{KL}(p\,\|\,q) = +\infty > 0. Otherwise −KL⁡(p ∥ q)=∑i∈Spilog⁡qipi-\operatorname{KL}(p\,\|\,q) = \sum_{i \in S} p_i\log\dfrac{q_i}{p_i}.Terms with pi=0p_i = 0 are 00 by convention, and −log⁡(pi/qi)=log⁡(qi/pi)-\log(p_i/q_i) = \log(q_i/p_i).
  3. ∑i∈Spilog⁡qipi≤∑i∈Spi(qipi−1)=∑i∈Sqi−1\sum_{i \in S} p_i\log\dfrac{q_i}{p_i} \le \sum_{i \in S} p_i\Big(\dfrac{q_i}{p_i} - 1\Big) = \sum_{i \in S} q_i - 1.Step 1 with t=qi/pi>0t = q_i/p_i > 0, multiplied by the positive weight pip_i; then ∑i∈Spi=1\sum_{i \in S} p_i = 1.
  4. ∑i∈Sqi≤∑iqi=1\sum_{i \in S} q_i \le \sum_i q_i = 1, so −KL⁡(p ∥ q)≤0-\operatorname{KL}(p\,\|\,q) \le 0.Leaving out terms of a sum of non-negative numbers can only make it smaller.
  5. Equality needs qi/pi=1q_i/p_i = 1 for every i∈Si \in S (step 3) and ∑i∈Sqi=1\sum_{i \in S} q_i = 1 (step 4), so qi=piq_i = p_i on SS and qi=0=piq_i = 0 = p_i off it.The equality case of step 1 applies term by term, because every weight pip_i in step 3 is positive.
  6. KL⁡(p ∥ q)≥0\operatorname{KL}(p\,\|\,q) \ge 0 for all p,qp, q, with equality if and only if q=pq = pThis is Gibbs' inequality. It is what makes H(p)H(p) the floor of the cross-entropy (Problem 4), log⁡K\log K the ceiling of the entropy (Problem 2) and the empirical distribution the maximum-likelihood fit (Problem 10).

Problem 6

Let p=(12,12)p = (\tfrac12, \tfrac12) and q=(910,110)q = (\tfrac{9}{10}, \tfrac{1}{10}). Compute KL⁡(p ∥ q)\operatorname{KL}(p\,\|\,q) and KL⁡(q ∥ p)\operatorname{KL}(q\,\|\,p).

  1. KL⁡(p ∥ q)=12log⁡1/29/10+12log⁡1/21/10=12log⁡59+12log⁡5=12log⁡259=log⁡53\operatorname{KL}(p\,\|\,q) = \tfrac12\log\dfrac{1/2}{9/10} + \tfrac12\log\dfrac{1/2}{1/10} = \tfrac12\log\tfrac59 + \tfrac12\log 5 = \tfrac12\log\tfrac{25}{9} = \log\tfrac53.The definition, then 12(log⁡a+log⁡b)=12log⁡ab\tfrac12(\log a + \log b) = \tfrac12\log ab and 12log⁡(25/9)=log⁡(5/3)\tfrac12\log(25/9) = \log(5/3).
  2. KL⁡(q ∥ p)=910log⁡9/101/2+110log⁡1/101/2=0.9log⁡1.8−0.1log⁡5\operatorname{KL}(q\,\|\,p) = \tfrac{9}{10}\log\dfrac{9/10}{1/2} + \tfrac{1}{10}\log\dfrac{1/10}{1/2} = 0.9\log 1.8 - 0.1\log 5.The weights now come from qq, and log⁡(1/5)=−log⁡5\log(1/5) = -\log 5.
  3. KL⁡(p ∥ q)=ln⁡53≈0.511\operatorname{KL}(p\,\|\,q) = \ln\tfrac53 \approx 0.511; KL⁡(q ∥ p)=0.9ln⁡1.8−0.1ln⁡5≈0.368\operatorname{KL}(q\,\|\,p) = 0.9\ln 1.8 - 0.1\ln 5 \approx 0.368The two differ, so KL is not symmetric and not a distance. The larger one weights by pp, which puts half its mass on an outcome qq gives only 110\tfrac1{10}: KL is large when the first distribution is often surprised by the second.

Problem 7

Let p=(12,0,12)p = (\tfrac12, 0, \tfrac12), a distribution with two modes, and consider two approximations: qA=(1,0,0)q_A = (1, 0, 0), which keeps one mode, and qB=(13,13,13)q_B = (\tfrac13, \tfrac13, \tfrac13), which covers everything. Compute KL⁡(p ∥ q)\operatorname{KL}(p\,\|\,q) and KL⁡(q ∥ p)\operatorname{KL}(q\,\|\,p) for both, and say which approximation each direction prefers.

  1. KL⁡(p ∥ qA)=+∞\operatorname{KL}(p\,\|\,q_A) = +\infty.Outcome 33 has p3=12>0p_3 = \tfrac12 > 0 and qA,3=0q_{A,3} = 0, so its term is 12log⁡(12/0)=+∞\tfrac12\log(\tfrac12/0) = +\infty.
  2. KL⁡(p ∥ qB)=12log⁡1/21/3+0+12log⁡1/21/3=log⁡32≈0.405\operatorname{KL}(p\,\|\,q_B) = \tfrac12\log\tfrac{1/2}{1/3} + 0 + \tfrac12\log\tfrac{1/2}{1/3} = \log\tfrac32 \approx 0.405.The middle term has p2=0p_2 = 0 and is 00 by convention.
  3. KL⁡(qA ∥ p)=1⋅log⁡11/2=log⁡2≈0.693\operatorname{KL}(q_A\,\|\,p) = 1\cdot\log\dfrac{1}{1/2} = \log 2 \approx 0.693.Now the weights are qAq_A's; the terms with qA,i=0q_{A,i} = 0 vanish.
  4. KL⁡(qB ∥ p)=+∞\operatorname{KL}(q_B\,\|\,p) = +\infty.Outcome 22 has qB,2=13>0q_{B,2} = \tfrac13 > 0 and p2=0p_2 = 0.
  5. KL⁡(p ∥ qA)=∞\operatorname{KL}(p\,\|\,q_A) = \infty, KL⁡(p ∥ qB)=ln⁡32\operatorname{KL}(p\,\|\,q_B) = \ln\tfrac32; KL⁡(qA ∥ p)=ln⁡2\operatorname{KL}(q_A\,\|\,p) = \ln 2, KL⁡(qB ∥ p)=∞\operatorname{KL}(q_B\,\|\,p) = \infty: the forward KL prefers the covering qBq_B, the reverse KL the single-mode qAq_AKL⁡(p ∥ q)\operatorname{KL}(p\,\|\,q) is infinite when qq misses mass that pp has, so minimising it over qq spreads qq out (mass-covering). KL⁡(q ∥ p)\operatorname{KL}(q\,\|\,p) is infinite when qq puts mass where pp has none, so minimising it shrinks qq onto one mode (mode-seeking). Maximum likelihood minimises the forward direction (Problem 10); variational inference, including the VAE, minimises the reverse.

Problem 8

Let p=N(μ1,σ12)p = \mathcal{N}(\mu_1, \sigma_1^2) and q=N(μ2,σ22)q = \mathcal{N}(\mu_2, \sigma_2^2). Derive

KL⁡(p ∥ q)=log⁡σ2σ1+σ12+(μ1−μ2)22σ22−12.\operatorname{KL}(p\,\|\,q) = \log\frac{\sigma_2}{\sigma_1} + \frac{\sigma_1^2 + (\mu_1 - \mu_2)^2}{2\sigma_2^2} - \frac12 .
  1. log⁡p(x)=−12log⁡(2πσ12)−(x−μ1)22σ12\log p(x) = -\tfrac12\log(2\pi\sigma_1^2) - \dfrac{(x - \mu_1)^2}{2\sigma_1^2}, and likewise for qq.The log of the density in the definitions.
  2. log⁡p(x)−log⁡q(x)=log⁡σ2σ1−(x−μ1)22σ12+(x−μ2)22σ22\log p(x) - \log q(x) = \log\dfrac{\sigma_2}{\sigma_1} - \dfrac{(x - \mu_1)^2}{2\sigma_1^2} + \dfrac{(x - \mu_2)^2}{2\sigma_2^2}.The 2π2\pi cancels, and −12log⁡σ12+12log⁡σ22=log⁡(σ2/σ1)-\tfrac12\log\sigma_1^2 + \tfrac12\log\sigma_2^2 = \log(\sigma_2/\sigma_1).
  3. Ep[(x−μ1)2]=σ12\mathbb{E}_p\big[(x - \mu_1)^2\big] = \sigma_1^2.The definition of the variance of pp.
  4. Ep[(x−μ2)2]=σ12+(μ1−μ2)2\mathbb{E}_p\big[(x - \mu_2)^2\big] = \sigma_1^2 + (\mu_1 - \mu_2)^2.Write x−μ2=(x−μ1)+(μ1−μ2)x - \mu_2 = (x - \mu_1) + (\mu_1 - \mu_2) and expand: the cross term is 2(μ1−μ2) Ep[x−μ1]=02(\mu_1 - \mu_2)\,\mathbb{E}_p[x - \mu_1] = 0.
  5. KL⁡(p ∥ q)=Ep[log⁡p(x)−log⁡q(x)]=log⁡σ2σ1−σ122σ12+σ12+(μ1−μ2)22σ22\operatorname{KL}(p\,\|\,q) = \mathbb{E}_p[\log p(x) - \log q(x)] = \log\dfrac{\sigma_2}{\sigma_1} - \dfrac{\sigma_1^2}{2\sigma_1^2} + \dfrac{\sigma_1^2 + (\mu_1 - \mu_2)^2}{2\sigma_2^2}.KL is the expectation under pp of the log-ratio; expectation is linear, so steps 3 and 4 apply term by term.
  6. KL⁡(p ∥ q)=ln⁡σ2σ1+σ12+(μ1−μ2)22σ22−12\operatorname{KL}(p\,\|\,q) = \ln\dfrac{\sigma_2}{\sigma_1} + \dfrac{\sigma_1^2 + (\mu_1 - \mu_2)^2}{2\sigma_2^2} - \dfrac12Sanity check: with μ1=μ2\mu_1 = \mu_2 and σ1=σ2\sigma_1 = \sigma_2 it is 0+12−12=00 + \tfrac12 - \tfrac12 = 0. The mean difference is measured in units of qq's spread, σ2\sigma_2, which is again not symmetric.

Problem 9

A VAE encoder outputs μ∈Rd\mu \in \mathbb{R}^d and s∈Rds \in \mathbb{R}^d with sj=log⁡σj2s_j = \log\sigma_j^2, defining q=N(μ,diag⁡(σ2))q = \mathcal{N}(\mu, \operatorname{diag}(\sigma^2)); the prior is N(0,I)\mathcal{N}(0, I). Show that

KL⁡(q ∥ N(0,I))=12∑j=1d(μj2+esj−sj−1),\operatorname{KL}\big(q\,\|\,\mathcal{N}(0, I)\big) = \frac12\sum_{j=1}^{d}\big(\mu_j^2 + e^{s_j} - s_j - 1\big),

and compute its gradients with respect to μ\mu and ss.

  1. Both densities are products over coordinates, so log⁡q(x)−log⁡p0(x)=∑j[log⁡qj(xj)−log⁡p0(xj)]\log q(x) - \log p_0(x) = \sum_j\big[\log q_j(x_j) - \log p_{0}(x_j)\big], and the KL is ∑jKL⁡(N(μj,σj2) ∥ N(0,1))\sum_j \operatorname{KL}\big(\mathcal{N}(\mu_j, \sigma_j^2)\,\|\,\mathcal{N}(0, 1)\big).A diagonal covariance makes the coordinates independent, the log of a product is a sum, and the expectation of each term involves only xjx_j, whose distribution under qq is N(μj,σj2)\mathcal{N}(\mu_j, \sigma_j^2).
  2. Each term is −log⁡σj+12(σj2+μj2)−12-\log\sigma_j + \tfrac12(\sigma_j^2 + \mu_j^2) - \tfrac12.Problem 8 with μ1=μj\mu_1 = \mu_j, σ1=σj\sigma_1 = \sigma_j, μ2=0\mu_2 = 0, σ2=1\sigma_2 = 1.
  3. −log⁡σj=−12log⁡σj2=−12sj-\log\sigma_j = -\tfrac12\log\sigma_j^2 = -\tfrac12 s_j, and σj2=esj\sigma_j^2 = e^{s_j}.Rewrite in the variable the encoder outputs.
  4. KL⁡=12∑j(μj2+esj−sj−1)\operatorname{KL} = \tfrac12\sum_j\big(\mu_j^2 + e^{s_j} - s_j - 1\big).Steps 2 and 3, summed over jj.
  5. ∂KL⁡/∂μj=μj\partial\operatorname{KL}/\partial\mu_j = \mu_j and ∂KL⁡/∂sj=12(esj−1)\partial\operatorname{KL}/\partial s_j = \tfrac12(e^{s_j} - 1).Only the jj-th term contains μj\mu_j or sjs_j; 12μj2\tfrac12\mu_j^2 differentiates to μj\mu_j, and 12(esj−sj)\tfrac12(e^{s_j} - s_j) to 12(esj−1)\tfrac12(e^{s_j} - 1).
  6. KL⁡=12∑j(μj2+esj−sj−1)\operatorname{KL} = \tfrac12\sum_j(\mu_j^2 + e^{s_j} - s_j - 1); ∇μKL⁡=μ\nabla_\mu\operatorname{KL} = \mu and ∂KL⁡/∂sj=12(esj−1)=12(σj2−1)\partial\operatorname{KL}/\partial s_j = \tfrac12(e^{s_j} - 1) = \tfrac12(\sigma_j^2 - 1)Both gradients vanish exactly at μ=0\mu = 0, σ2=1\sigma^2 = \mathbf{1}, the prior. Outputting s=log⁡σ2s = \log\sigma^2 rather than σ2\sigma^2 keeps the variance positive with no constraint on the network.

Problem 10

Data x1,…,xNx_1, \dots, x_N take values in {1,…,K}\{1, \dots, K\}; outcome ii occurs nin_i times, and p^i=ni/N\hat p_i = n_i/N is the empirical distribution. A model assigns probabilities qq. Show that the mean negative log-likelihood equals H(p^,q)H(\hat p, q), and use it to find the qq that maximises the likelihood and the smallest mean negative log-likelihood.

  1. 1N∑n=1N−log⁡qxn=∑i=1KniN (−log⁡qi)\dfrac1N\sum_{n=1}^{N} -\log q_{x_n} = \sum_{i=1}^{K}\dfrac{n_i}{N}\,(-\log q_i).Group the terms by outcome: the term −log⁡qi-\log q_i appears once for each of the nin_i data points equal to ii.
  2. =H(p^,q)=H(p^)+KL⁡(p^ ∥ q)= H(\hat p, q) = H(\hat p) + \operatorname{KL}(\hat p\,\|\,q).The definition of cross-entropy with p^i=ni/N\hat p_i = n_i/N, then Problem 4.
  3. H(p^)H(\hat p) does not depend on qq, and KL⁡(p^ ∥ q)≥0\operatorname{KL}(\hat p\,\|\,q) \ge 0 with equality only at q=p^q = \hat p.Problem 5. Maximising the likelihood is minimising the mean negative log-likelihood, because log⁡\log is increasing and 1/N>01/N > 0.
  4. Mean NLL =H(p^,q)=H(p^)+KL⁡(p^ ∥ q)= H(\hat p, q) = H(\hat p) + \operatorname{KL}(\hat p\,\|\,q); it is minimised at qi=ni/Nq_i = n_i/N, where it equals H(p^)H(\hat p)The maximum-likelihood page reaches the same estimate with a Lagrange multiplier. For a model family qθq_\theta that cannot reach p^\hat p, step 2 still holds: maximum likelihood picks the θ\theta minimising the forward divergence KL⁡(p^ ∥ qθ)\operatorname{KL}(\hat p\,\|\,q_\theta).

Where this goes wrong

1. Maximum likelihood written as KL(q_θ ‖ p̂)

KL is often described as the divergence between the model and the data, and the order of its arguments looks like a choice of notation.

  1. Mean NLL =H(p^)+KL⁡(p^ ∥ qθ)= H(\hat p) + \operatorname{KL}(\hat p\,\|\,q_\theta)Right so far: Problem 10, step 2.
  2. “Fitting a model to data minimises how far the model is from the data, KL⁡(model ∥ data)\operatorname{KL}(\text{model}\,\|\,\text{data}).”The phrasing that causes the mistake: it names the model first, but the order is fixed by which distribution the log-loss averages over, and the loss averages over the data.
  3. Maximum likelihood minimises KL⁡(qθ ∥ p^)\operatorname{KL}(q_\theta\,\|\,\hat p)p^\hat p is 00 on every outcome that is absent from the data, so KL⁡(qθ ∥ p^)=∞\operatorname{KL}(q_\theta\,\|\,\hat p) = \infty for any model that gives an unseen outcome positive probability, including every softmax model. Maximum likelihood minimises KL⁡(p^ ∥ qθ)\operatorname{KL}(\hat p\,\|\,q_\theta), the mass-covering direction (Problem 7); the reverse is the one variational inference minimises.

2. KL term with q_i = 0 dropped as if it were 0 log 0

The convention 0log⁡0=00\log 0 = 0 makes zero probabilities harmless in an entropy, and it is tempting to apply it to every zero in a KL.

  1. KL⁡(p ∥ qA)=∑ipilog⁡(pi/qA,i)\operatorname{KL}(p\,\|\,q_A) = \sum_i p_i\log(p_i/q_{A,i}) with p=(12,0,12)p = (\tfrac12, 0, \tfrac12) and qA=(1,0,0)q_A = (1, 0, 0)Right so far: the set-up of Problem 7.
  2. “Terms with a zero probability contribute nothing, so keep only the outcomes where both are positive.”The shortcut that causes the mistake: the convention covers a zero weight pip_i outside the log, not a zero qiq_i inside it.
  3. KL⁡(p ∥ qA)=12log⁡1/21=−12ln⁡2\operatorname{KL}(p\,\|\,q_A) = \tfrac12\log\tfrac{1/2}{1} = -\tfrac12\ln 2A negative KL is already a contradiction of Problem 5. The third term has p3=12p_3 = \tfrac12 and qA,3=0q_{A,3} = 0, so it is +∞+\infty, and so is the KL (Problem 7, step 1). In code the same slip appears as a mask that skips qi=0q_i = 0, or a clamp qi≥10−12q_i \ge 10^{-12} that turns ∞\infty into a large finite number.

3. Gaussian KL with log(σ₂²/σ₁²) and no ½

Gaussians are usually parametrised by their variances, and log-variance is what a network outputs, so the log term gets written with variances.

  1. log⁡p(x)=−12log⁡(2πσ12)−(x−μ1)2/(2σ12)\log p(x) = -\tfrac12\log(2\pi\sigma_1^2) - (x - \mu_1)^2/(2\sigma_1^2)Right so far: Problem 8, step 1.
  2. “The normalising constants contribute the log of the ratio of the variances.”The shortcut that causes the mistake: dropping the 12\tfrac12 that comes from the square root in 1/2πσ21/\sqrt{2\pi\sigma^2}.
  3. KL⁡=log⁡(σ22/σ12)+σ12+(μ1−μ2)22σ22−12\operatorname{KL} = \log(\sigma_2^2/\sigma_1^2) + \dfrac{\sigma_1^2 + (\mu_1 - \mu_2)^2}{2\sigma_2^2} - \dfrac12The log term is twice what it should be: log⁡(σ2/σ1)=12log⁡(σ22/σ12)\log(\sigma_2/\sigma_1) = \tfrac12\log(\sigma_2^2/\sigma_1^2) (Problem 8). With μ1=μ2\mu_1 = \mu_2, σ1=1\sigma_1 = 1, σ2=2\sigma_2 = 2 the correct value is ln⁡2+18−12≈0.318\ln 2 + \tfrac18 - \tfrac12 \approx 0.318 and this gives ln⁡4−38≈1.011\ln 4 - \tfrac38 \approx 1.011. The test p=qp = q still gives 00, so only a check with unequal variances catches it.

4. VAE gradient for log σ² computed as the derivative in σ

Code calls the encoder's second output “the variance”, and the KL is easiest to differentiate in the form it was first written in, with σj\sigma_j.

  1. KL⁡j=−log⁡σj+12(σj2+μj2)−12\operatorname{KL}_j = -\log\sigma_j + \tfrac12(\sigma_j^2 + \mu_j^2) - \tfrac12Right so far: Problem 9, step 2.
  2. “Differentiate with respect to the variance parameter, σj\sigma_j, and pass that to the encoder.”The habit that causes the mistake: the encoder outputs sj=log⁡σj2s_j = \log\sigma_j^2, and the gradient backprop needs is with respect to that output.
  3. ∂KL⁡/∂sj=σj−1/σj\partial\operatorname{KL}/\partial s_j = \sigma_j - 1/\sigma_jThat is ∂KL⁡/∂σj\partial\operatorname{KL}/\partial\sigma_j. With σj=esj/2\sigma_j = e^{s_j/2}, dσj/dsj=σj/2d\sigma_j/ds_j = \sigma_j/2, so ∂KL⁡/∂sj=(σj−1/σj) σj/2=12(σj2−1)\partial\operatorname{KL}/\partial s_j = (\sigma_j - 1/\sigma_j)\,\sigma_j/2 = \tfrac12(\sigma_j^2 - 1) (Problem 9). Both vanish at σj=1\sigma_j = 1, which hides the slip, but at σj=3\sigma_j = 3 the wrong value is 83\tfrac83 against the correct 44.

5. Reading a label-smoothed loss of 0.51 as far from optimal

A one-hot cross-entropy can be driven towards 00, and a loss curve trained with label smoothing gets read on the same scale.

  1. H(p,q)=H(p)+KL⁡(p ∥ q)H(p, q) = H(p) + \operatorname{KL}(p\,\|\,q) with p=(1−ε)y+εK1p = (1 - \varepsilon)y + \tfrac{\varepsilon}{K}\mathbf{1}Right so far: Problem 4.
  2. “A perfect model has zero loss.”The assumption that causes the mistake: it holds only for a one-hot target, whose entropy is 00.
  3. With ε=0.1\varepsilon = 0.1 and K=10K = 10, a training loss that levels off at 0.510.51 nats means the model is still far from its targetsThe floor is H(p)H(p), with pc=0.91p_c = 0.91 and the other nine entries 0.010.01: −0.91ln⁡0.91−9(0.01)ln⁡0.01≈0.500-0.91\ln 0.91 - 9(0.01)\ln 0.01 \approx 0.500. A loss of 0.510.51 is a KL of about 0.010.01, almost at the optimum. Subtract H(p)H(p) before reading the curve.

Print this set: entropy-cross-entropy-and-kl-divergence.pdf (problems, answers, and worked solutions on separate pages).