Post · 8 min read· pp. 21–25

KL-Divergence as an Objective Function

It's well-known that KL-divergence is not symmetric, but which direction is right for fitting your model?

Which KL Is Which? A Cheat Sheet

If we're fitting qθq_\theta to pp using

KL(p∣∣qθ)\textbf{KL}(p || q_\theta)

  • mean-seeking, inclusive (more principled because approximates the full distribution)
  • requires normalization wrt pp (i.e., often not computationally convenient)

KL(qθ∣∣p)\textbf{KL}(q_\theta || p)

  • mode-seeking, exclusive
  • no normalization wrt pp (i.e., computationally convenient)

Mnemonic: "When the truth comes first, you get the whole truth" (h/t Philip Resnik). Here "whole truth" corresponds to the inclusiveness of KL(p∣∣qθ)\textbf{KL}(p || q_\theta).

As far as remembering the equation, I pretend that "∣∣||" is a division symbol, which happens to correspond nicely to a division symbol in the equation (I'm not sure it's intentional).

Inclusive vs. Exclusive Divergence

Figure by John Winn.

For an interactive version of this diagram where you can drag the modes around and watch both directions optimize in real time, see the interactive KL divergence fitting post.

Computational Perspective

Let's look at what's involved in fitting a model qθq_\theta in each direction. In this section, I'll describe the gradient and pay special attention to the issue of normalization.

Notation: p,qθp,q_\theta are probability distributions. p=pˉ/Zpp = \bar{p} / Z_p, where ZpZ_p is the normalization constant. Similarly for qθq_\theta.

The Easy Direction KL(qθ∣∣p)\textbf{KL}(q_\theta || p)

\begin{align*} \textbf{KL}(q_\theta || p) &= \sum_d q_\theta(d) \log \left( \frac{q_\theta(d)}{p(d)} \right) \ &= \sum_d q_\theta(d) \left( \log q_\theta(d) - \log p(d) \right) \ &= \underbrace{\sum_d q_\theta(d) \log q_\theta(d)}{-\text{entropy}} - \underbrace{\sum_d q\theta(d) \log p(d)}_{\text{cross-entropy}} \ \end{align*}

Let's look at normalization of pp, the entropy term is easy because there is no pp in it. \begin{align*} \sum_d q_\theta(d) \log p(d) &= \sum_d q_\theta(d) \log (\bar{p}(d) / Z_p) \ &= \sum_d q_\theta(d) \left( \log \bar{p}(d) - \log Z_p \right) \ &= \sum_d q_\theta(d) \log \bar{p}(d) - \sum_d q_\theta(d) \log Z_p \ &= \sum_d q_\theta(d) \log \bar{p}(d) - \log Z_p \end{align*}

In this case, −log⁡Zp-\log Z_p is an additive constant, which can be dropped because we're optimizing.

This leaves us with the following optimization problem: \begin{align*} & \underset{\theta}{\text{argmin}}, \textbf{KL}(q_\theta || p) \ &\qquad = \underset{\theta}{\text{argmin}}, \sum_d q_\theta(d) \log q_\theta(d) - \sum_d q_\theta(d) \log \bar{p}(d) \end{align*}

Let's work out the gradient \begin{align*} & \nabla_\theta\left[ \sum_d q_\theta(d) \log q_\theta(d) - \sum_d q_\theta(d) \log \bar{p}(d) \right] \ &\qquad = \sum_d \nabla_\theta \left[ q_\theta(d) \log q_\theta(d) \right] - \sum_d \nabla_\theta\left[ q_\theta(d) \right] \log \bar{p}(d) \ &\qquad = \sum_d \nabla_\theta \left[ q_\theta(d) \right] \left( 1 + \log q_\theta(d) \right) - \sum_d \nabla_\theta\left[ q_\theta(d) \right] \log \bar{p}(d) \ &\qquad = \sum_d \nabla_\theta \left[ q_\theta(d) \right] \left( 1 + \log q_\theta(d) - \log \bar{p}(d) \right) \ &\qquad = \sum_d \nabla_\theta \left[ q_\theta(d) \right] \left( \log q_\theta(d) - \log \bar{p}(d) \right) \ &\qquad = \mathbb{E}{q\theta}!\left[ \left( \log q_\theta(d) - \log \bar{p}(d) \right) \nabla_\theta \log q_\theta(d) \right] \end{align*}

