Math & Statistics

Entropy, Cross-Entropy & KL Divergence

Information theory's core trilogy: measuring uncertainty, cross-distribution surprise, and probabilistic distance.

🟡 intermediate5 min readinformation-theory
Entropy H(P) measures the inherent uncertainty/impurity of a probability distribution P. Cross-Entropy H(P, Q) measures the expected code length when events from true distribution P are encoded using predicted model distribution Q. Kullback-Leibler (KL) Divergence D_KL(P || Q) = H(P, Q) - H(P) quantifies the extra surprise (relative entropy) from approximating P with Q. In ML, minimizing Cross-Entropy loss is identical to minimizing KL Divergence because true target entropy H(P) is constant.

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