Search

Search papers and lessons

Perplexity

Level 3 · 12 min read

Perplexity is the exponential of a model's average cross-entropy per token: the number of equally likely choices the model is, on average, deciding between.

On this page

  • Colour = topic mix
  • Discrete math
  • Calculus
  • Probability
  • Losses & regularization
  • Pre-training
  • Neural network basics
  • ReadReady to readNot yet

TL;DR

Perplexity is a single number that says how well a language model predicts a piece of text. It is the exponential of the model's average cross-entropy per token, so a lower value means the model was less surprised by the text. Intuitively, a perplexity of kk means the model was, on average, as uncertain as if it were choosing uniformly among kk tokens at each step. It is cheap to compute for any model that gives next-token probabilities, which is why it is the standard first measure when a language model is trained.

Builds on: Language Model, Cross-Entropy Loss, Logarithm · Leads to: model comparison on held-out text, and the training curves that researchers watch while a Language Model learns

Where it's used

Tracking training. A team training a large model logs the perplexity on a held-out set of text every few thousand steps. A falling curve shows that the model is learning the structure of the language. A flat or rising curve on the held-out set, while the training curve still falls, is an early sign of overfitting.

Comparing models on the same data. Two models that read the same test set can be ranked by perplexity, whether one is a counting model such as an N-gram Language Model or a neural network. The comparison is only fair when both models use the same tokens, so the test set is split the same way for both.

Speech recognition and transcription. A recogniser must choose among several word sequences that sound alike. A Language Model's perplexity on the task's text tells how many plausible word choices the recogniser faces at each step. The measure was introduced in this setting, and it is still used to tune the language model that scores candidate transcripts.

How it works

Trace one tiny text through the model. The text is four tokens: "the cat sat down". The model gives the following probabilities, one for each token given the tokens before it.

  1. Read the first token. The model gives "the" a probability of 0.2. In words: the chance that a text starts with "the" is 0.2, as far as this model knows. Check: this is one number between 0 and 1.

  2. Read the second token given the first. Given "the", the model gives "cat" a probability of 0.1. Check: the number is conditional on everything before it, which is the only thing a left-to-right model can use.

  3. Read the third token given the first two. Given "the cat", the model gives "sat" a probability of 0.5. Check: a more predictable continuation gets a higher number.

  4. Read the fourth token given the first three. Given "the cat sat", the model gives "down" a probability of 0.25. Check: all four numbers are between 0 and 1.

  5. Multiply to get the probability of the whole text. By the Chain Rule of probability, the probability of the text is the product: 0.2⋅0.1⋅0.5⋅0.25=0.00250.2 \cdot 0.1 \cdot 0.5 \cdot 0.25 = 0.0025. Check: a text of four tokens with these numbers has a probability much smaller than any single token.

  6. Take the Logarithm of each token's probability and average. Using natural logarithms, the four values are ln⁡0.2=−1.61\ln 0.2 = -1.61, ln⁡0.1=−2.30\ln 0.1 = -2.30, ln⁡0.5=−0.69\ln 0.5 = -0.69 and ln⁡0.25=−1.39\ln 0.25 = -1.39. They sum to −5.99-5.99. Divide by the number of tokens, 4, to get −1.50-1.50. This average is negative because each logarithm is negative. Check: the average is the same as the logarithm of the product, divided by 4.

  7. Flip the sign and exponentiate. The negative of the average is 1.50, the cross-entropy per token in nats, and e1.50≈4.47e^{1.50} \approx 4.47. The perplexity of this text under the model is about 4.47. Check: a model that gave every token probability 1/4 would score exactly 4.

The number 4.47 is read as the average branching factor. At each step, the model behaved as if it were choosing among about 4.5 equally likely tokens.

flowchart LR
    A["Token probabilities p_t"] --> B["Take ln of each"]
    B --> C["Average over the N tokens"]
    C --> D["Negate and exponentiate"]
    D --> E["Perplexity"]

The key idea is in steps 5 to 7. The probability of a text shrinks with its length, so it cannot be compared across texts of different sizes. Taking the average of the logarithms removes the length effect, and exponentiating turns the result back into a count of choices.

The math

Definition: Perplexity

For a text of NN tokens x1,…,xNx_1, \dots, x_N and a model that gives p(xt∣x1,…,xt−1)p(x_t \mid x_1, \dots, x_{t-1}) for each token, the perplexity is

PPL=exp⁡ ⁣(−1N∑t=1Nln⁡p(xt∣x1,…,xt−1)).\mathrm{PPL} = \exp\!\left( -\frac{1}{N} \sum_{t=1}^{N} \ln p(x_t \mid x_1, \dots, x_{t-1}) \right).

In words: p(xt∣… )p(x_t \mid \dots) is the probability the model gave to the true token at position tt, given the tokens before it. The sum runs over all positions. The minus sign and the average turn it into a positive number, and the exponential converts that number back to a count.

