The KL divergence is a quantity that expresses how different two probability distributions and are as a single non-negative number. It is 0 when and are identical and grows as they differ. In deep learning it is used as a loss function for pulling a model's distribution toward the true (or target) distribution . The name comes from Kullback and Leibler, who introduced the quantity in a 1951 paper; in information theory it is also called relative entropy.
For continuous distributions, the sum is replaced by an integral and probabilities by probability densities. The log is the natural logarithm.
It means "the average extra surprise that someone who believes feels, compared with someone who knows , when observing data drawn from ." For example, in a town where the weather is really 70% sunny and 30% rainy, a person who assumes 50% sunny and 50% rainy is slightly more surprised on sunny days than someone who knows the true probabilities, and slightly less surprised on rainy days. This "extra surprise" averaged over the true frequencies is the KL divergence.
The figure at the top shows its value for a distribution over two outcomes, with the target distribution fixed at and the predicted distribution varied.
- It is 0 only when : At , both curves are 0.
- Swapping the direction changes the value: At , and .
- It grows without bound when puts nearly zero probability where says something happens: As approaches 0 or 1, the solid curve keeps rising.

