AI Compass
Compass

Information and entropy

Entropy measures surprise, cross-entropy measures the price of a wrong expectation. Together they are the loss function of every language model.

·2 min read·By Fachredaktion Technik
DETAIL
3 sections

The idea

If I tell you "the sun will rise tomorrow", you have learned nothing. If I tell you "it will snow in Vienna tomorrow, in August", you have learned a lot. Information is surprise, and surprise is the opposite of probability.

Entropy is the average surprise of a whole source. A die has more entropy than a coin, because more can happen.

What it is good for

A language model is trained to be surprised as little as possible. The better it predicts the next token, the smaller the surprise and the smaller the loss. That is the entire training process.

The three quantities

QuantityMeasuresTypical value
Entropy H(p)Uncertainty of the true distributionEnglish prose: 1.0 to 1.5 bits per character
Cross-entropy H(p,q)Cost of using q instead of pThe training loss, in nats per token
KL divergence D(p‖q)Only the avoidable extraIn RLHF, the distance from the base model
import numpy as np

def entropy(p, base=2):
    p = np.asarray(p, dtype=float)
    p = p[p > 0]                          # 0·log0 is defined as 0
    return float(-(p * np.log(p) / np.log(base)).sum())

print(round(entropy([0.5, 0.5]), 3))                 # 1.0   -> fair coin
print(round(entropy([0.9, 0.1]), 3))                 # 0.469 -> nearly certain
print(round(entropy([0.25] * 4), 3))                 # 2.0   -> four options

What you can measure in operation

  • Perplexity on your own domain text shows how foreign that domain is to a model.
  • Per-token output entropy shows where a model is guessing: a usable early warning for hallucination.
  • KL divergence between two model versions shows how far a fine-tune moved behaviour.

The formulas

Entropy, cross-entropy, KL divergence

H(p) = − Σᵢ pᵢ · log pᵢ H(p,q) = − Σᵢ pᵢ · log qᵢ D(p‖q) = Σᵢ pᵢ · log(pᵢ / qᵢ) = H(p,q) − H(p)

Cross-entropy is entropy plus KL divergence; only the second term depends on the model and is therefore what gets minimised.

p
the true distribution
q
the distribution predicted by the model
H(p)
entropy: the unavoidable uncertainty
H(p,q)
cross-entropy: the actual cost
D(p‖q)
KL divergence: the avoidable share

Worked through

A model has to predict the next token. The truth is token 2, so p = [0, 1, 0, 0]. The model says q = [0.1, 0.7, 0.15, 0.05].

  • H(p,q) = −(0·log0.1 + 1·log0.7 + 0·log0.15 + 0·log0.05) = −log 0.7 = 0.357 nats
  • In bits: 0.357 / ln2 = 0.515 bits
  • Perplexity of this single token: e^0.357 = 1.43

If the model instead says q = [0.25, 0.25, 0.25, 0.25], then H(p,q) = −log 0.25 = 1.386 nats and perplexity is exactly 4: the number of options. That is the intuition behind the metric.

Perplexity

PPL = exp( −(1/N) · Σᵢ log qᵢ )

Perplexity is the exponential of the mean negative log probability.

PPL
perplexity
N
number of scored tokens
qᵢ
the probability the model assigned to the token that actually occurred

Pitfalls

  • Base. log₂ gives bits, ln gives nats. A factor of 1.4427 between the two explains most discrepancies when checking someone else's numbers.
  • Tokeniser. Perplexity is per token. A vocabulary that splits compound words into more pieces lowers perplexity without any improvement. It only becomes comparable as bits per character.
  • Numerics. log q as q → 0 goes to minus infinity. Implementations never compute log(softmax(x)) but log_softmax(x) in one step, see Numbers in a computer.
Was this page helpful?
Information and entropy