Entropy, Cross-Entropy & KL Divergence

Uncertainty, surprise and the gap between two distributions, all in one family.

🟡 intermediate5 min readinformation-theory
Entropy measures how unpredictable something is: a fair coin has high entropy, a coin that always lands heads has none. Cross-entropy measures how surprised your model is, on average, by what actually happens, so it is low when the model gives high probability to the right answers. KL divergence is the extra surprise caused by using your model instead of the truth. In training the true labels never change, so minimising cross-entropy and minimising KL divergence are exactly the same thing.

The Trilogy Defined

Given true distribution P and predicted distribution Q:

1. Shannon Entropy H(P)

Measures average information (surprise / uncertainty) in P:

H(P) = - ∑ P(x) log P(x)

2. Cross-Entropy H(P, Q)

Expected cost of encoding events from true P using model Q:

H(P, Q) = - ∑ P(x) log Q(x)

In classification, for one-hot label P where P(y=c)=1 and P(y≠c)=0: H(P, Q) = - log Q(y=c).

3. KL Divergence D_KL(P || Q)

Extra bits needed when encoding P using Q (relative entropy):

D_KL(P || Q) = ∑ P(x) log( P(x) / Q(x) )  =  H(P, Q) - H(P)
   Cross-Entropy H(P, Q)  =  True Entropy H(P)  +  KL Divergence D_KL(P || Q)
  (What loss optimizes)       (Fixed constant)       (Distance to truth)

Key Properties of KL Divergence

Applications in Deep Learning

  1. Classification Loss: Minimizing Cross-Entropy loss pushes model distribution Q toward true target distribution P.
  2. Variational Autoencoders (VAEs): Loss combines reconstruction MSE with KL Divergence D_KL( q(z|x) || N(0,I) ) to regularize latent space.
  3. RLHF / PPO in LLMs: Adds KL penalty D_KL( π_RL || π_SFT ) to prevent fine-tuned policy LLM from drifting too far from base SFT model.

Say this out loud

"Entropy H(P) measures inherent uncertainty in a distribution. Cross-Entropy H(P,Q) measures the surprise of observing true distribution P under model distribution Q. KL Divergence D_KL(P||Q) = H(P,Q) - H(P) measures the non-symmetric information loss when approximating P with Q. Because H(P) is constant for fixed ground-truth labels, minimizing Cross-Entropy loss is mathematically identical to minimizing KL Divergence."

Follow-ups to expect

Check yourself

Question 1 of 3

Why is minimizing Binary Cross-Entropy loss in classification equivalent to minimizing KL Divergence D_KL(P || Q)?

More in Math & Statistics

See all →
Bayes’ Theorem4 minCentral Limit Theorem4 minLaw of Large Numbers4 min