← All Posts
Machine Learning · Loss Functions · KL Divergence· Part 3

KL Divergence in ML Objectives

KL divergence appears whenever training says: "make these two probability distributions close." The distributions might be labels and predictions, a teacher and a student model, or a learned latent distribution and a fixed prior.

A useful translation is:

KL penalty = how much the current probabilities disagree with the reference probabilities

Classification

In classification, the target distribution says which class should receive probability. For a one-hot label, the correct class has probability $1$ and the others have $0$. Cross-entropy trains the model to put probability on that target class. This can be viewed as minimizing KL from the target distribution to the model prediction, ignoring a constant term that the model cannot change.

With soft labels, the target may look like $(0.7,0.2,0.1)$ instead of one-hot. Then KL/cross-entropy teaches the model to match all of those probabilities, not only the largest class.

Distillation

In knowledge distillation, a large teacher model produces a probability distribution. A smaller student model is trained to match it. KL is natural here because the goal is not just "pick the same top class." The goal is "make the student's whole probability vector look like the teacher's."

Variational Autoencoders

A variational autoencoder has a latent space. The model learns a distribution over latent variables for each input, but it also wants those latent distributions to stay close to a simple prior, often a standard normal distribution. The KL term acts like a regularizer that keeps the latent space organized enough for generation.

Policy Constraints

In reinforcement learning, a policy is a probability distribution over actions. A KL penalty can keep a new policy from moving too far away from the old policy in one update. This prevents a single update from making the agent's behavior suddenly unstable.

Summary

Whenever KL appears, first identify the two distributions and the direction. Then ask what would be bad: missing real events, drifting from a teacher, breaking a latent prior, or changing a policy too quickly. That usually explains why KL is in the objective.