Loss Functions › KL Divergence
Language AI HandbookAll chapters 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 differs from a reference distribution . 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.
where:
- : the probability of event under the reference distribution
- : the probability of event under the approximate distribution
- : the log ratio, measuring how much deviates from for event
KL divergence is non-negative (by Jensen's inequality) and equals zero if and only if . Importantly, it is asymmetric: . When is the true distribution, penalizes cases where assigns low probability to events that considers likely. This is the relevant direction for model training.
The relationship between cross-entropy and KL divergence is:
where:
- : the entropy of the true distribution , a constant with respect to model parameters
- : the KL divergence between and
Since 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 : 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. is large when assigns low probability to events that considers common (mode-seeking behavior). is large when spreads probability mass over regions where 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 ) is preferred over the reverse direction.