Skip to content
mlmentorship

KL divergence

Asymmetric distance between probability distributions. Cross-entropy minus entropy. The mathematical glue holding most of probabilistic ML together.

Published · 5 min read ·Core ·Foundation

Visual quick review

Visual first · depth when needed

Predict why forward KL prefers a distribution that covers both modes while reverse KL can prefer a distribution concentrated on one mode when the approximation family cannot match a bimodal target.

Preparing the visual…

Summary

For probability distributions and over the same space:

It’s the expected log-ratio of to under . Measuring how much information is lost using to encode samples from .

KL divergence is the fundamental object of statistical learning. It connects:

  • Maximum likelihood (minimizing ).
  • Variational inference (minimizing ).
  • Cross-entropy loss = entropy of data + KL.
  • Information bottleneck and mutual information.
  • Policy gradient methods in RL (TRPO, PPO use KL constraints).
  • Knowledge distillation (student matches teacher distribution via KL).

Properties

  • Non-negative: , with equality iff (Gibbs’ inequality).
  • Asymmetric: in general. Choose direction based on whether you are “fitting to ” or vice versa.
  • Not a metric: no triangle inequality, not symmetric.
  • Infinite if where : must cover the support of .
  • Information-theoretic: equals expected extra bits (or nats) per sample needed to encode using a code optimized for .

Forward vs. reverse KL

The asymmetry matters in practice. For approximating with :

  • Forward KL, : penalizes for missing modes of (“mass-covering”; under a restricted unimodal family, the result often sits between modes). Used in standard MLE.
  • Reverse KL, : penalizes for placing mass where has none (“mode-seeking”. collapses to one mode). Used in variational inference.

For a multimodal and a restricted approximation family, forward KL often gives a broad average while reverse KL can pick one mode.

Learning objective

Why can swapping the KL arguments change which approximation wins?

Forward and reverse KL rank two approximations differently A three-outcome target distribution p has probabilities 0.49, 0.02, and 0.49, forming two modes. A broad candidate q cover has probabilities 0.34, 0.32, and 0.34. A one-mode candidate q left has probabilities 0.96, 0.02, and 0.02. Forward KL from p to q is 0.30 nats for the broad candidate and 1.24 for the one-mode candidate, so it selects broad coverage. Reverse KL from q to p is 0.64 nats for the broad candidate and 0.58 for the one-mode candidate, so it selects one mode. The example compares only these fixed candidates. TARGET p · TWO MODES 0.49 0.02 0.49 left middle right CANDIDATE A · q cover · BROAD 0.34 0.32 0.34 Places mass across both target modes and the low-density middle. CANDIDATE B · q left · ONE MODE · DASHED PANEL 0.96 0.02 0.02 Concentrates on the left target mode and nearly ignores the right. DIRECTION · NATURAL-LOG UNITS BROAD ONE MODE KL(p || q) · p weights misses 0.30 · PICK 1.24 KL(q || p) · q weights extras 0.64 0.58 · PICK Same target and candidates; only the expectation changes.
Read it this way: hold the target and both candidates fixed, then read each score row from left to right. In KL(p || q), target mass weights the penalty, so nearly missing the right mode makes the one-mode candidate expensive. In KL(q || p), candidate mass weights the penalty, so the broad candidate pays for its extra middle mass while the one-mode candidate scarcely samples the missed mode. This original three-outcome construction illustrates a restricted choice between two candidates, not a universal optimizer. Values are in nats and were checked against the KL definition in Kullback and Leibler (1951) and the inclusive/exclusive analysis in Minka (2005).

Connection to cross-entropy

For empirical distribution over a finite dataset:

Cross-entropy = entropy + KL. Since entropy of the data doesn’t depend on , minimizing cross-entropy = minimizing KL = MLE. This is why “the loss is cross-entropy” and “we’re minimizing KL to the data” are the same statement.

Common usage in ML

Use caseDirection
Classification cross-entropy lossForward
Variational inference (ELBO)Reverse
RLHF / PPO penaltyReverse . Keep new policy close to reference
Knowledge distillationForward with temperature
t-SNEForward on pairwise similarities

Common pitfalls

  • Computing KL between distributions with different supports. If and , KL is .
  • Confusing JS divergence (symmetric) with KL. GANs originally used JS; modern variants (Wasserstein) avoid both.
  • Forgetting the asymmetry direction. Forward and reverse KL produce qualitatively different optimizers.
  • Using KL on samples without density estimates. KL is between distributions, not between sample sets; sample-based estimators are noisy and biased.

Related: entropy, mutual information, and information gain, cross-entropy and softmax, and variational autoencoders.