· 14 min read
NLP #5: Perplexity and Test Sets
On this page
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:
After “i want a”, our café model bets its 64 chips on the next word: half on “coffee”, half on “cake”, its two probabilities of 1/2.
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.
You can change your answer at any time. The answers come in a new order each time.
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?
2. (Intuition) Why do we keep a development set as well as a test set?
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?
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?
5. (Intuition · Review) Why did the last lesson add log probabilities instead of multiplying probabilities?
6. (Math · Definition 2) A model gives a 3-token test sequence probability 1/27. What is its perplexity?
7. (Math · Key equation) With the café bigram model, "you want a cake" has probability 1/12. Which should the perplexity use?
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?
9. (Math · Comparing models) Why can't you compare perplexities measured on two different test sets?
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?
Score: 0 of 10 (0 answered)
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).