AI State of the Art
Math/Information Theory/11 min
Section 334

Entropy: Measuring Uncertainty

Listen as a podcast β†’

Entropy: a universal measure of uncertainty

When a language model predicts the next word, some contexts make the prediction easy ("The sky is...") and others make it hard ("She decided to..."). Entropy quantifies exactly this difficulty: it's the mathematical measure of the uncertainty of a probability distribution. The greater the uncertainty, the higher the entropy.

Shannon's formula

Definition

For a discrete random variable XX with distribution PP, Shannon entropy is:

H(X)=βˆ’βˆ‘xP(x)log⁑P(x)H(X) = -\sum_{x} P(x) \log P(x)

By convention, 0log⁑0=00 \log 0 = 0 (the limit as pβ†’0p \to 0).

If the logarithm is base 2, entropy is in bits. If it's the natural logarithm, it's in nats. In ML, we generally use the natural log in calculations, but we often reason in bits for intuition.

Why this formula?

Shannon sought the only function that satisfies three natural properties:

  • Continuity: small changes in probabilities produce small changes in entropy
  • Maximum for uniform: uncertainty is maximized when all outcomes are equally likely
  • Additivity: the entropy of independent events adds up

The formula H=βˆ’βˆ‘plog⁑pH = -\sum p \log p is the unique function (up to a multiplicative constant) that satisfies these three properties.

Intuition: how many bits to encode?

Entropy measures the minimum average number of bits needed to encode a message drawn from distribution PP.

Let's take an example. An alphabet of 4 symbols:

  • Case 1: all equally likely (p=1/4p = 1/4 each). H=log⁑24=2H = \log_2 4 = 2 bits. You need 2 bits per symbol (00, 01, 10, 11).
  • Case 2: P=(1/2,1/4,1/8,1/8)P = (1/2, 1/4, 1/8, 1/8). H=1.75H = 1.75 bits. You can do better than a fixed-length code: assign a short code ("0") to the frequent symbol and longer codes to rare symbols.

Entropy says: you can't compress below HH bits per symbol on average. It's the theoretical limit of compression.

Huffman tree built on the letters of the phrase bibbity bobbity: each leaf carries the frequency of its symbol (6/15 for b, 3/15 for i, 1/15 for o) and each edge is labelled 0 or 1, so a symbol code is read along the path from the root.
Huffman coding puts the entropy bound into practice. The most frequent symbol (b, 6 times out of 15) sits at the top of the tree and needs a single bit, while the rare symbols (o and the space, 1 time out of 15) sit at the bottom and cost four bits. This is exactly the strategy of case 2 above: short codes for likely symbols, long codes for rare ones.Source : The Arcane Algorithm Archive, Huffman encoding chapter

Language models are compressors

A language model learns the distribution P(next token∣context)P(\text{next token} | \text{context}). A good model assigns high probability to tokens that actually appear, which corresponds to low average surprise, hence low cross-entropy. In other words: a good language model compresses text well.

This connection between prediction and compression is deep. Every good predictor is a good compressor, and vice versa.

Extreme cases

Maximum entropy: the uniform distribution

When all KK outcomes are equally likely (P(x)=1/KP(x) = 1/K for all xx):

Hmax⁑=log⁑KH_{\max} = \log K

This is the case of greatest uncertainty. For a balanced 6-sided die: H=log⁑26β‰ˆ2.58H = \log_2 6 \approx 2.58 bits. For an LLM vocabulary of 50,000 tokens: Hmax⁑=log⁑250000β‰ˆ15.6H_{\max} = \log_2 50000 \approx 15.6 bits.

Minimum entropy: certainty

When one outcome has probability 1 (the others are 0):

H=βˆ’1β‹…log⁑1=0H = -1 \cdot \log 1 = 0

Entropy is zero: there is no uncertainty. You know exactly what will happen. No bits are needed to encode a message whose content you already know.

In between

For a Bernoulli with parameter pp:

H(p)=βˆ’plog⁑pβˆ’(1βˆ’p)log⁑(1βˆ’p)H(p) = -p \log p - (1-p) \log (1-p)

