Foundations

Entropy, perplexity, and what 'good' means

Entropy measures uncertainty and perplexity summarizes average surprise per tokenizer-specific token; comparisons across tokenizers need a common unit such as bits per byte.

Updated

01 · Concept

Concept

Two teams evaluate language models on the same corpus. One reports a perplexity of 7.4, the other 4.2, and the second declares victory. Before accepting that, ask whether the two numbers average surprise over the same kind of event. A tokenizer is part of a model’s learned interface: changing it changes the token sequence, embedding indices, and probabilities being scored. Per-token perplexities from different model-tokenizer pairs therefore need not be directly rankable.

Information theory turns probability into a measure of surprise. An event of probability pp carries self-information logp-\log p: likely events are unsurprising, unlikely ones carry more information when they occur. The base of the logarithm chooses the unit — base two gives bits, the natural logarithm gives nats. Deep-learning libraries almost always use nats, papers frequently quote bits, and the conversion factor ln20.693\ln 2 \approx 0.693 is responsible for a healthy fraction of the wrong numbers in circulation.

The entropy of a discrete distribution is expected surprise:

H(p)=xp(x)logp(x).H(p)=-\sum_x p(x)\log p(x).

A distribution concentrated on one outcome has zero entropy. A uniform distribution over many outcomes has the highest entropy available at that size. For a categorical distribution over VV candidates, the uniform baseline has perplexity VV and surprise log2V\log_2 V bits per event. Apply that anchor only after stating which candidates the runtime includes; Qwen’s tokenizer maps 248,077 ids while its output tensor has 248,320 rows.

To score a model distribution qq against outcomes drawn from pp, use cross-entropy:

H(p,q)=xp(x)logq(x).H(p,q)=-\sum_x p(x)\log q(x).

On a real corpus the observed next token stands in for the data distribution, so this reduces to averaging the negative log probability the model gave to each token that actually appeared. That is mean negative log-likelihood, the ordinary training loss. Assign tiny probability to what happened and pay heavily; assign high probability and pay little. Cross-entropy decomposes as H(p,q)=H(p)+DKL(pq)H(p,q) = H(p) + D_{KL}(p \Vert q): irreducible uncertainty in the data, plus the extra cost of using the wrong distribution. No model can drive the first term to zero, which is why a perplexity of one on natural text would be evidence of leakage rather than genius.

Perplexity exponentiates the average. In nats, PPL=exp(H)\operatorname{PPL} = \exp(H); in bits, PPL=2H\operatorname{PPL} = 2^{H}. If the model faced kk equally likely options at every step, perplexity would be kk, which is where the phrase effective branching factor comes from. Real distributions are not uniform, so treat it as an intuition rather than a count of candidates.

For pair A, let the total negative log-likelihood be SA=2886S_A=2886 bits over NA=1000N_A=1000 tokens:

SANA=28861000=2.886 bits per token,PPLA=22.8867.4.\frac{S_A}{N_A}=\frac{2886}{1000}=2.886\ \text{bits per token},\qquad \operatorname{PPL}_A=2^{2.886}\approx 7.4.

Pair B may produce NB=1400N_B=1400 tokens over the same displayed text, but it has its own total surprise SBS_B:

PPLB=2SB/NB=2SB/1400.\operatorname{PPL}_B=2^{S_B/N_B}=2^{S_B/1400}.

You cannot substitute SA=2886S_A=2886 into the second formula: a different tokenizer defines different prediction events, and arbitrary ids no longer address the rows learned for them. Holding 2886 fixed would be a useful arithmetic demonstration of denominator sensitivity—yielding about 4.2—but not a valid cross-tokenizer model evaluation. Real comparisons measure both numerators and use a common unit.

The classic wrong turn here deserves naming because it is subtle. Faced with the mismatch, people often try to fix it by comparing per-word perplexity instead, reasoning that words are language-neutral. They are not, as lesson 1.2 showed: word boundaries are a convention that several major languages do not use, and inflected languages pack more into one word than analytic ones. Bytes are not perfect either — a script whose characters cost three UTF-8 bytes is charged more per character than one whose characters cost one — but bytes are at least defined identically for every model and every language, and the definition is checkable.

