KL Divergence
made visual
Kullback–Leibler divergence — no scary math. Just two distributions,
and how far apart they are.
What even is a distribution?
Before KL Loss, let's get comfortable with distributions. A probability distribution tells you how likely each outcome is. Think of it as a "belief" about what will happen.
Below are two distributions — P (the true one) and Q (our guess). Drag the sliders and watch them shift.
When P and Q overlap perfectly, they're identical. When they drift apart — that gap is exactly what KL divergence measures.
What KL divergence actually measures
Kullback–Leibler divergence (KL) answers: "How much extra information do I waste if I use Q to describe a world that's actually P?"
Three things to notice:
Let's compute it ourselves
Hit Random to shuffle two full distributions over Rain, Sunny, and Cloudy. P is truth; Q is the forecast.
What if the loss is KL(P ∥ Q) or KL(Q ∥ P)?
Same two distributions — but the direction flips who weights the sum and which ratio you take. In training, P is frozen (truth / parent). Only Q, the student, gets updated.
Purple = P (frozen) · Green = Q (student updating each step)
Forward · KL(P ∥ Q)
- P weights the sum · ratio P/Q
- Q spreads to cover all modes — blurry but inclusive
- Watch left panel: Q widens across both peaks
Reverse · KL(Q ∥ P)
- Q weights the sum · ratio Q/P
- Q locks onto one mode — sharp, mode-seeking
- Watch right panel: Q snaps to a single peak
Two different questions — RLHF picks one
Forward and reverse KL are not just swapped formulas. Each one asks a different question about frozen P and trained Q. In RLHF, we care far more about the reverse one.
Forward KL(P ∥ Q)
"If P is truth, where is Q missing mass that P cares about?"
Expectation under P · punishes Q for being too low where P is high · pushes Q to cover every mode
Reverse KL(Q ∥ P)
"Where Q actually puts mass, is it saying things P would not?"
Expectation under Q · punishes Q for being high where P is low · keeps the student on a leash
Why reverse KL is what we use
- P stays frozen — the SFT parent we trust. Q is trained with reward signals on its own outputs.
- We only sample from Q during RL — reverse KL weights exactly those tokens, asking whether the student is drifting off-policy.
- Forward KL would force Q to spread across all of P's modes — useful in VI, but too blurry for coherent language.
- Reverse KL is the right guardrail: improve from reward, but stay close to the parent. Hence reward − β · KL(Q ∥ P).
Reverse KL on every token the student writes
Ask "What is the capital of France?" — the student replies below. Click any word (or use ← →) to see how far Q drifted from frozen parent P at that step.
In PPO, we sum KL(Q∥P) across every token the student actually writes.