KL Divergence

1 min read

KL divergence measures how one probability distribution QQ diverges from a reference distribution PP:

DKL(PQ)=xP(x)logP(x)Q(x)=ExP[logP(x)Q(x)]D_{\text{KL}}(P \| Q) = \sum_x P(x) \log\frac{P(x)}{Q(x)} = \mathbb{E}_{x \sim P}\left[\log\frac{P(x)}{Q(x)}\right]

Key properties:

  • DKL(PQ)0D_{\text{KL}}(P \| Q) \geq 0 (Gibbs' inequality), with equality iff P=QP = Q
  • Not symmetric: DKL(PQ)DKL(QP)D_{\text{KL}}(P \| Q) \neq D_{\text{KL}}(Q \| P) — so it's not a true distance
  • Forward KL DKL(PQ)D_{\text{KL}}(P \| Q): QQ must cover all of PP's support → mean-seeking
  • Reverse KL DKL(QP)D_{\text{KL}}(Q \| P): QQ avoids regions where PP is small → mode-seeking

Where it appears:

  • Cross-entropy: H(P,Q)=H(P)+DKL(PQ)H(P, Q) = H(P) + D_{\text{KL}}(P \| Q). Minimizing cross-entropy = minimizing KL from true distribution
  • RLHF Pipeline — KL penalty DKL(πθπref)D_{\text{KL}}(\pi_\theta \| \pi_{\text{ref}}) prevents the policy from drifting too far from the reference model → KL Penalty
  • VAEs — KL term regularizes the latent distribution toward the prior
  • Information theory — KL is the extra bits needed when using code optimized for QQ but the true distribution is PP

See also: Maximum Likelihood Estimation, Loss Functions

Linked from