Comparability has further boundaries beyond tokenization. Perplexity depends on the evaluation corpus, on how beginning and end tokens are handled, on the context length, on whether a sliding window recomputes overlapping regions, and on which positions are masked out of the loss. Two published numbers that differ in any of those are not rankable, and the honest response to a mismatch is to report the discrepancy rather than to average it away.

Leakage is the other quiet destroyer. Evaluation text duplicated in training measures memory alongside generalization, and at modern corpus scales assuming a clean split is optimistic. Domain mismatch cuts both ways: a strong model can look weak on text unlike anything it will serve, or flatteringly strong on text that resembles its training distribution.

Report perplexity the way lesson 1.6 asked you to report an analogy result: with the corpus provenance, the token count, the model and tokenizer versions, the context policy, and enough precision to support the conclusion without exaggerating it. A number without its protocol is not a measurement.

02 · Analogy

Analogy

A locksmith is tested against a corridor of doors. A confident, accurate guide points to nearly one key per door; a confused one spreads belief across the ring. Cross-entropy charges the guide according to the probability placed on the key that actually turned. Perplexity converts the average charge into an effective number of equally plausible keys. The catch is that the score is per door, so a corridor rebuilt with twice as many, easier doors will flatter the same guide enormously.

03 · Teach it back

Teach it back

Connect entropy, cross-entropy, and perplexity, then show with numbers why two perplexities measured under different tokenizers cannot be compared.

Minimum: 80 characters and 15 words. Your writing stays only in this browser.

Waiting for your explanation.

Compare with a model answer

Entropy is expected surprise under a distribution. Cross-entropy averages the negative log probability a model assigns to observed outcomes, and perplexity exponentiates that per-token average. For model-tokenizer pair A, let total surprise be S_A bits over N_A tokens, so PPL_A = 2^(S_A/N_A); pair B uses its own S_B and N_B. You cannot hold weights functional, swap in an unrelated tokenizer, and assume total surprise stays fixed, because the token events and embedding ids change together. Raw perplexities therefore do not share a denominator across tokenizers. Bits per byte—S_A/B and S_B/B on the same byte corpus—uses a common denominator, making the units comparable without making the models' probabilities identical.

04 · Check your understanding

Check your understanding

01Model-tokenizer pair A assigns 2886 bits of surprise to 1000 tokens. Can you compute pair B's perplexity from only the fact that its tokenizer emits 1400 tokens on the displayed text?
Answer and explanation

No; pair B has different token events, so its own total log-likelihood must be measured — Pair A's perplexity is about 7.4. Pair B needs its own total surprise S_B: changing tokenization changes the sequence of prediction events, so reusing A's 2886-bit numerator would not describe B.

02If mean token loss is measured in nats, how is perplexity computed?
Answer and explanation

Exponentiate the mean loss — Perplexity is exp of cross-entropy when cross-entropy is in nats, and two to the power of cross-entropy when it is in bits. Mixing the two conventions is a common source of wrong numbers.

03Lesson 1.6 showed that the analogy result depended on an evaluation rule that was rarely reported. What is the analogous omission for perplexity?
Answer and explanation

Failing to state the tokenizer, context policy, and corpus alongside the number — In both cases the headline number is reproducible only with its protocol attached, and in both cases the protocol can move the result more than the thing being measured.

Complete the teach-back and answer the quiz correctly to finish this lesson.

◎ · Evidence marker

Sources

  1. Claude E. Shannon (1948). A Mathematical Theory of Communication.
  2. Soham Chowdhury and Warren Woolf (2026). Benchmarking Byte-Pair Encoding Tokenizers on Different Languages with Bits per Byte.
  3. Qwen Team (2026). Qwen3.8-27B Model Card.