Entropy is maximized at p=0.5p = 0.5 (fair coin flip: 1 bit) and tends toward 0 as p→0p \to 0 or p→1p \to 1.

Binary entropy curve as a function of the probability p, a symmetric bell shape: it is 0 at both ends p equals 0 and p equals 1, and reaches its maximum of about 0.69 nats in the middle, marked by a dashed vertical line at p equals 0.5.
The whole intuition behind entropy is in this curve. At both edges, a biased coin that always lands the same way never surprises you: entropy is zero. In the middle, the fair coin is the most unpredictable case and entropy peaks. The axis here is in nats (natural log), which is why the maximum sits at 0.69 rather than 1 bit.Source : Michael Brenndoerfer, Cross-Entropy Loss: Information Theory for Language Model Training

Conditional entropy

Definition

Conditional entropy measures the residual uncertainty about XX when you know YY:

H(X∣Y)=βˆ’βˆ‘x,yP(x,y)log⁑P(x∣y)H(X|Y) = -\sum_{x,y} P(x, y) \log P(x|y)

It's the average of the entropy of XX over the different values of YY.

Fundamental property

H(X∣Y)≀H(X)H(X|Y) \leq H(X)

Knowing YY can only reduce (or maintain) the uncertainty about XX. Information cannot increase uncertainty.

Equality holds if and only if XX and YY are independent: knowing YY tells you nothing about XX.

In ML

In an autoregressive model, H(tokent∣tokens1:tβˆ’1)H(\text{token}_t | \text{tokens}_{1:t-1}) decreases as the context grows. With more context, predicting the next token is easier. That's why models with longer contexts tend to have better perplexities.

Perplexity: entropy made readable

Definition

Perplexity is simply the exponential of cross-entropy:

PPL=2H(P,Q)\text{PPL} = 2^{H(P, Q)}

where H(P,Q)H(P, Q) is the cross-entropy between the true distribution and the model's predictions (in base 2 for bits). In practice, with the natural log:

PPL=eβˆ’1Nβˆ‘i=1Nlog⁑Q(xi)\text{PPL} = e^{-\frac{1}{N}\sum_{i=1}^N \log Q(x_i)}

Interpretation

Perplexity can be interpreted as the effective number of choices the model "hesitates" between at each step. A perplexity of 10 means the model is as uncertain as if it were choosing uniformly among 10 options.

Numbers in practice

  • Perplexity of 1: the model predicts perfectly, with zero uncertainty
  • Perplexity of 50,000: the model knows nothing, it chooses uniformly from the vocabulary
  • GPT-2: perplexity of about 30 on standard benchmarks
  • GPT-3/4: significantly lower perplexities

Why perplexity and not entropy directly?

Perplexity is more intuitive. Saying "the model hesitates between 25 words" is more meaningful than "the entropy is 4.64 bits." Both contain the same information, but perplexity has an immediate interpretation.

Entropy and regularization

Maximum entropy principle

When you only have partial knowledge about a distribution (for example, you know its mean but nothing else), the maximum entropy principle says: choose the distribution with the highest entropy compatible with those constraints. It's the most "honest" distribution, the one that doesn't add unjustified assumptions.

  • Constraint on the mean: the maximum entropy distribution is exponential
  • Constraints on the mean and variance: it's the Gaussian
  • No constraint (finite support): it's the uniform

Label smoothing

Label smoothing replaces one-hot labels (1,0,0,…)(1, 0, 0, \ldots) with "softened" labels (0.9,0.033,0.033,…)(0.9, 0.033, 0.033, \ldots). It's a form of entropy regularization: you prevent the model from being too confident by increasing the entropy of the target distribution.

Key takeaways

  • Entropy H(X)=βˆ’βˆ‘p(x)log⁑p(x)H(X) = -\sum p(x) \log p(x) measures the uncertainty of a distribution
  • It's maximized for the uniform distribution and zero for a certain distribution
  • Entropy is the theoretical limit of compression
  • Language models are compressors: a good model has low cross-entropy
  • Perplexity =2H= 2^H is the effective number of choices, the standard metric for LLMs
  • Conditional entropy decreases with context: more information = less uncertainty

Where this is used. The perplexity derived from entropy is the yardstick used to judge a model compression: see quantization concepts.