Equations · Kullback and Leibler (1951)
The KL divergence formula, explained
How much one probability distribution differs from another: the expected extra log-loss from using when the data really follow .
Term by term
The divergence of from . The separates the arguments; the order matters.
An average over outcomes , weighted by : an expectation under . For continuous variables, an integral.
A small worked example
, : nats. Reversed, .
In code
import numpy as np
def kl(p, q):
return np.sum(p * np.log(p / q))Every symbol in it
Other equations
- Scaled dot-product attention
- Softmax
- Cross-entropy loss
- Adam update
- Layer normalization
- RMSNorm
- The ELBO
- Backpropagation through one layer
- LoRA
- GELU
- Dropout
- Batch normalization
- Rotary position embedding (RoPE)
- Multi-head attention
- PPO clipped objective
- DPO loss
- Policy gradient (REINFORCE)
- Bellman equation
- Q-learning update
- InfoNCE contrastive loss
- Diffusion forward process
- DDPM simplified loss
- VAE reparameterization trick
- GAN minimax objective
- AdamW
- Cosine learning-rate schedule
- Perplexity
- Logistic regression
- Normal equation (linear regression)
- Singular value decomposition (SVD)
- Principal component analysis (PCA)