Step 5 in symbols: the chain rule

The Chain Rule of probability says that the probability of the whole text is a product of conditional probabilities:

p(x1,…,xN)=∏t=1Np(xt∣x1,…,xt−1).p(x_1, \dots, x_N) = \prod_{t=1}^{N} p(x_t \mid x_1, \dots, x_{t-1}).

For the example, p=0.2⋅0.1⋅0.5⋅0.25=0.0025p = 0.2 \cdot 0.1 \cdot 0.5 \cdot 0.25 = 0.0025. A product of many numbers below 1 gets very small, so it is hard to read and hard to compare across texts of different lengths.

Steps 6 and 7 in symbols: the logarithm and the average

The Logarithm turns a product into a sum: ln⁡∏tpt=∑tln⁡pt\ln \prod_t p_t = \sum_t \ln p_t. Dividing by NN gives an average per token. Here is the same calculation as before:

−14(ln⁡0.2+ln⁡0.1+ln⁡0.5+ln⁡0.25)=−14ln⁡0.0025≈1.50.-\frac{1}{4} \left( \ln 0.2 + \ln 0.1 + \ln 0.5 + \ln 0.25 \right) = -\frac{1}{4} \ln 0.0025 \approx 1.50.

Exponentiating gives e1.50≈4.47e^{1.50} \approx 4.47, where ee is the base of the exponential. Because exp⁡(−1N∑tln⁡pt)=(∏tpt)−1/N\exp(-\tfrac{1}{N} \sum_t \ln p_t) = \left(\prod_t p_t\right)^{-1/N}, the perplexity is the inverse of the geometric mean of the token probabilities. This is why it is read as a branching factor.

The quantity inside the exponential is the average cross-entropy per token, measured in nats. So

PPL=exp⁡(cross-entropy per token).\mathrm{PPL} = \exp(\text{cross-entropy per token}).

Minimising the training loss of a language model and minimising perplexity are the same task. The two numbers differ only by the exponential. The loss is the one the optimiser uses. Perplexity is the same quantity read on a scale of choices.

Worked examples

Example
Example 1: uniform guessing (tiny)

A model knows nothing. For each of 3 tokens it gives every token of a 4-token vocabulary the same probability, 1/41/4.

  1. Each token has probability 1/4=0.251/4 = 0.25.
  2. Each logarithm is ln⁡0.25=−1.386\ln 0.25 = -1.386.
  3. The average over the 3 tokens is −1.386-1.386.
  4. The negative average is 1.3861.386, and e1.386=4e^{1.386} = 4.

Interpretation: a model that is as uncertain as a fair choice among 4 tokens has perplexity exactly 4. A perfect model that gives every true token probability 1 has perplexity exactly 1.

Example
Example 2: a two-token text (standard)

Take the text "yes no". A tiny model gives "yes" a probability of 0.7 at the start. After "yes", it gives "no" a probability of 0.4. The text has N=2N = 2 tokens.

  1. The probability of the text is 0.7⋅0.4=0.280.7 \cdot 0.4 = 0.28.
  2. The logarithms are ln⁡0.7=−0.357\ln 0.7 = -0.357 and ln⁡0.4=−0.916\ln 0.4 = -0.916. Their sum is −1.273-1.273.
  3. The average over 2 tokens is −0.636-0.636.
  4. The negative average is 0.6360.636, and e0.636≈1.89e^{0.636} \approx 1.89.

Interpretation: the model is about as uncertain as a choice between 1.9 equally likely tokens, which is less than a fair two-way choice. It is a good model of this tiny text, because it puts most of its probability on what actually happened.

Example
Example 3: two models on the same five-token sentence (realistic)

Two models are scored on the same held-out sentence of five tokens. The numbers below are invented for illustration, not measured. For each token, the table shows the probability each model gave to the true token.

TokenModel AModel B
10.250.10
20.050.02
30.500.30
40.100.05
50.400.20
  1. For Model A, the logarithms are −1.386-1.386, −2.996-2.996, −0.693-0.693, −2.303-2.303 and −0.916-0.916. Their sum is −8.294-8.294.
  2. The average over 5 tokens is −1.659-1.659, so the perplexity is e1.659≈5.25e^{1.659} \approx 5.25.
  3. For Model B, the logarithms are −2.303-2.303, −3.912-3.912, −1.204-1.204, −2.996-2.996 and −1.609-1.609. Their sum is −12.024-12.024.
  4. The average over 5 tokens is −2.405-2.405, so the perplexity is e2.405≈11.08e^{2.405} \approx 11.08.
  5. Model A has the lower perplexity, so it is the better predictor of this sentence. Its perplexity of 5.25 means it is about as uncertain as a choice among 5 tokens. Model B is about as uncertain as a choice among 11.

Interpretation: the two models differ by a factor of about two in effective choices, even though the per-token probabilities look close. The logarithm makes the comparison fair across tokens with very different probabilities, because a single very low probability weighs heavily in the average.

