Welcome to the final module of our course. Having journeyed through the intricate processes of training, adapting, and aligning large language models—culminating in our last lesson on Direct Preference Optimization—we now pivot to a crucial, practical question: How do we measure how "good" these models are? This module is dedicated to model evaluation and deployment, and we begin with the most fundamental metric in language modeling: perplexity.
In this lesson, you will learn to define perplexity and explain its uses and limitations. We will trace its origins from information theory, build a strong mathematical and intuitive understanding of what it represents, and critically examine the scenarios where it is a valuable guide versus where it can be misleading. This will lay the groundwork for understanding why a broader suite of evaluation benchmarks, which we will cover next, is necessary.
The Problem with Simple Evaluation
Our first instinct when evaluating a model might be to use a familiar metric like accuracy. However, for language generation, this quickly falls apart. If a model is prompted with "I came home from a long day of work and sat on my...", there isn't one single "correct" next word. "Couch," "chair," "sofa," and even "floor" are all plausible completions, while "spaceship" or "eggplant" are not. Accuracy, which expects a single right answer, is ill-suited for this ambiguity.
A better approach is to ask: how well does a model's predicted probability distribution over the vocabulary align with the text that humans actually produce? A good language model should assign high probability to plausible sequences and very low probability to nonsensical ones. Perplexity is the standard metric that quantifies this alignment.
How you can use Perplexity to see how good a Language Model is [Lecture]
To start, watch this segment from a lecture by Jordan Boyd-Graber. It clearly explains why simple accuracy is not a suitable metric for language models and sets the stage for a more nuanced, distribution-based evaluation.
Watch the introduction from the beginning, which uses a clear example to illustrate the shortcomings of accuracy and introduces the core idea of comparing probability distributions.
Foundations in Information Theory
To understand perplexity, we must first visit its roots in information theory, a field pioneered by Claude Shannon. Your strong mathematical background will be an asset here. The central concept is entropy.
For a probability distribution over a vocabulary , the entropy is defined as:
Entropy measures the average uncertainty or "surprise" of the distribution. It can be interpreted as the average number of bits required to encode a word drawn from this distribution using an optimal compression scheme. A distribution with high entropy (like a uniform distribution) is highly unpredictable, while a distribution with low entropy (where one word is very likely) is predictable.
Now, imagine we have the true probability distribution of language, , and our language model's predicted distribution, . We can measure the mismatch between them using cross-entropy:
Cross-entropy represents the average number of bits needed to encode words from the true distribution if we use a coding scheme optimized for our model's distribution . It's always true that . The "extra" bits we need are a penalty for our model being an imperfect approximation of reality . Therefore, minimizing cross-entropy is equivalent to making our model's predictions better match the true distribution.
In practice, we don't know the true distribution . Instead, we approximate it using a large, held-out test set. The cross-entropy of our model on a test set simplifies to the average negative log-likelihood of the words in the set:
This is the core quantity we compute. A lower cross-entropy means our model assigned, on average, a higher probability to the words that actually appeared in the test data.
Perplexity: The Standard Metric for Evaluating Language Models
This article provides an excellent, step-by-step derivation of cross-entropy from the first principles of information theory, clarifying the concepts of entropy and surprisal along the way.
Read the sections From Cross-Entropy to Perplexity. Pay close attention to the distinction between entropy and cross-entropy and how the practical formula for cross-entropy is derived from the empirical distribution of the test set.
From Cross-Entropy to Perplexity
While cross-entropy is the quantity we optimize, reporting that a model has "2.7 bits per word" isn't very intuitive. Perplexity translates this bit-based measure into a more interpretable number.
Perplexity (PP) is defined as the exponentiation of the cross-entropy:

Why this transformation? Because each bit of uncertainty doubles the number of possibilities. A cross-entropy of 3 bits means our model is, on average, as "perplexed" as if it had to choose between equally likely options at each step. This gives us the most important intuition for perplexity: it is the effective branching factor.
If a language model has a perplexity of 100 on a test set, it means that at each point in the sequence, the model's uncertainty is equivalent to choosing uniformly from 100 possible next words. A lower perplexity indicates the model is less uncertain and is making more confident, accurate predictions.

