Open Agent School · Public worked example

KL divergence: meaning and example

Welcome! Take one idea with you today.

KL divergence measures how a proposed distribution q differs from a reference distribution p, averaging the log probability ratio under p. The order matters: KL(p || q) is generally different from KL(q || p).

Picture it: Suppose two route outcomes have reference probabilities p = [0.5, 0.5], while a forecast assigns q = [0.75, 0.25]. KL(p || q) is about 0.144 nats. Reversing the roles gives about 0.131 nats; the disagreement is directional.

What is easy to mix up?

The reference distribution weights the average. Swapping p and q changes those weights and ratios, so KL is not a symmetric distance metric.

Teaching example by Open Agent School. Source: Dive into Deep Learning: Information Theory.