Try it: a model gives every token of a 5-token sentence probability 0.2. What is its perplexity? Answer: each logarithm is ln⁡0.2=−1.609\ln 0.2 = -1.609, so the average is −1.609-1.609. Its negative is 1.609=ln⁡51.609 = \ln 5, and eln⁡5=5e^{\ln 5} = 5. The perplexity is exactly 5.

Common mistakes and tradeoffs

It is tempting to think a lower perplexity always means better text. In fact perplexity measures how well the model predicts a specific held-out text. A model can have a low perplexity and still produce dull or false output, and a model tuned to chat can have a higher perplexity than a base model on the same text. Use it to compare models on the same data, and check generated text separately.

It is tempting to compare perplexities across different tokenisers. In fact perplexity is per token, so a model that splits words into more pieces gets a different number for the same text. Two models are comparable only when they use the same tokens, or when the perplexity is normalised to a per-character or per-byte quantity.

It is tempting to read perplexity as the number of words the model knows. In fact it is the effective number of choices the model faces at each step, averaged as a geometric mean of the token probabilities. A value of 5 means the model is as uncertain as a choice among 5 equally likely tokens on this text, not that it has a vocabulary of 5.

It is tempting to average the probabilities directly. In fact the average of the logarithms, then the exponential, is the right way. An arithmetic average of probabilities would let one confident prediction hide several very wrong ones.

When to use it: to track training, to compare models on the same held-out text, and to compare candidate language models for tasks such as speech recognition. When not to use it: to judge whether generated text is useful, true or safe. For those, use task benchmarks and human or model-based ratings. The main alternatives are the same loss reported on a different scale, and task accuracy, which is harder to compute but closer to what people want.

History

In 1951 Claude Shannon estimated how much information English carries by asking people to guess the next letter of printed text. His estimates were roughly one bit per letter. That work tied the prediction of text to Entropy, and it set the idea that a good predictor is a good compressor of language.

In the 1970s, speech recognition researchers at IBM, led by Frederick Jelinek, needed a way to say how hard a recognition task was for its language model. They used the exponential of the average log probability, and named it perplexity. The measure caught on because it had a plain reading: the average number of choices the model was deciding between.

Modern large models are trained with the same loss, and their held-out perplexity is still reported during training. The measure stayed in use because it is cheap, needs no task-specific labels, and works for any model that gives next-token probabilities.

Recap

  • A language model gives a probability to each next token, given the tokens before it.
  • The probability of a text is the product of those token probabilities, by the chain rule.
  • Perplexity takes the average of the logarithms of those probabilities over the tokens, negates it, and exponentiates. The result is the inverse of the Geometric Mean of the token probabilities.
  • A perplexity of kk means the model was, on average, as uncertain as a fair choice among kk tokens.
  • Perplexity is the exponential of the cross-entropy per token, so minimising the training loss and minimising perplexity are the same task.
  • Read next: N-gram Language Model, which computes these probabilities from counts, and Autoregressive Model, which is the general one-token-at-a-time setting that perplexity measures.

Check yourself

QuestionRecall: what is the perplexity of a model that gives every token probability 1/10?Show answer

The logarithm of each probability is ln⁡0.1=−2.303\ln 0.1 = -2.303. The average is −2.303-2.303, the negative is 2.303=ln⁡102.303 = \ln 10, and eln⁡10=10e^{\ln 10} = 10. The perplexity is 10, the same as a fair choice among 10 tokens.

QuestionComputation: a model gives probabilities 0.5, 0.25, and 0.125 to the three tokens of a text. What is its perplexity?Show answer

The product is 0.5⋅0.25⋅0.125=0.0156250.5 \cdot 0.25 \cdot 0.125 = 0.015625. The logarithms are −0.693-0.693, −1.386-1.386 and −2.079-2.079, summing to −4.159-4.159. The average over 3 tokens is −1.386-1.386. Its negative is 1.3861.386, and e1.386=4e^{1.386} = 4. The perplexity is 4.

QuestionWhy: why does perplexity average the logarithms of the probabilities instead of the probabilities themselves?Show answer

The logarithm turns the probability of the whole text, a product, into a sum, so it can be averaged over tokens. Averaging the raw probabilities would let one confident prediction hide several wrong ones, and it would not give the geometric mean that makes perplexity a count of choices. Taking the average of the logarithms also matches the cross-entropy loss that the model is trained on.

QuestionApplied: a team reports that its new model has a lower held-out perplexity than the old one, but the tokeniser was also changed. What should be checked before the claim is accepted?Show answer

Perplexity is measured per token, so a change in tokenisation changes the number even when the model predicts the same text equally well. Check that both models are scored on the same text with the same tokens, or convert both to a per-character or per-byte measure. Without that, the lower number does not show that the new model is better.

Sources