Perplexity

Perplexity is a measure of how well a language model predicts a text: the lower the perplexity, the less surprised the model is by each next token.

A language model assigns a probability to every possible next token. Perplexity summarises how much probability it gave to the tokens that actually came next across a whole text. Hugging Face's documentation defines it as the exponentiated average negative log-likelihood of a sequence, which is the same as the exponential of the cross-entropy between the data and the model's predictions. A model that predicts the text well gets a low perplexity; one that is often surprised gets a high one.

There is a useful intuition. Google's machine learning glossary describes perplexity, for a keyboard that suggests word completions, as roughly the number of guesses you would need to offer for the list to contain the word the user is typing. A perplexity of 20 means the model is, on average, about as uncertain as if it were choosing among 20 equally likely options at each step.

Perplexity is mainly a development tool. Researchers watch it fall during pre-training and compare it on held-out text to see whether a change helps. Papers have long reported it on standard text collections; the GPT-2 paper, for example, gave a perplexity on WikiText-2. Hugging Face's worked example shows how much method matters: GPT-2 large scored 19.44 on WikiText-2 when the text was cut into separate chunks, and 16.44 when a sliding window gave the model more context, with the same model and data.

It has firm limits. Hugging Face notes that perplexity applies to autoregressive models that predict the next token, and is not well defined for masked models such as BERT. Because it is calculated per token, models with different tokenisers cannot be compared fairly on raw perplexity. And a low perplexity says the model predicts typical text well, not that its answers are true, helpful or safe, which is why chat models are judged mainly on benchmarks and human preference.

Common questions

Is a lower perplexity better?

Yes. Lower perplexity means the model assigned higher probability to the text that actually occurred, so it predicted that text better. Comparisons are only meaningful on the same evaluation text, with the same tokeniser and the same calculation method.

How is perplexity calculated?

Take the model's predicted probability for each actual token in a text, average the negative logarithms of those probabilities, and raise e to that average. This equals the exponential of the cross-entropy. For long texts, the model's context limit means the text is processed in windows, and the windowing choice changes the result.

Can you compare perplexity between different language models?

Only with care. Perplexity is measured per token, so models that split text into tokens differently produce numbers that are not directly comparable. The evaluation data and the windowing method also have to match.

Does low perplexity mean a model gives accurate answers?

No. Perplexity measures how well a model predicts text like its evaluation data. A model can predict fluent text well and still state falsehoods or follow instructions poorly, so practical quality is measured with task benchmarks and human or model-based judging.

Finding more

Search for "perplexity language model", "perplexity cross-entropy" or "perplexity of fixed-length models". Library documentation such as Hugging Face's, machine learning glossaries from major providers, and language modelling papers that state exactly how they computed perplexity are the most dependable. When comparing reported figures, check the dataset, tokeniser and context window used.

Working out what this means for your business?

Aileron Shift builds the systems behind this site: AI newsrooms that research, write and publish on their own, marketing automation and WordPress hosting for companies that need to move faster than their competitors.