4.2.9 · D4Tokenization & Language Modeling

Exercises — Perplexity as a metric

2,254 words10 min readBack to topic

Before we start, one shared toolkit. Every problem below uses exactly two moves, so let us name them once.


Level 1 — Recognition

L1.1 A model predicts each of exactly tokens with equal probability at every step. Without computing , state its perplexity and say why.

L1.2 True or false: "If model A has cross-entropy bits and model B has bits on the same text, then A has the higher perplexity."

L1.3 Match each quantity to its formula: (a) surprisal of one event, (b) average per-token cross-entropy, (c) perplexity — from the list .

Recall Solution L1

L1.1 For a uniform model over options, every , so Perplexity of a uniform model is exactly the vocabulary size — the branching factor. The model is genuinely torn between 8 equally-good tokens.

L1.2 False. is monotonically increasing in : bigger entropy → bigger perplexity. Since , model A has the lower perplexity ( vs ). A is the better model.

L1.3 (a) surprisal (b) average cross-entropy (c) perplexity


Level 2 — Application

L2.1 A model assigns these probabilities to the four real tokens of a sentence: . Compute in bits and then .

L2.2 In code the same sentence is scored with natural log. Compute the mean negative log-likelihood and confirm gives the same perplexity as L2.1.

L2.3 A model gives every real token the same probability . What is its perplexity, for any sentence length?

Recall Solution L2

L2.1 Surprisals in bits (): , , , .

L2.2 (twice), , . Identical to L2.1 — the base cancels once you exponentiate with the matching base. This is the meaning of .

L2.3 Uniform-per-token: every , so regardless of . Constant per-token probability ⇒ the geometric mean is that same number ⇒ perplexity is its reciprocal.


Level 3 — Analysis

L3.1 Two three-token predictions from the parent note. Model A: . Model C: . Which has lower perplexity? Compute both and explain which token dominates each score.

L3.2 A model is almost perfect: on tokens it assigns , but on one real token it assigns . Compute the perplexity and comment on how much that single token hurts.

L3.3 Take the limit: a model assigns probability 0 to a token that actually occurred. What is the perplexity, and what does the geometry of (see figure) tell you about why?

Figure — Perplexity as a metric
Recall Solution L3

L3.1 Model A: product , so . In nats: ; divide by 3 → ; negate → ; . Model C: product , so . Model C is lower (3.333 < 4.310). Even though A is very confident on token 1 (), its worst token () drags the geometric mean down. Perplexity is a geometric mean, so the smallest probabilities do the most damage — one bad guess costs more than one good guess saves.

L3.2 . Now and . . Compare: without the bad token, . One catastrophic token in a hundred pushes perplexity from to — a large jump from a single rare event, because surprisal explodes as .

L3.3 As , (look at the red curve shooting up on the left of the figure). One infinite surprisal makes the sum infinite, so and . A single impossible-but-real token destroys the entire score. This is exactly why real models smooth probabilities so no token ever gets exactly .


Level 4 — Synthesis

L4.1 The same sentence "the cat sat" is scored by two systems on different tokenizations:

  • System W (word-level): tokens [the, cat, sat], , probabilities .
  • System S (sub-word): tokens [the, ca, t, sat], , probabilities .

Compute both perplexities. Are they comparable? What is comparable?

L4.2 For System W and System S above, compute the total sentence log-probability for each. Which quantity is legitimately comparable across the two tokenizers, and why?

Recall Solution L4

L4.1 System W: product ; . ; ; negate ; . System S: product ; . ; ; negate ; . Not comparable. differs ( vs ) and the per-token distributions differ, so these perplexities live on different scales. A finer tokenizer packs the same sentence into more, individually-easier tokens, artificially lowering per-token perplexity. To compare fairly, normalize by a tokenizer-independent unit like bits-per-character — see Bits-Per-Character (BPC).

L4.2 System W: . System S: . The total log-probability of the identical text is comparable (higher = better model of that string), because it does not depend on how you cut the string into tokens — the product of conditionals telescopes to . Here System S assigns higher probability (), so it models the string better, even though you cannot legitimately compare their per-token perplexities directly. Dividing this total by the number of characters (not tokens) gives the tokenizer-fair metric.


Level 5 — Mastery

L5.1 (Design a smoothing floor). A model outputs a probability of for one real token out of , giving all other tokens probability . You add smoothing: replace any probability below a floor with (leaving the s unchanged for simplicity). Compute the perplexity after smoothing, and state the perplexity before smoothing.

L5.2 (Invert the metric). A benchmark reports over a text of tokens using natural log. Recover (a) the average per-token cross-entropy in nats, (b) the same in bits, and (c) the total sentence log-probability .

L5.3 (Prove the branching-factor bound). Show that for any model over a vocabulary of size , the perplexity on held-out text satisfies only when the model is uniform at the top; more precisely prove always, with equality iff the model is perfectly certain ( for every real token). State the reasoning cleanly.

Recall Solution L5

L5.1 Before smoothing: one ⇒ surprisal . After smoothing: tokens at and token at . . Smoothing turns an infinite, useless score into a finite, meaningful one — the whole point of never allowing zero probability.

L5.2 With and : (a) nats. (b) bits. (c) , so .

L5.3 Every real token has , so (log of a number is ; the minus makes it ). The average of non-negative numbers is non-negative: . Then . Equality holds iff every surprisal is , i.e. every : the model assigns all its mass to the token that actually occurs at every step — perfect certainty, zero confusion. The upper bound is reached at the opposite extreme, the uniform model (worst case), where and ; any non-uniform model that still respects a -way vocabulary can exceed only if it wastes mass on wrong tokens, so is the uniform benchmark, not a hard ceiling. Hence the clean, always-true statement is .


Connections