We killed the one in the second-to-last equality because $\sum_d \nabla_\theta \left[ q_\theta(d) \right] = \nabla_\theta \left[ \sum_d q_\theta(d) \right] = \nabla_\theta \left[ 1 \right] = 0$, for any qθq_\theta which is a probability distribution. The last step applies the log-gradient trick, $\nabla_\theta q_\theta(d) = q_\theta(d), \nabla_\theta \log q_\theta(d)$, to write the gradient as an expectation under qθq_\theta.

This direction is convenient because we don't need to normalize pp. Unfortunately, the "easy" direction is nonconvex in general—unlike the "hard" direction, which (as we'll see shortly) is convex.

When qθq_\theta is in the exponential family, $\nabla_\theta \log q_\theta(d) = \phi_q(d) - \mathbb{E}{q\theta}[\phi_q]$. Substituting into the gradient above gives

\begin{align*} & \mathbb{E}{q\theta}!\left[ \left( \log q_\theta(d) - \log \bar{p}(d) \right) \left(\phi_q(d) - \mathbb{E}{q\theta}[\phi_q]\right) \right] \ &\qquad = \text{Cov}{q\theta}!\left[\phi_q,; \log q_\theta - \log \bar{p},\right] \ &\qquad = \text{Cov}{q\theta}!\left[\phi_q,; \log (q_\theta / p)\right] \end{align*}

where the last step uses the fact that log⁡pˉ\log \bar{p} and log⁡p\log p differ by the constant log⁡Zp\log Z_p, which drops out of the covariance. In other words, this gradient is invariant to whether pp is normalized or not. Setting it to zero gives the optimality condition, residual orthogonality:

Covqθ ⁣[ϕq,  log⁡(qθ/p)]=0\text{Cov}_{q_\theta}\!\left[\phi_q,\; \log(q_\theta/p)\right] = 0

The log-density ratio log⁡(qθ/p)\log(q_\theta/p) must be uncorrelated with the sufficient statistics under qθq_\theta. No feature can predict whether qθq_\theta is over- or under-estimating pp—if one could, the optimizer would exploit it to further reduce the KL.

Harder Direction KL(p∣∣qθ)\textbf{KL}(p || q_\theta)

\begin{align*} \textbf{KL}(p || q_\theta) &= \sum_d p(d) \log \left( \frac{p(d)}{q_\theta(d)} \right) \ &= \sum_d p(d) \left( \log p(d) - \log q_\theta(d) \right) \ &= \sum_d p(d) \log p(d) - \sum_d p(d) \log q_\theta(d) \ \end{align*}

Clearly the first term (entropy) won't matter if we're just trying to optimize wrt θ\theta. So, let's focus on the second term (cross-entropy). \begin{align*} \sum_d p(d) \log q_\theta(d) &= \frac{1}{Z_p} \sum_d \bar{p}(d) \log \left( \bar{q}\theta(d)/Z{q_\theta} \right) \ &= \frac{1}{Z_p} \sum_d \bar{p}(d) \left( \log \bar{q}\theta(d) - \log Z{q_\theta} \right) \ &= \left(\frac{1}{Z_p} \sum_d \bar{p}(d) \log \bar{q}\theta(d)\right) - \left(\frac{1}{Z_p} \sum_d \bar{p}(d) \log Z{q_\theta}\right) \ &= \left(\frac{1}{Z_p} \sum_d \bar{p}(d) \log \bar{q}\theta(d)\right) - \left( \log Z{q_\theta} \right) \left( \frac{1}{Z_p} \sum_d \bar{p}(d)\right) \ &= \left(\frac{1}{Z_p} \sum_d \bar{p}(d) \log \bar{q}\theta(d)\right) - \log Z{q_\theta} \end{align*}

