Volume I, Chapter 3 — Part IV. Kullback-Leibler divergence from cross-entropy and entropy, Gibbs' inequality proof, asymmetry and forward vs reverse KL, multivariate Gaussian closed form, and applications in VAEs, diffusion models, and RLHF.
p(x),q(x) — Probability densities or mass functions
EP[⋅] — Expectation under distribution P
Core Intuition
How different are two probability distributions P and Q? The Kullback–Leibler (KL) divergence answers: If data are generated from P, how many extra bits per sample do we waste by encoding them with a code optimized for Q?
KL divergence is not a distance in the geometric sense — it is asymmetric (DKL(P∥Q)=DKL(Q∥P) in general) and does not satisfy the triangle inequality. Nevertheless, it is the canonical measure of distribution mismatch in machine learning:
Training classifiers minimizes DKL(Pdata∥Pmodel) via cross-entropy.
VAEs regularize the encoder with DKL(q(z∣x)∥p(z)).
RLHF constrains policy updates with DKL(πθ∥πref).
Diffusion model variational bounds decompose into sums of Gaussian KL terms.
This chapter derives KL divergence rigorously, proves its key properties, and connects it to the loss functions used throughout modern AI.
Key: KL is asymmetric — KL(P‖Q) ≠ KL(Q‖P). Make them identical to see KL→0. In VAE/VI, we minimize KL(q‖p) to approximate the posterior.
Cross-Entropy and the Definition of KL Divergence
Definition 1 (Cross-Entropy — Discrete). For distributions P and Q on countable X:
H(P,Q)=−x∈X∑P(x)logQ(x).(1)
Definition 2 (Entropy — Discrete).
H(P)=−x∑P(x)logP(x).(2)
Definition 3 (KL Divergence). The Kullback–Leibler divergence from Q to P (equivalently, of P from Q) is:
DKL(P∥Q)=H(P,Q)−H(P)=x∈X∑P(x)logQ(x)P(x).(3)
Convention. When P(x)=0, the term P(x)logQ(x)P(x)=0. When P(x)>0 and Q(x)=0, the term is +∞ (divergence is infinite if Q fails to cover the support of P).
Definition 4 (KL Divergence — Continuous). For PDFs p and q:
DKL(p∥q)=∫p(x)logq(x)p(x)dx.(4)
Interpretation.DKL(P∥Q) is the expected log-likelihood ratioEx∼P[logP(x)−logQ(x)] — how much more (or less) log-probability P assigns to typical samples from P compared to Q.
Step-by-Step Derivation
We derive (3) from (1) and (2) with no gaps.
Step 1. Write cross-entropy:
H(P,Q)=−x∑P(x)logQ(x).(5)
Step 2. Write entropy:
H(P)=−x∑P(x)logP(x).(6)
Step 3. Subtract (6) from (5):
H(P,Q)−H(P)=−x∑P(x)logQ(x)+x∑P(x)logP(x).(7)
Step 4. Combine the sums (same index set):
=x∑P(x)[logP(x)−logQ(x)]=x∑P(x)logQ(x)P(x).(8)
Step 5. By Definition 3, this equals DKL(P∥Q). ■
Remark. The derivation requires only algebra and the definitions — no additional assumptions.
Gibbs' Inequality: Proof of Non-Negativity
Theorem 1 (Gibbs' Inequality).For discrete P and Q:
DKL(P∥Q)≥0,(9)
with equality if and only if P(x)=Q(x) for all x with P(x)>0.
Equality condition. Jensen's inequality is tight iff Z=Q(x)/P(x) is constant for all x in the support of P. Since both P and Q sum to 1, the constant must be 1, so P(x)=Q(x) wherever P(x)>0. ■
Theorem 2 (Continuous Case).For PDFs p,q with p absolutely continuous w.r.t. q:
DKL(p∥q)=∫p(x)logq(x)p(x)dx≥0,(13)
with the same equality condition (a.e.).
Proof. Same Jensen argument with the integral in place of the sum. ■
where the second term is Ex∼P(X)[DKL(P(Y∣X=x)∥Q(Y∣X=x))].
Property 6 (Data processing / Invariance). If Y=f(X) deterministically, then DKL(P(Y)∥Q(Y))≤DKL(P(X)∥Q(X)) — processing cannot increase divergence (information processing inequality).
Property 7 (Additivity for independent products). If P=P1×P2 and Q=Q1×Q2, then:
DKL(P∥Q)=DKL(P1∥Q1)+DKL(P2∥Q2).(17)
Forward KL vs Reverse KL
The asymmetry of KL divergence leads to qualitatively different behavior when used as an optimization objective.
Forward KL: DKL(P∥Q) — Mean-Seeking
Minimize over Q (approximation) with P (target) fixed:
QminDKL(P∥Q)=Qminx∑P(x)logQ(x)P(x).(18)
Behavior.
If P(x)>0 and Q(x)→0, then logQ(x)P(x)→+∞. So Q(x)>0 wherever P(x)>0 — Q must cover all modes of P.
Q tends to be broader than P (overdispersed) to avoid missing any mass.
Used in expectation propagation, variational inference (ELBO), and MLE (via cross-entropy).
Reverse KL: DKL(Q∥P) — Mode-Seeking
Minimize over Q with P fixed:
QminDKL(Q∥P)=Qminx∑Q(x)logP(x)Q(x).(19)
Behavior.
If Q(x)>0 and P(x)=0, then logP(x)Q(x)→+∞. So Q(x)=0 wherever P(x)=0 — Q is supported only where P is positive.
Q can ignore modes of P and concentrate on one.
Used in RLHF (policy vs reference), some GAN objectives, and mode-seeking variational approximations.
Proposition 1.For multimodal P, the forward KL minimizer argminQDKL(P∥Q) typically covers all modes; the reverse KL minimizer often selects a single mode.
KL Divergence and Maximum Likelihood
Theorem 3 (MLE as KL Minimization).Let p^n(x)=n1∑i=1nδ(x−xi) be the empirical distribution. Then:
Proof sketch. Use logp(x)=−2dlog(2π)−21log∣Σ1∣−21(x−μ1)TΣ1−1(x−μ1) and take expectation under p, using E[(x−μ1)(x−μ1)T]=Σ1 and the trace trick for cross-terms. See The Multivariate Gaussian, equation (33).
Special case (Standard Normal Prior). For p=N(μ,Σ) and q=N(0,I):
DKL(p∥q)=21[−log∣Σ∣−d+tr(Σ)+μTμ].(29)
This is the VAE regularization term when the prior is standard normal.
Each term is a Gaussian KL (Theorem 5) — tractable closed form.
RLHF and PPO
L=−Ex∼πθ[R(x)]+β⋅DKL(πθ∥πref).(35)
Reverse KL (or symmetrized variants) prevents the policy from drifting far from the reference LLM, avoiding reward hacking.
Variational Inference
Approximating posterior p(θ∣D) with q(θ) by minimizing DKL(q∥p(θ∣D)) (reverse KL, mode-seeking) or maximizing ELBO equivalent to minimizing DKL(q∥p(θ∣D)) — see Variational Inference.
Common Pitfalls and Misconceptions
Pitfall 1: Calling KL a "distance." It is a divergence or premetric, not a metric — asymmetry and triangle inequality failure matter for interpretation.
Pitfall 2: Swapping argument order.DKL(P∥Q) and DKL(Q∥P) optimize differently (mean-seeking vs mode-seeking). VAEs use forward KL in ELBO; RLHF often uses reverse KL.
Pitfall 3: Infinite KL from support mismatch. If Q(x)=0 where P(x)>0, forward KL is +∞. Ensure model support covers data support (softmax over all classes, positive variance in Gaussians).
Pitfall 4: Confusing KL with JS divergence. Jensen–Shannon divergence JS(P∥Q)=21DKL(P∥M)+21DKL(Q∥M) with M=21(P+Q) is symmetric and bounded — different tradeoffs.
Pitfall 5: Ignoring that cross-entropy offset differs from KL scale.H(P,Q)=H(P)+DKL(P∥Q); training logs cross-entropy, but generalization depends on KL to the true P, not H(P) alone.
Research Perspective
Kullback and Leibler (1951) introduced DKL in the context of sufficient statistics and hypothesis testing. It became central to information theory (I-divergence, Csiszár) and statistics (Bahadur efficiency, large deviations).
In machine learning, KL appears in the EM algorithm (E-step as KL minimization), variational Bayes (Jordan et al., 1999), VAEs (Kingma & Welling, 2014), and modern alignment (RLHF, Ouyang et al., 2022). Research on f-divergences (generalizing KL), Wasserstein distances (true metrics for generative modeling), and rate-distortion theory continues to refine how we measure distribution mismatch.
Open questions include: optimal divergence choice for generative models, tight KL-based generalization bounds for deep networks, and whether reverse KL penalties in RLHF optimally balance exploration and safety.
Summary of Takeaways
KL definition — ∑P(x)logQ(x)P(x) — Distribution mismatch
Cross-entropy relation — DKL(P—Q)=H(P,Q)−H(P) — Training loss link