← All Posts
Machine Learning · Loss Functions · KL Divergence· Part 2

Forward KL versus Reverse KL

The direction of KL changes the question being asked.

Forward KL: Cover the Real Data

Forward KL gives a large penalty when real data appears somewhere but the model gives that region almost zero probability. So if the real data has two well-separated clusters and the model can only use one bell curve, it tends to spread out to cover both.

Reverse KL: Stay in a Plausible Region

Reverse KL only averages over samples the model itself generates. If the model never generates samples near one real cluster, it may not pay much penalty for missing that cluster. So with two real clusters and a simple one-cluster model, reverse KL may choose one cluster and fit it tightly.

Concrete Picture

Imagine real customer spending has two groups: budget customers around 20, and premium customers around 200. A one-bell-curve model cannot represent both groups perfectly.

The figure draws the same two-group idea using example positions $-3$ and $+3$. We choose these positions to make the picture simple. Each group contains half the probability, and each has standard deviation $0.5$. The model $Q$ is restricted to one Gaussian bell curve.

Same two groups, different compromises Green solid curves show the fixed data distribution $P$; orange dashed curves show the one-Gaussian model $Q$.

Here $\mathcal N(x;\mu,v)$ means a Gaussian density at $x$, with mean $\mu$ and variance $v$. The green curve is:

$$\begin{aligned} P(x)&=\tfrac12\mathcal N(x;-3,0.25)\\ &\quad+\tfrac12\mathcal N(x;3,0.25). \end{aligned}$$

In the formulas below, $\mathbb E_{x\sim P}$ means average over samples drawn from $P$; $\mathbb E_{x\sim Q}$ means average over samples drawn from $Q$. $P(x)$ and $Q(x)$ are the curves' density heights.

Forward KL: cover both groups

$$D_{KL}(P\|Q)=\mathbb E_{x\sim P}\!\left[\log_2\frac{P(x)}{Q(x)}\right].$$ Two green data peaks at minus 3 and plus 3, and one broad orange Gaussian centered at 0 that covers both peaks and the valley between them.

Where the average looks: both green peaks, because both generate real data. A narrow model at just one peak would give the other group's data extremely low probability.

The compromise: $Q$ spreads across both groups, but also puts probability in the quiet middle. The forward-optimal Gaussian here has mean 0 and variance 9.25, matching $P$'s mean and variance.

Reverse KL: fit one group

$$D_{KL}(Q\|P)=\mathbb E_{x\sim Q}\!\left[\log_2\frac{Q(x)}{P(x)}\right].$$ The same two green data peaks, with a narrow orange Gaussian centered at minus 3. It concentrates on the left group and gives the right group very little probability.

Where the average looks: mainly the left peak, where this $Q$ generates samples. Making $Q$ broad would send many samples into the middle, where $P$ is tiny.

The compromise: $Q$ fits one group closely and mostly misses the other. The illustrated model has mean $-3$ and variance $0.25$; choosing the right peak gives the same loss by symmetry.

Both plots use the same axes. The narrow orange peak is taller because it holds all of $Q$'s probability, while each green peak holds only half of $P$'s. Missing a group still leaves a positive reverse KL; it is not a perfect match.

These behaviors arise because one bell curve cannot reproduce two separated peaks. If $Q$ can represent $P$ exactly, both directions are minimized by $Q=P$. The mode-covering and mode-seeking comparison also appears in Stanford's variational-inference tutorial.

Neither direction is always better. Forward KL is safer when missing real possibilities is bad. Reverse KL is useful when generating sharp plausible samples matters more than covering every mode.

Where It Appears