By substituting the formula for cross-entropy, we get the direct computational formula for the perplexity of a test set :
This reveals that perplexity is the geometric mean of the inverse conditional probabilities of the words in the test set. In practice, to avoid numerical underflow from multiplying many small probabilities, we compute perplexity using the average log-likelihood (the cross-entropy):
Using and Interpreting Perplexity
The primary use of perplexity is to compare language models.
- Model Comparison: Given two models, A and B, trained on the same data, we can compute their perplexity on the same held-out test set. If Model A has a lower perplexity than Model B, we can conclude that Model A is a better probabilistic model of the language in that test set.
- Ablation Studies: It's used to measure the impact of architectural changes or training techniques. For instance, we could compare a model with 12 layers to one with 24 to see how depth affects perplexity.
The historical progression of language models can be tracked by their perplexity scores on standard benchmarks like the Penn Treebank (PTB). As architectures evolved from n-grams to LSTMs and now Transformers, perplexity scores have consistently dropped, reflecting our increasing ability to model the statistical patterns of language.
This video provides a complementary perspective, introducing perplexity through the lens of extrinsic vs. intrinsic evaluation and Claude Shannon's word prediction game.
Watch the introduction on evaluation types to understand where intrinsic metrics like perplexity fit. Focus on the segment explaining the branching factor intuition. It provides an excellent concrete example involving a phone routing system. Finally, observe the worked example of calculating perplexity for a sequence of random digits (random digits), which clearly shows how the branching factor of 10 emerges from the calculation.
The Critical Limitations of Perplexity
While perplexity is a powerful and fundamental metric, it is crucial to understand its limitations. Optimizing solely for perplexity does not guarantee a model that is useful, truthful, or safe.
-
Sensitivity to Tokenization and Vocabulary: Perplexity scores are only comparable if the models use the exact same vocabulary and tokenization method. A character-level model will naturally have a much lower perplexity than a word-level model because its "branching factor" (the size of the alphabet) is much smaller. This does not mean it's a better language model.
-
Domain Mismatch: A model trained on scientific papers will achieve a very high (poor) perplexity score when evaluated on social media text, and vice-versa. Perplexity measures a model's fit to a specific data distribution, so comparisons are only meaningful on in-domain test data.
-
It Doesn't Measure Factual Accuracy or Quality: A model can achieve a low perplexity by generating grammatically correct, plausible-sounding, yet factually incorrect or nonsensical text. It can also be rewarded for producing generic, repetitive, and uninteresting language, as common phrases have high probability. Perplexity has no understanding of truth or creativity.
-
Poor Correlation with some Downstream Tasks: While a lower perplexity often correlates with better performance on tasks that are close to language modeling (e.g., speech recognition), the correlation is much weaker for tasks that require reasoning, summarization, or classification. A model with slightly higher perplexity might actually perform better on a Q&A task if it has better world knowledge.
-
Handling of Unknown Words: In older word-level models, out-of-vocabulary (OOV) words were a major issue, as the model would have to assign some probability mass to an
<unk>token, which could skew results. While modern subword tokenization (like BPE, which we studied in Module 1) has largely mitigated this, the way a model tokenizes rare or unseen words can still affect its score.
How you can use Perplexity to see how good a Language Model is [Lecture]
Jordan Boyd-Graber's lecture concludes with an essential discussion on the pitfalls of perplexity, covering many of the limitations we've just discussed.
Watch the final segment on when not to use perplexity and the various factors that can make perplexity scores inconsistent and difficult to compare.
For a more rigorous and formal treatment of the information-theoretic foundations of perplexity, especially regarding the assumptions about the stochastic process of language (stationarity and ergodicity), the article "Perplexity of language models revisited" by Pirmin Lemberger is an excellent resource for those with a strong mathematical inclination. I recommend it as optional further reading if you are curious about the deeper theory.
Conclusion
In this lesson, we have dissected perplexity, the cornerstone metric for evaluating language models. You have seen that it is far more than a simple score; it is a direct application of information theory that provides a principled way to measure a model's probabilistic performance.
Here are the key takeaways:
- Perplexity is defined as , where cross-entropy is the average negative log-likelihood of the tokens in a test set.
- Its most powerful intuition is as the effective branching factor: the average number of choices the model faces at each step of a prediction.
- Lower perplexity is better, indicating that a model is less "perplexed" by the test data and assigns higher probabilities to the correct sequences.
- Perplexity is an intrinsic metric that is invaluable for comparing models and guiding development, but it has significant limitations.
- It is highly sensitive to tokenization, vocabulary, and data domain, and scores are not comparable across these different conditions.
- Crucially, perplexity does not measure factual accuracy, coherence, creativity, or safety, which are vital qualities for modern LLMs.
The limitations of perplexity highlight the need for a more holistic approach to evaluation. In our next lesson, we will explore the world of standardized evaluation benchmarks like MMLU and HELM, which assess models on a wide range of tasks to provide a more comprehensive picture of their capabilities.