Originally published on almoghagbi.com, where the figures are interactive.
By the end of this post, you'll be able to grade a language model the way
researchers do: hold out text the model has never seen, compute its
perplexity by hand, and say what the number means. In natural language
processing (NLP), perplexity is the standard score for language models. It
works for the n-gram models of the last lesson
and for today's large language models. Sections 3.2 and 3.3 of
Speech and Language Processing
introduce it.
Intuition
Never grade a model on its own homework
Last time we counted bigrams in four café orders:
i want a coffee\
i want a cake\
you want coffee\
i like cake
Here's a puzzle. A second model memorizes these four lines and gives each one
probability 1/4. Our bigram model gave "i want a coffee" only 1/6. Is the
memorizer the better model? Try a new order before you read on.
| Order | Memorizer | Bigram model |
|---|---|---|
| i want a coffee (seen) | 1/4 | 1/6 |
| you want a cake (new) | 0 | 1/12 |
The memorizer wins on the orders it saw and gives 0 to the new one. The
bigram model never saw "you want a cake" either, but its counts still give it
1/12. A score on the training text measures memory, not language.
So we split the data before training. The training set is what the model
counts. The test set is held back like a sealed exam. Now say you try
bigrams, trigrams and 4-grams. You check each on the test set and keep the best.
You've just studied from the exam. So we hold back a second slice, the
development set, for those checks, and open the test set once, at the end.
A good model saw it coming
Two models read the same order. Model A gave the next word "coffee"
probability 1/2. Model B gave it 1/4. Which did better, and by how much? Guess,
then step through.
Picture each model as a gambler with a stack of chips. Before each word, it
spreads its chips over the words that could come next, in proportion to its
probabilities. Then the real word appears, and only the chips on that word
survive:
▶ Interactive figure: try it on the original post.
The fraction of chips left at the end is exactly the sentence's probability. A
model that bets on the right words keeps a big stack. After three words, A keeps
8 chips and B keeps 1: A did 8 times better, a factor of 2 on every word.
Score it per word
Step 5 of the figure shows both stacks ending at 1 chip, so both
sentences get probability 1/64. Which model is better? Look at the arrows
before you read on.
Model A only ever halved
its stack; it had six words to read. Model B dropped to a quarter at every
word. The total can't tell them apart, because every extra word shrinks the
stack, even for a good model.
So ask how fast the stack shrinks per word. Model A's stack halves at every
word: a per-word shrink of 2. Model B's drops to a quarter: a shrink of 4. That
per-word shrink factor is the perplexity. Lower is better.
Bets are rarely that even. If the stack halves on one word and drops to an
eighth on the next, it ends 16 times smaller after two words. That's the same as
dropping to a quarter on each word, so the perplexity is 4: the per-word factor
that gives the same total.
As good as a blind guess
Another puzzle: a model that knows nothing splits its chips evenly over 4
words, every time. What's its perplexity? (Step 8 of the figure shows it.) Each word keeps a quarter of the
stack, so it's 4. That's model B exactly. Whatever model B thinks it knows, it
does no better than a blind guess among 4 words.
That's the second way to read the number: perplexity is how many equally
likely words a blind guesser would choose between to do just as well. The book
trains a unigram, a bigram and a trigram model on 38 million words of Wall
Street Journal text and tests them on 1.5 million more. Their perplexities are
about 962, 170 and 109: the trigram model does as well as a blind guess among
109 words, where the unigram model needs 962. More context, fewer surprises.
The math
Training, development and test sets
Definition 1 (Data splits). The training set, development set and
test set are three non-overlapping parts of the data: one to estimate the
parameters, one to compare models and tune choices such as the n-gram order, and
one for the final score only. The test set should look like the text the model
will be used on.
Perplexity
Definition 2 (Perplexity). The perplexity of a language model on a test
sequence
of
tokens is its probability raised to the
power
:
The
-th root turns the total shrink into a per-word shrink: it's the
geometric mean of the factors
. Lower perplexity means higher probability.
Key equation (Bigram perplexity). With the chain rule and the bigram
assumption from the last lesson,
counts every predicted token, including the end marker </s> but not
<s>, which is never predicted. (The figure skipped </s> to stay short; a
real score counts it.)
Worked example. Score the test order "you want a cake" with the bigram
model counted from the four orders:
| Factor | Count | Value |
|---|---|---|
| 1 of 4 orders start with "you" | 1/4 | |
| "you" is always followed by "want" | 1 | |
| 2 of 3 "want"s | 2/3 | |
| 1 of 2 "a"s | 1/2 | |
| "cake" always ends the order | 1 |
The product is
,
over
tokens, so
It must be between 1 and 2, since
is more than 12. On average, the
model does as well as a blind guess among 1.6 words.
Perplexity in logs
A long test set has a tiny probability, so, as with the
log probabilities
of the last lesson, compute perplexity in logs:
Check it on the figure. Model A: each word gives
, so the
average is
and
. The uneven bets give
and
, so the
average is
and
. Same answers. Logs turn the per-word geometric mean into an
ordinary average. The value
is how surprised the model is by a word,
in bits, so perplexity is 2 to the power of the average surprise.
Comparing models fairly
A puzzle: a model replaces every word with one token, <unk>, and predicts it
with probability 1. What's its perplexity? It's 1, a perfect score from a
useless model. That's why perplexities compare only on the same test set and the
same vocabulary.
And what does the bigram model give "you like coffee"? Try the factors one at a time.
The bigram "you like" never occurred in training, so
. The product is 0, and the perplexity is
infinite. Real test sets always contain unseen n-grams, which is why the next
lesson is about zeros.
What you can do now
- Split data into training, development and test sets, and explain why the test set is used only once.
- Compute a sentence's perplexity: multiply the inverse probabilities and take
the
-th root, counting
</s>but not<s>. - Read perplexity as how many times smaller the stack of chips gets per word, or as the number of words a blind guesser would choose between to do as well.
- Compare models only on the same test set and vocabulary, where lower is better.
Exercises
Each question has one correct answer. Pick your answer, then read the explanation. Questions 6–10 use the definitions and equations above.
1. (Intuition) A model scores very well on the text it was trained on. What does that tell you about how it handles new text?
A. That it will score just as well on new text.
B. That it will score worse on new text, by a known amount.
C. Almost nothing: a model can score well on its training text just by memorizing it.
D. That its vocabulary is large enough for any text.
C. Training text is the model's homework. A model that memorized the four café orders scores them perfectly and gives 0 to any new order.Answer
2. (Intuition) Why do we keep a development set as well as a test set?
A. So we can tune choices like the n-gram order on held-out text without wearing out the test set.
B. Because the training set is too small to count bigrams.
C. So the model can learn from the development set after training.
D. Because perplexity can't be computed on the test set.
A. Every look at a held-out set nudges our choices toward it. The development set takes those looks, and the test set stays a sealed exam for the final score.Answer
3. (Intuition) In the chips figure, model A gives each real word probability 1/2 and starts with 64 chips. How many chips are left after 3 words?
A. 32
B. 16
C. 2
D. 8
D. Each word keeps half the stack: 64, then 32, then 16, then 8. The fraction left, 8/64 = 1/8, is the sentence's probability.Answer
4. (Intuition) A model's stack halves on one word and drops to an eighth on the next. What is its perplexity on these two words?
A. 5, the average of 2 and 8.
B. 4, because 16 times smaller over two words is 4 times per word.
C. 16, the total shrink.
D. 8, the worst word.
B. Over two words the stack is
2×8=16
times smaller. The per-word factor that gives 16 over two words is 4, since
4×4=16
.Answer
5. (Intuition · Review) Why did the last lesson add log probabilities instead of multiplying probabilities?
A. Logs make every probability positive.
B. Logs make the model more accurate.
C. The product of many small probabilities underflows to 0 on a computer.
D. Logs remove the need for the end marker.
C. Multiplying thousands of numbers below 1 soon gives a number too small to store, and it becomes 0. Adding their logs gives the same information safely. Perplexity is computed the same way.Answer
6. (Math · Definition 2) A model gives a 3-word test sequence probability 1/27. What is its perplexity?
A. 3
B. 9
C. 27
D. 1/3
A.
PP=(1/27)−1/3=271/3=3
.Answer
7. (Math · Key equation) With the café bigram model, "you want a cake" has probability 1/12. Which should the perplexity use?
A. 4, the words in the sentence.
B. 6, the words plus
<s>and</s>.C. 3, the bigrams inside the sentence.
D. 5, the four words plus
</s>.
D.
N
counts every predicted token. The model predicts "you", "want", "a", "cake" and the end marker, which is five factors. Answer
<s> is given, never predicted.
</s>, which the model also predicts.<s> is never predicted, so it doesn't count.
8. (Math · Definition 2) Model X has perplexity 120 and model Y has perplexity 80 on the same test set, with the same vocabulary. Which gives the test set the higher probability?
A. X, because a higher perplexity means a higher probability.
B. Y, because perplexity goes down when probability goes up.
C. They give the same probability.
D. It can't be told from perplexity.
B. Perplexity is
P(W)−1/N
, which shrinks as
P(W)
grows. On the same test set, lower perplexity means higher probability.Answer
9. (Math · Comparing models) Why can't you compare perplexities measured on two different test sets?
A. Because perplexity is only defined for bigram models.
B. Because the root in the formula changes with the test set's language.
C. Because one test set can simply be easier to predict than the other.
D. You can, as long as both have the same number of words.
C. Perplexity measures a model on a text. A repetitive text gets a low perplexity from any model, so a difference could come from the texts rather than the models.Answer
10. (Math · Comparing models) In the test order "you like coffee", the bigram "you like" never appeared in training. What is the bigram model's perplexity on it?
A. Infinite, because one zero factor makes the whole probability 0.
B. 0, because the probability is 0.
C. The same as on "you want coffee".
D. It skips the unseen bigram and averages the rest.
A.
P(like∣you)=0
, so the product is 0 and
PP=(1/0)1/N
is infinite. The model gives no chance at all to a normal order.Answer
Next: what to do about zeros. We'll generate text from a model, then fix
the zeros with smoothing (sections 3.4 to 3.6).
Co-authored with AI.
Top comments (0)