4.2.9 · D2Tokenization & Language Modeling

Visual walkthrough — Perplexity as a metric

2,032 words9 min readBack to topic

Step 1 — A model is a bag of guesses

WHAT. Imagine reading a story one word at a time. Before each word appears, our model spreads out its belief: it puts a fraction of confidence on every possible next token (a token = one chunk of text; see Tokenization). Those fractions are all positive and sum to 1 — that's what a probability distribution is (produced in practice by a softmax).

WHY. Everything downstream is just "how good were these guesses?" So we must first see a guess as a set of bars whose heights add to 1.

PICTURE. In the figure, the amber bar is the token that actually came next. A good model makes the amber bar tall; a bad model makes it short.

Figure — Perplexity as a metric

Step 2 — Surprise = how disappointed a tall/short bar makes you

WHAT. We want a surprise score for each guess. If the true token got probability , surprise should be (you called it perfectly). If is tiny, surprise should be huge. If (you swore it was impossible), surprise should be infinite.

WHY a logarithm — and which base? We need a function that is at , grows as , hits at and that adds up nicely when independent guesses combine. There is essentially one function that does all of this: the surprisal . The minus sign flips "small probability" into "big number"; the log is chosen because it turns multiplying probabilities into adding surprises (we'll cash that in at Step 4). A logarithm always needs a base — the number we count doublings/growth against. We call that base : choose and surprise is measured in bits (yes/no forks); choose (the number code uses) and it is measured in nats. The base is a unit choice; we keep it as the letter so the picture works for either. This is the foundation of Entropy and Information Content.

PICTURE. Watch the curve dive from on the left down to at . The amber dots mark (no surprise) and a small (big surprise).

Figure — Perplexity as a metric

Step 3 — Add up all the surprises along the sentence

WHAT. Say the sentence has tokens — so is simply the number of tokens (positions) in the passage we are scoring. That gives guesses, so surprises. Stack them: total surprise .

WHY sum and not multiply? Because Step 2 already converted "multiply probabilities" into "add surprises." Total surprise for the whole passage is honestly just the running total of per-step surprises.

PICTURE. Each cyan block is one token's surprise. Wider/taller block = the model was more shocked there. The total is the whole tower's height.

Figure — Perplexity as a metric

Step 4 — Average the surprise → cross-entropy

WHAT. Divide the tower by (the token count from Step 3) to get the typical surprise per token:

WHY. Dividing by makes the number length-independent — now a tweet and an article are on the same footing. This per-token average is the cross-entropy , and minimizing it is the Language Modeling Objective.

PICTURE. The tall tower of Step 3 is flattened into one bar of the average height — the dashed amber line is where each block would sit if all surprises were equal.

Figure — Perplexity as a metric

Step 5 — Undo the log: from surprise back to "number of choices"

WHAT. is measured in log-units (bits/nats), which is abstract. Let's translate it back into a count by raising the base to the power :

WHY undo the log now? Because a count of choices is far more intuitive than "3.2 bits of surprise." Raising to a surprise inverts the we introduced in Step 2 — it answers: "surprise is what you'd feel if you were guessing uniformly among how many equally-likely options?" That count is the answer.

PICTURE. Imagine bits of surprise. Each bit is one yes/no fork in a decision tree. bits means leaves — that many equally-likely endings. The tree's leaf-count is the perplexity.

Figure — Perplexity as a metric

Step 6 — Assemble the compact formula

WHAT. Two more names, then the punchline. Write for the joint probability of the whole sequence — the single number you get by multiplying all the per-token bars together (chain rule): . With the token count from Step 3, we now show that equals the compact expression .

WHY. One truth deserves the cleaner face. Seeing the algebra makes the strange exponent earned rather than memorized.

PICTURE. The chain: multiply the bars (sequence probability ) → take the -th root (per-token typical bar) → flip it (reciprocal). Each arrow is one algebra move.

Figure — Perplexity as a metric
  • ::: logs turn a sum into a log-of-product (Step 2's whole point).
  • ::: raising the base undoes its log — the and annihilate.
  • exponent ::: the -th root (geometric mean) and the reciprocal, glued together.
  • ::: the chain rule — the whole sentence's joint probability.

Step 7 — Every edge case, on one number line

WHAT. We now walk the full range of possible perplexities so no scenario surprises you. One new name we need here: = the vocabulary size, i.e. the total number of distinct tokens the model could ever emit (fixed by the tokenizer). A uniform "know-nothing" model spreads its bet equally over all of them.

PICTURE. A number line of PPL. Read left (best) to right (worst):

Figure — Perplexity as a metric
  • Perfect model — every true token got . Then and . One choice: no confusion at all. This is the floor; PPL can never go below .
  • Uniform model over vocab size — each , so and . The model recovers the raw vocabulary size — the honest "I know nothing" baseline.
  • Zero on a real token — some . A single "impossible-but-real" token detonates the whole score. This is why we smooth and never allow a hard zero.

The one-picture summary

Everything above, compressed into a single pipeline: bars → surprise → sum → average → exponentiate → count of choices.

Figure — Perplexity as a metric
Recall Feynman: the whole walkthrough in plain words

A model reads a story and, before each word, spreads its confidence over all possible next words (Step 1). We score each guess by surprise: guessed the truth confidently → small surprise; swore it was impossible → infinite surprise (Step 2). We add up the surprises across the whole story (Step 3), then divide by the number of words so short and long stories compete fairly — that average is the cross-entropy (Step 4). But "3 bits of surprise" is abstract, so we translate it back into a count: "you were this surprised — as if you'd been guessing blindly among how many equally-likely words?" That count is the perplexity (Step 5). A little algebra shows this count equals one over the typical probability you gave the truth, i.e. (Step 6). The best possible score is 1 (never confused); a know-nothing model scores the vocabulary size ; and a single word you called impossible sends the score to infinity (Step 7). Lower = smarter guesser.


Connections