What is KL divergence and how does it relate to cross-entropy?
Written by the mlprep team · Updated
What is KL divergence? How does it relate to cross-entropy? Where do you see it used in ML?
KL divergence defined#
KL divergence measures how much distribution q differs from reference distribution p:
Which gives the fundamental relationship:
Cross-entropy = entropy of p + KL divergence from p to q. When p is fixed (training labels), minimizing cross-entropy is identical to minimizing KL(p||q).
It is not a true distance#
KL(p||q) ≠ KL(q||p) in general. It's also not a metric (no triangle inequality). This asymmetry matters:
-
KL(p||q) — "forward KL", used in maximum likelihood / standard CE training. Where p > 0 and q ≈ 0, you get infinite penalty. The model is forced to cover all modes of p. Called mean-seeking — q spreads out to cover p.
-
KL(q||p) — "reverse KL", used in variational inference. Where q > 0 and p ≈ 0, you get infinity. The model avoids placing mass where p is low. Called mode-seeking — q concentrates on one mode of p.
Where KL divergence appears in ML#
Variational Autoencoders (VAEs): The ELBO objective is:
The KL term regularizes the encoder's learned posterior toward the prior p(z) = N(0, I). It's the "regularization" that keeps the latent space structured.
Knowledge distillation: The student minimizes KL(teacher_softmax || student_softmax) — forward KL, forcing the student to match the teacher's full distribution over classes, not just the top-1.
Reinforcement learning from human feedback (RLHF): KL penalty between the fine-tuned policy and reference policy prevents the model from reward-hacking by drifting too far from the base distribution.
Information bottleneck: KL(q(z|x) || p(z)) measures how much information z retains about x.
KL vs Jensen-Shannon divergence#
JS divergence: JS(p||q) = ½·KL(p||m) + ½·KL(q||m) where m = ½(p+q). Symmetric, bounded between 0 and log(2). Used in GANs — the original GAN objective implicitly minimizes JS divergence between real and generated distributions.
The key takeaway unlocks after you reveal an expert answer.
Key Takeaway
KL(p||q) = H(p,q) - H(p): the extra bits you pay for using q when p is true. Not symmetric. Forward KL (CE training) is mean-seeking — model covers all modes. Reverse KL (variational inference) is mode-seeking — model picks one mode. Appears in VAEs (regularize latent space), RLHF (policy constraint), and distillation (match teacher's full distribution).
Follow-up Questions to Test Yourself
Interviewers often pivot here once you cover the core concept.
- In a VAE, what goes wrong if you remove the KL term? What goes wrong if the KL term dominates?
- Why is forward KL used for supervised learning and reverse KL common in variational inference?
- How does Jensen-Shannon divergence differ from KL, and why was it preferred in the original GAN objective?
How confident do you feel explaining this?
Track your understanding or flag it to revisit before your next loop.
Related Questions
Explain cross-entropy loss — binary vs categorical, and why it works for classification
What is focal loss and when would you use it over cross-entropy?
Compare MSE, MAE, and Huber loss — when would you use each?
Implement softmax and cross-entropy loss from scratch