Skip to content
BIASED

Entropy, cross-entropy and KL

Block 1 of 7~2 min

Surprise

The topic What generative modeling is left us one quantity computable from samples alone, the average log-likelihood, and Multivariate Gaussians taught us to sample noise and compute a log-density. To fit a model distribution QQ to a target distribution PP we still need a number that says how far QQ is from PP. The standard first choice is the Kullback–Leibler divergence. It is built from a simpler idea: how surprised we are by an outcome.

Take two boards with ten marks each. Board A has 5 noughts and 5 crosses; board B has 9 noughts and 1 cross. Drawing an X is clearly more surprising on board B, where P(X)=110P(\text{X}) = \tfrac1{10}, than on board A, where P(X)=12P(\text{X}) = \tfrac12. So surprise must grow as the probability falls.

A first guess, 1/P(x)1/P(x), ranks the boards correctly but fails on a certain event: a coin that always lands heads gives 1/P(H)=11/P(H) = 1, although heads should not surprise us at all. Taking the logarithm fixes this. The surprise, or self-information, of an outcome is

s(x)=log⁡1P(x)=−log⁡P(x).s(x) = \log\frac{1}{P(x)} = -\log P(x).

It equals 00 when P(x)=1P(x) = 1 and grows without bound as P(x)→0P(x) \to 0.

The logarithm also makes surprise additive. For independent events the probability of a sequence is a product, and the logarithm of a product is a sum, so the surprise of a sequence is the sum of the surprises of its parts.

Units. log⁡2\log_2 gives bits and ln⁡\ln gives nats, with 11 bit =ln⁡2≈0.693= \ln 2 \approx 0.693 nats. One fair binary choice carries exactly 11 bit. The base never changes a comparison, only the unit; a plain log⁡\log means either, as long as one base is used throughout.

Answers are saved on the server and tied to this browser until you sign in.