4.2.9 · D1Tokenization & Language Modeling

Foundations — Perplexity as a metric

2,436 words11 min readBack to topic

This page assumes nothing. If the parent note Perplexity as a metric used a symbol, we build it here from the ground up, in an order where each idea leans only on the ones before it.


0. What is a "token"? (the atom of everything)

Before we can talk about probabilities of text, we need to know what text is made of.

The picture (Figure 1): imagine a sentence as a train. Each carriage is one token. The model reads the train one carriage at a time, left to right.

Why the topic needs it: perplexity is measured per token. If you don't know what a token is, you can't know what "per token" divides by. Two people who chop the same sentence into different-sized carriages will get different perplexities — this is exactly why you can only compare on the same tokenization.


1. The symbols , , and the subscript

Now we name the carriages.

The picture (back to Figure 1): the same train, but now each carriage wears a label .


2. Probability — the model's "how likely" number

The picture (Figure 2): a horizontal bar. Full bar = probability . Half-full = . Empty = .

Why the topic needs it: perplexity's whole job is to grade how much probability the model handed to the tokens that actually appeared. High probability on the real token = good. That grading needs this number.


3. The vertical bar: — "given what came before"

Language is not a bag of independent words; the next word depends on the earlier ones. We need notation for "the probability of this, given that."

The picture: the reader stands on carriage and looks backward down the track at everything already read; that backward view is .

Why the topic needs it: a good model uses context. After "the cat sat on the ___", the word "mat" is likely and "helicopter" is not. Only conditional probability captures this, and language models are trained exactly to predict .


4. Joint probability and the chain rule

To score a whole sentence we must combine per-token probabilities. First we need the notation for "the probability that several things all happen together."

The key link between "and" and "given." How likely are and together? First happens (probability ), then happens given (probability ). Multiply those two:

Now unroll it for a sequence. Apply the product rule again and again, peeling off one token at a time from the back:

Each step splits off the last token as "given everything before it," leaving a smaller joint probability to split again. Do this for all tokens and the pattern is:

Why the topic needs it: this product is the raw score that appears inside perplexity. Notice the problem it creates: multiplying many numbers below makes the result tiny and shrinks with length — a 100-token sentence scores far lower than a 5-token one, even from the same model. Fixing that shrinkage is the entire reason perplexity does its next two tricks (the root and the log).


5. Logarithms — turning multiplying into adding

Multiplying many small numbers is numerically nasty and hard to average. The logarithm is the tool that rescues us.

The picture (Figure 3): think of a factory line. Multiplying is stacking blocks (heights multiply, awkward). is a magic ruler that turns "stacking" into "laying blocks end-to-end" (heights add).


6. Surprisal — measuring "how shocked am I?"

Now we assemble logs into the emotional core of perplexity: surprise.

The picture (Figure 4): a curve that is on the right (at , no surprise) and shoots up toward infinity on the left (as , unbounded shock).

Why the minus sign? Because is between and , its log is negative. Multiplying by flips it to a positive "amount of surprise." A cost should be positive.

Why the topic needs it: perplexity is literally the average surprise, exponentiated. Read more at Entropy and Information Content and Cross-Entropy Loss.

Recall What is the surprisal of a certainty?

An event with has surprisal . You are not surprised by what was guaranteed. :::


7. Averaging with and the "root"

Two final tools glue everything into a length-independent score.

The picture: the geometric mean of is the single value that, multiplied by itself 3 times, gives the same product — one "typical" per-token probability standing in for all of them.


8. The capstone: assembling the perplexity formula

Now every symbol is earned, so we write the whole thing in one place.

Step A — start from the sentence score (Section 4):

Step B — turn "bigger = good" into a length-independent cost. Take the -th root and the reciprocal (Section 7):

Step C — the equivalent surprise form. Using (Section 5) to turn the product into a sum of surprisals (Section 6) and then averaging (Section 7):

These two boxed forms are the same quantity written two ways; the parent note Perplexity as a metric proves their equality line by line. And from Section 6's edge case: if any , the sum contains and .


Prerequisite map

Tokenization: text into tokens

w_i and N: naming tokens

Probability P: how likely

Conditional P given history

Joint P and product rule

Chain rule: product of probs

Logarithm: product into sum

Surprisal: minus log P

Average and root: per-token mean

Perplexity 4.2.9

Softmax: where probs come from


Equipment checklist

Test yourself — cover the right side and answer aloud.

What does the subscript in mean?
The position/address of the token; = "the token at position ." It is never multiplication.
What does the vertical bar in read as?
"given" — the probability of token given all the tokens before it.
What does the comma in read as?
"and" — the joint probability that and both happen.
What is the product rule for two events?
— "both" = "first" times "second given first."
What does stand for?
All earlier tokens — the history so far.
What does tell you to do?
Multiply all terms together (the multiplication cousin of ).
Why does the chain-rule product cause a problem for long sentences?
Multiplying many numbers below makes the score tiny and shrinking with length, so it can't compare different lengths.
What single property makes the logarithm the right tool here?
— it turns the awkward product into an easy sum.
What is and why?
, because ; the log asks "the base to what power gives this?"
What is the surprisal of an event with probability ?
: large for rare events, zero when .
Why is there a minus sign in surprisal?
Because is negative for ; the minus makes surprise a positive cost.
What happens to perplexity if some ?
Surprisal is , so perplexity becomes ; this motivates smoothing.
What does the power do, split into its two parts?
takes the -th root (geometric mean = per-token average); the minus takes the reciprocal (flips good into cost).
Write perplexity in its average-surprisal form.
.
What number does a uniform model over tokens give for perplexity?
Exactly .