Skip to content
mlmentorship

KL divergence: which distribution weights the error?

Score fixed candidates in both directions, derive the cross-entropy identity, and test support and approximation-family changes.

Published · 15 min read ·Core ·Foundation
Reading depth

Summary

KL divergence averages a log probability ratio under its first distribution. Swapping the arguments changes that weighting, so the same candidates can receive opposite rankings. Start with expected surprisal, not a distance analogy.

Task: compare only two candidates for a three-outcome target:

Each vector sums to one. Use natural logarithms, giving scores in nats:

Properties

Terms with contribute zero, including when . If but , the divergence is infinite. Otherwise finite probability vectors give a nonnegative score, zero exactly when the distributions agree. KL is not symmetric and does not satisfy metric guarantees such as the triangle inequality.

Forward vs. reverse KL

Hold the target and both candidates fixed while changing only the sampling weights.

Learning objective

Changing which distribution does the averaging reverses this candidate ranking

The averaging distribution reverses this candidate rankingTarget P=(.9,.1): forward KL prefers Q1=(.7,.3); reverse KL prefers Q2=(.99,.01). Both panels use the same scale in nats; lower is better.Changing the averaging population canreverse rankAverage under P: candidate Q1KL 0.1163Average under P: candidate Q2KL 0.1445Average under Q1: candidate Q1KL 0.1537Average under Q2: candidate Q2KL 0.07133Forward keeps P fixed. Reverseaverages each Q.
Derivation
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 compare the score rows. Forward KL weights errors by target mass; nearly missing the right mode is expensive. Reverse KL weights errors by candidate mass; the broad candidate pays for its extra middle mass. This is a restricted choice between two candidates, not a universal optimizer. Values are in nats, following Kullback and Leibler (1951). For divergence direction, see Minka (2005).
Check: Why does changing the expectation's weights reverse the preferred candidate without changing either candidate distribution?
Read it this way: Target P=(.9,.1): forward KL prefers Q1=(.7,.3); reverse KL prefers Q2=(.99,.01). Both panels use the same scale in nats; lower is better.
Which distribution supplies the averaging weights in reverse KL?

Connection to cross-entropy

Combining equal terms gives four complete expectations:

ExpectationWeighted log termsScore, nats

Forward KL prefers cover; reverse KL prefers left among these candidates. Reversing KL is not negating a forward score: it also changes the weights. For fixed discrete , add and subtract :

Only stays constant when optimizing . Categorical log-loss minimization and forward-KL minimization therefore have the same optimizer over the chosen model family.

Remove candidate support

New task: replace the left candidate with . Picture and change: the target’s final outcome still has mass , but the candidate now assigns zero. Check: forward KL becomes infinite; reverse KL remains nats. Use when: truncation or exact zeros remove support; exchanging arguments changes the requirement.

Expand the candidate set

New task: permit as another candidate. Every log ratio is now zero for that candidate. Check: both objectives attain zero at , beating the original choices. Use when: a richer approximation family becomes available; reverse KL does not inevitably select one mode.

Failure and transfer

For and , the wrong average is . The actual is . Wrong sampling weights changed the quantity, not the nonnegativity theorem.

Reconstruct: fixed has entropy nats and cross-entropy nats. What is KL?

Answer

It is nats. Minimizing cross-entropy over also minimizes forward KL only because this , and hence its entropy, stays fixed.

Why the divergence is nonnegative

For positive , . Apply this with wherever :

Equality requires on the positive support and no remaining candidate mass outside it. Zero candidate probability at positive target mass instead gives infinite KL immediately. The coding interpretation compares expected ideal code lengths under the same sampling law; base-two logarithms give bits rather than nats.

Continuous laws and empirical samples

For general measures, finite KL requires absolute continuity and suitable integrability. For densities, use against the common base measure. An atomic empirical measure has infinite KL to a continuous law assigning zero probability to every observed point. Empirical negative log density remains meaningful; it is not that literal finite empirical-to-continuous KL identity.

Common usage in ML

Use caseDirection
Classification cross-entropy lossForward
Variational inferenceApproximate posterior versus model posterior
Policy regularizationName both policies, expectation, and penalty direction explicitly
Knowledge distillationTeacher versus student distributions at the specified temperature
t-SNEHigh-dimensional versus low-dimensional pairwise probability distributions

PPO’s clipped surrogate is not generally an exact KL constraint. Information bottleneck objectives also use distinct joint, marginal, and conditional laws; name them before applying an identity.

Common pitfalls

Jensen-Shannon divergence averages and with . That symmetric bounded quantity is not KL with swapped arguments or a universal GAN objective. Finite-sample density estimates introduce their own support choices and estimation bias. Changing the feasible family can matter as much as divergence direction.

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