Loss Functions › KL Divergence

Michael BrenndoerferMay 2, 20252 min read
Language AI HandbookAll chapters
Part 10 of 67Neural Network Foundations
Chapter 64Loss Functions › KL Divergence
Reading section 7 of 17 · KL Divergence · 2 minSection 7 of 17 · 2 min

Book contents

Section 7 of 17 · 2 min read

Loss Functions › KL DivergenceOne section · Sections in this chapter7 / 17 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Kullback-Leibler (KL) divergence measures how much one probability distribution qq differs from a reference distribution pp. In neural network training, it appears when we want the model's output distribution to match a target distribution, not just predict a single class.

DKL(p∥q)=∑k=1Kpklog⁡pkqkD_{\text{KL}}(p \| q) = \sum_{k=1}^{K} p_k \log \frac{p_k}{q_k}

where:

  • pkp_k: the probability of event kk under the reference distribution pp
  • qkq_k: the probability of event kk under the approximate distribution qq
  • log⁡pkqk\log \frac{p_k}{q_k}: the log ratio, measuring how much qq deviates from pp for event kk

KL divergence is non-negative (by Jensen's inequality) and equals zero if and only if p=qp = q. Importantly, it is asymmetric: DKL(p∥q)≠DKL(q∥p)D_{\text{KL}}(p \| q) \neq D_{\text{KL}}(q \| p). When pp is the true distribution, DKL(p∥q)D_{\text{KL}}(p \| q) penalizes cases where qq assigns low probability to events that pp considers likely. This is the relevant direction for model training.

The relationship between cross-entropy and KL divergence is:

LCE(p,q)=H(p)+DKL(p∥q)\mathcal{L}_{\text{CE}}(p, q) = H(p) + D_{\text{KL}}(p \| q)

where:

  • H(p)=−∑kpklog⁡pkH(p) = -\sum_k p_k \log p_k: the entropy of the true distribution pp, a constant with respect to model parameters
  • DKL(p∥q)D_{\text{KL}}(p \| q): the KL divergence between pp and qq

Since H(p)H(p) is constant during training, minimizing cross-entropy is equivalent to minimizing KL divergence. This connection explains why cross-entropy is the correct loss for maximum likelihood estimation: it minimizes the information gap between the model's distribution and the true distribution.

KL divergence appears explicitly in variational autoencoders (VAEs) as a regularization term, and in knowledge distillation, where it is used to train a student model to match a teacher model's output distribution. In distillation, the student is trained to minimize DKL(pteacher∥pstudent)D_{\text{KL}}(p_{\text{teacher}} \| p_{\text{student}}): the teacher's soft probability distribution provides richer training signal than one-hot labels, because it encodes the teacher's beliefs about how similar various classes are to each other.

The asymmetry of KL divergence has practical consequences. DKL(p∥q)D_{\text{KL}}(p \| q) is large when qq assigns low probability to events that pp considers common (mode-seeking behavior). DKL(q∥p)D_{\text{KL}}(q \| p) is large when qq spreads probability mass over regions where pp has low probability (mean-seeking behavior). For model training, you generally want mode-seeking behavior: learn to correctly handle the common cases well. This is why cross-entropy (which minimizes DKL(pdata∥pmodel)D_{\text{KL}}(p_{\text{data}} \| p_{\text{model}})) is preferred over the reverse direction.