Perplexity is an evaluation metric that expresses how well a language model predicts the next token of a text as a single number: "how many choices, on average, the model is torn between." It averages the log of the probabilities assigned to the correct tokens, flips the sign, and takes the exponential.
Here is the evaluation text ( tokens), and is the probability the model predicts for coming next, given the preceding tokens. The value is at least 1, and smaller means a better model. A perfect model that assigns probability 1 to every token scores 1, and a model that picks uniformly at random from a vocabulary of tokens every time scores .
The probability of the whole text is not compared directly because it shrinks as the text gets longer, so texts of different lengths cannot be compared. Perplexity divides by the number of tokens (takes the geometric mean) to remove the effect of length.
Perplexity originated in 1977, when Jelinek et al. proposed it as a measure of the difficulty of speech recognition tasks. Today it is widely used to evaluate language models, from n-gram models to large language models.

