Comparing two distributions
In machine learning, we constantly compare probability distributions. The model's predictions vs the true labels. The approximate posterior vs the true posterior. The student's policy vs the teacher's. Cross-entropy and KL divergence are the mathematical tools to quantify these gaps.
Cross-entropy
Definition
The cross-entropy between a "true" distribution and a "predicted" distribution is:
It measures the average number of bits needed to encode messages drawn from using the code optimized for .
Link with entropy
Cross-entropy is the entropy of plus the KL divergence from to . If (perfect predictions), cross-entropy is minimal and equals exactly .
The cross-entropy loss in classification
In multi-class classification, the labels are one-hot: with a 1 for the true class . The predictions are the softmax outputs: .
The cross-entropy simplifies to:
That's the loss everyone uses. The higher the probability assigned to the correct class, the lower the loss. If , the loss is 0. If , the loss tends toward infinity.
Why not MSE for classification?
MSE also works mathematically, but it has gradients that weaken when the model is very confident and wrong. Cross-entropy, on the other hand, produces strong gradients when the model assigns low probability to the correct class. It severely penalizes confident errors, which speeds up learning.
KL divergence
Definition
The Kullback-Leibler divergence measures how different is from :
It's the "extra cost" in bits when you use the code for to encode messages from , beyond the theoretical minimum .
Fundamental properties
- Non-negativity: , with equality if and only if
- Not a distance: KL divergence is not symmetric and doesn't satisfy the triangle inequality
- Not symmetric: in general, which has important practical consequences

Asymmetry has consequences
The non-symmetry of KL divergence is not a flaw: the two directions capture different behaviors.
(forward KL): mode-covering
Minimizing with respect to forces to cover the entire support of . Where , we must have too (otherwise the log diverges). Result: tends to be too broad, covering areas where is zero.
(reverse KL): mode-seeking
Minimizing with respect to allows to concentrate on a single mode of . If is multimodal (multiple peaks), can ignore some modes and lock onto just one. This is "mode-seeking" behavior.
Illustration
Imagine that is a bimodal distribution (two peaks). With forward KL, will be a broad Gaussian covering both peaks. With reverse KL, will be a narrow Gaussian centered on a single peak. This difference has concrete consequences on model behavior: a VAE (reverse KL) may "choose" a single mode, while the cross-entropy loss (forward KL) forces the model to cover the entire data distribution.

Applications in deep learning
The cross-entropy loss = minimizing KL
When you train a model with cross-entropy, you minimize:
Since is a constant (independent of ), minimizing cross-entropy is exactly equivalent to minimizing . You're looking for parameters such that the predictions are as close as possible to the true data distribution.
VAE: KL between posterior and prior
In a Variational Autoencoder, the objective (ELBO) contains a KL divergence term:
The first term is reconstruction. The second term forces the approximate posterior to stay close to the prior .
For Gaussians, the KL has a closed-form analytical formula:
That's why VAEs use Gaussians: the KL is computable exactly, no approximation needed.
Knowledge distillation
Distillation transfers knowledge from a large model (teacher) to a small model (student). You minimize:
where the distributions are softmax outputs with a high temperature. The student learns not only the correct class, but also the relationships between classes captured by the teacher's probabilities.
RLHF and PPO
In RLHF (Reinforcement Learning from Human Feedback), you adjust an LLM according to human preferences while staying close to the base model. The loss contains a term:
The KL term prevents the aligned model from straying too far from the reference model , which avoids mode collapse and maintains language diversity.
Choice of KL direction
| Application | KL direction | Reason |
|---|---|---|
| Standard training | Forward KL via cross-entropy | |
| Variational inference (VAE) | Reverse KL, tractable | |
| Distillation | Forward KL, cover the teacher | |
| RLHF | Penalize deviations from the base model |
Other divergences and distances
Jensen-Shannon divergence
The JS divergence is the symmetric version of KL:
It's symmetric, bounded between 0 and , and its square root is a true distance. It's the divergence used in the original GAN objective.
Wasserstein distance
Also called the Earth Mover's Distance, it measures the "transportation cost" to transform into . Used in Wasserstein GANs (WGAN) to solve the training problems of classic GANs.
Key takeaways
- Cross-entropy is the standard classification loss
- KL divergence measures the "extra cost" of using instead of for encoding
- KL is not symmetric: forward KL covers everything, reverse KL concentrates
- Training with cross-entropy = minimizing the forward KL between data and predictions
- KL appears in VAEs, distillation, and RLHF with different roles
- For Gaussians, KL has an analytical form, which simplifies calculations
Where this is used. The KL penalty is the guardrail that keeps a model from drifting during alignment: see RLHF in practice.