Ran Wei/ Maths for AI and LLMs
中文
Maths for AI and LLMs · Information theory

Module 3Entropy, cross-entropy and KL divergence

Measure uncertainty under a distribution and the extra log loss incurred by predicting with another.

65 minYou need Module 1 and logarithms

In this module you will

  • Compute entropy and cross-entropy for finite distributions
  • Identify when a zero predicted probability gives infinite loss
  • Interpret KL divergence as the excess expected log loss
1

Surprise and entropy

An outcome with probability pi>0p_i>0 has surprise −log⁡pi.-\log p_i\mathclose{\text{.}} Use the natural logarithm throughout this series, so values are in nats. Use base two instead to measure bits. Entropy averages surprise under the true distribution p:p\mathclose{\text{:}}

H(p)=−∑i:pi>0piln⁡pi.H(p)=-\sum_{i:p_i>0}p_i\ln p_i.

A deterministic distribution has entropy zero. A uniform distribution on KK outcomes has entropy ln⁡K.\ln K\mathclose{\text{.}} Terms with zero weight contribute zero by continuity.

2

Prediction under a different distribution

A model qq assigns surprise −ln⁡qi.-\ln q_i\mathclose{\text{.}} Averaging over the data distribution gives H(p,q)=−∑i:pi>0piln⁡qi.H(p,q)=-\sum_{i:p_i>0}p_i\ln q_i\mathclose{\text{.}} If any qi=0q_i=0 with pi>0,p_i>0\mathclose{\text{,}} this loss is +∞.+\infty\mathclose{\text{.}} Subtracting the true entropy yields

DKL(p∥q)=∑i:pi>0piln⁡piqi=H(p,q)−H(p).D_{\rm KL}(p\|q)=\sum_{i:p_i>0}p_i\ln\frac{p_i}{q_i}=H(p,q)-H(p).

To see why it is nonnegative, apply −ln⁡x≥1−x-\ln x\ge 1-x to x=qi/pix=q_i/p_i on the positive support of p.p\mathclose{\text{.}} The resulting lower bound is 1−∑i:pi>0qi≥0.1-\sum_{i:p_i>0}q_i\ge0\mathclose{\text{.}} Equality requires p=q.p=q\mathclose{\text{.}} Swapping pp and qq changes which distribution weights each term, so KL is generally asymmetric.

Example 3.1Compare a fair model and a biased source✓ Checked

If p=(3/4,1/4)p=(3/4,1/4) and q=(1/2,1/2),q=(1/2,1/2)\mathclose{\text{,}} then H(p,q)=ln⁡2.H(p,q)=\ln2\mathclose{\text{.}} The source entropy is about 0.56230.5623 nats and the excess loss is about 0.13080.1308 nats.

Try it

Set the source weights to 3, 1, 0 and the predicted weights to 1, 1, 0. Select nats to reproduce the worked example. Then set the second predicted weight to zero and check which direction of KL divergence becomes infinite.

Compute itAverage surprise

Calculate the scores using the same units throughout.

Python
from math import log

p, q = (0.75, 0.25), (0.5, 0.5)
entropy = -sum(pi * log(pi) for pi in p if pi > 0)
cross_entropy = -sum(pi * log(qi) for pi, qi in zip(p, q) if pi > 0)
print(f"{entropy:.4f} {cross_entropy:.4f} {cross_entropy - entropy:.4f}")
Output
0.5623 0.6931 0.1308
3

Exercises

Exercise 3.1★Uniform source

Compute the entropy in nats for four equally probable tokens.

Show solution✓ Checked

Each token has surprise ln⁡4.\ln4\mathclose{\text{.}} Its average is ln⁡4.\ln4\mathclose{\text{.}}

Exercise 3.2★★A deterministic source

Compute the excess loss when p=(1,0)p=(1,0) and q=(1/4,3/4).q=(1/4,3/4)\mathclose{\text{.}}

Show solution✓ Checked

H(p)=0,H(p)=0\mathclose{\text{,}} and the only outcome that occurs incurs loss −ln⁡(1/4)=ln⁡4.-\ln(1/4)=\ln4\mathclose{\text{.}}

Exercise 3.3★★★Change the units

Convert the entropy of eight equally probable outcomes from nats to bits.

Show solution✓ Checked

Divide ln⁡8\ln8 by ln⁡2\ln2 to obtain 33 bits.

4

Check yourself

1
Which distribution supplies the weights in H(p,q)?H(p,q)\mathclose{\text{?}}
2
What if pi>0p_i>0 and qi=0?q_i=0\mathclose{\text{?}}
3
When is DKL(p∥q)=0?D_{\rm KL}(p\|q)=0\mathclose{\text{?}}
Finished this module?