4.2.9Tokenization & Language Modeling

Perplexity as a metric

1,780 words8 min readdifficulty · medium

WHAT is perplexity?

WHY the weird power of 1/N-1/N?

  • The raw sequence probability P(w1,,wN)P(w_1,\dots,w_N) shrinks toward 0 as NN grows (you multiply many numbers <1<1). So it can't compare texts of different lengths.
  • Taking the ==NN-th root== turns the product into a per-token average (geometric mean).
  • The negative exponent flips "probability" (bigger = better) into "perplexity" (bigger = worse), so it behaves like a cost.

HOW to derive it from cross-entropy (first principles)

We build perplexity from information theory, step by step.

Step 1 — Surprise of one event. Information content (surprisal) of an event with probability pp is log2p-\log_2 p bits. Why? Rare events (small pp) carry more information; log-\log makes surprise large when pp is small and 00 when p=1p=1.

Step 2 — Average surprise = cross-entropy. Using the chain rule, P(w1,,wN)=i=1NP(wiw<i)P(w_1,\dots,w_N) = \prod_{i=1}^{N} P(w_i \mid w_{<i}). The average per-token cross-entropy (in bits) is H=1Ni=1Nlog2P(wiw<i)H = -\frac{1}{N}\sum_{i=1}^{N} \log_2 P(w_i \mid w_{<i}) Why the average? We want a length-independent "typical surprise per token."

Step 3 — Perplexity is just 22 raised to that entropy. PPL=2H=21Nilog2P(wiw<i)\text{PPL} = 2^{H} = 2^{-\frac{1}{N}\sum_i \log_2 P(w_i\mid w_{<i})}

Step 4 — Show this equals the definition.

21Nilog2Pi=2log2(iPi)1/N=(iPi)1/N=P(w1,,wN)1/N2^{-\frac{1}{N}\sum_i \log_2 P_i} = 2^{\log_2 \left(\prod_i P_i\right)^{-1/N}} = \left(\prod_{i} P_i\right)^{-1/N} = P(w_1,\dots,w_N)^{-1/N}

So the two definitions are identical. ✅

Figure — Perplexity as a metric

Worked examples


Common mistakes (steel-manned)


The 80/20 core

Recall Feynman: explain to a 12-year-old

Imagine you're guessing the next word in a story. If you're a great guesser, the real word is usually one you thought was likely — you're rarely shocked. Perplexity is like counting how many words you were "torn between" each time. If you were torn between 2 words, perplexity is 2. If you were sure and always right, it's 1 (no confusion). A bad guesser is torn between hundreds of words, so their perplexity is huge. Lower number = smarter guesser.


Flashcards

What is perplexity in one line?
The exponentiated average per-token negative log-likelihood; the effective number of equally-likely tokens the model is choosing among.
Formula for perplexity from sequence probability?
PPL=P(w1,,wN)1/N\text{PPL}=P(w_1,\dots,w_N)^{-1/N}.
Relation between perplexity and cross-entropy HH?
PPL=bH\text{PPL}=b^{H} where HH is the average per-token cross-entropy in base-bb logs.
Why take the 1/N-1/N power?
The NN-th root makes it a per-token geometric mean (length-independent); the negative flips probability into a cost.
Is lower or higher perplexity better?
Lower is better — the model is less surprised by the real text.
Perplexity of a uniform model over vocab size VV?
Exactly VV (worst case; recovers the branching factor).
What happens if the model gives probability 0 to a real token?
Perplexity becomes \infty; this motivates smoothing / avoiding zero probabilities.
Can you compare perplexities across different tokenizers?
No — PPL is per-token, so NN and the distribution change; only compare on identical text and tokenization.
In code, how is PPL computed with natural log?
PPL=exp(1NilnP(wiw<i))\text{PPL}=\exp\left(\frac{1}{N}\sum_i -\ln P(w_i\mid w_{<i})\right).
Does the log base affect the final perplexity?
No, as long as you exponentiate with the same base (eHnats=2Hbitse^{H_{nats}}=2^{H_{bits}}).

Connections

Concept Map

shrinks with length

per-token average

flips into cost

averaged over tokens

factorizes probability

exponentiate

equals

must match

interpreted as

worst case

Perplexity PPL

Sequence probability P w1..wN

N-th root geometric mean

Negative exponent -1/N

Surprisal -log p

Cross-entropy H

Chain rule product of P wi

Master relation PPL equals b^H

Log base b matches exponent

Effective number of choices

Uniform case PPL equals V

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Perplexity ka matlab simple hai: model kitna "surprise" ho raha hai real text dekh ke. Agar model ko pata hai ki next word kya aayega — yaani woh us word ko high probability deta hai — toh woh surprised nahi hota, aur perplexity kam aati hai. Kam perplexity = accha model. Isko aise socho: har step pe model kitne equally-likely options ke beech confuse hai — agar 2 options ke beech confuse hai toh PPL=2, agar sure hai toh PPL=1.

Formula ka dil ye hai: PPL=P(poora sequence)1/N\text{PPL}=P(\text{poora sequence})^{-1/N}. Yahan NN-th root isliye lete hain taaki har token ka average (geometric mean) mil jaaye, aur alag-alag length ke texts compare ho saken. Aur ek important connection: perplexity basically cross-entropy ka exponential hai — PPL=eaverage negative log-likelihood\text{PPL}=e^{\text{average negative log-likelihood}}. Deep learning training mein hum cross-entropy loss minimize karte hain, matlab hum indirectly perplexity hi kam kar rahe hote hain.

Do practical baatein yaad rakho. Ek: agar model kisi real token ko probability 0 de de, toh log0=-\log 0 = \infty, aur perplexity infinite ho jaati hai — isiliye smoothing zaroori hai. Do: perplexity ko sirf same tokenizer aur same text pe compare karo. Alag tokenizer matlab alag number of tokens (NN), toh comparison galat ho jaayega. Iss case mein bits-per-character use karo.

Exam ya interview mein bas ye 80/20 pakdo: lower PPL = better, PPL = effective number of choices (branching factor), aur PPL = 2H2^H (entropy se jud़ा hua). Bas itna clear rahe toh perplexity poora samajh gaye.

Go deeper — visual, from zero

Test yourself — Tokenization & Language Modeling

Connections