The gradient, when qθq_\theta is in the exponential family, is intuitive:

\begin{align*} \nabla_\theta \left[ \frac{1}{Z_p} \sum_d \bar{p}(d) \log \bar{q}\theta(d) - \log Z{q_\theta} \right] &= \frac{1}{Z_p} \sum_d \bar{p}(d) \nabla_\theta \left[ \log \bar{q}\theta(d) \right] - \nabla\theta \log Z_{q_\theta} \ &= \frac{1}{Z_p} \sum_d \bar{p}(d) \phi_q(d) - \mathbb{E}{q\theta} \left[ \phi_q \right] \ &= \mathbb{E}p \left[ \phi_q \right] - \mathbb{E}{q_\theta} \left[ \phi_q \right] \end{align*}

Setting this to zero gives the optimality condition, known as moment matching:

Ep[ϕq]=Eqθ[ϕq]\mathbb{E}_p \left[ \phi_q \right] = \mathbb{E}_{q_\theta} \left[ \phi_q \right]

The expected sufficient statistics under qθq_\theta must equal those under pp.

Why do we say this is hard to compute? Well, for most interesting models, we can't compute Zp=∑dpˉ(d)Z_p = \sum_d \bar{p}(d). This is because pp is presumed to be a complex model (e.g., the real world, an intricate factor graph, a complicated Bayesian posterior). If we can't compute ZpZ_p, it's highly unlikely that we can compute another (nontrivial) integral under pˉ\bar{p}, e.g., $\sum_d \bar{p}(d) \log \bar{q}_\theta(d)$.

Nonetheless, optimizing KL in this direction is still useful. Examples include: expectation propagation, variational decoding, and maximum likelihood estimation. In the case of maximum likelihood estimation, pp is the empirical distribution, so technically you don't have to compute its normalizing constant, but you do need samples from it, which can be just as hard to get as computing a normalization constant.

Optimization problem is convex when qθq_\theta is an exponential family—i.e., for any pp the optimization problem is "easy." You can think of maximum likelihood estimation (MLE) as a method which minimizes KL divergence based on samples of pp. In this case, pp is the true data distribution! The first term in the gradient is based on a sample instead of an exact estimate (often called "observed feature counts"). The downside, of course, is that computing Ep[ϕq]\mathbb{E}_p \left[ \phi_q \right] might not be tractable or, for MLE, require tons of samples.

Remarks

  • In many ways, optimizing exclusive KL makes no sense at all! Except for the fact that it's computable when inclusive KL is often not. Exclusive KL is generally regarded as "an approximation" to inclusive KL. The bias in this approximation can be quite large.
  • Inclusive vs. exclusive is an important distinction: Inclusive divergences require qθ>0q_\theta > 0 whenever p>0p > 0 (i.e., no "false negatives"), whereas exclusive divergences favor a single mode (i.e., only a good fit around that mode).
  • When qθq_\theta is an exponential family, KL(p∣∣qθ)\textbf{KL}(p || q_\theta) will be convex in θ\theta, no matter how complicated pp is, whereas $\textbf{KL}(q_\theta || p)$ is generally nonconvex (e.g., if pp is multimodal).
  • Computing the value of either KL divergence requires normalization. However, in the "easy" (exclusive) direction, we can optimize KL without computing ZpZ_p (as it results in only an additive constant difference).
  • Both directions of KL are special cases of α\alpha-divergence. For a unified account of both directions consider looking into α\alpha-divergence.

Acknowledgments

I'd like to thank the following people:

  • Ryan Cotterell for an email exchange which spawned this article.
  • Jason Eisner for teaching me all this stuff.
  • Florian Shkurti for a useful email discussion, which caught a bug in my explanation of why inclusive KL is hard to compute/optimize.
  • Sabrina Mielke for suggesting the "inclusive vs. exclusive" figure.