Perplexity is the standard evaluation metric for language models โ a direct, exponentiated transformation of cross-entropy loss that's easier to interpret intuitively than a raw loss number.
Formula
\(L\) is exactly the average cross-entropy loss (see Cross-Entropy) of the language model's predictions across a sequence of \(N\) tokens, each predicted given the tokens before it. Perplexity is simply \(e\) raised to this loss value.
An Intuitive Interpretation
Perplexity can be understood as: the effective number of equally likely choices the model is "confused between," on average, at each token position. A perplexity of 1 means the model is perfectly certain about every next token (predicts it with probability 1). A perplexity of 50 means the model's uncertainty at each position is roughly as if it were choosing uniformly at random among 50 equally likely options โ even though, in practice, the actual probability distribution is rarely perfectly uniform.
Numerical Example
If a language model's average cross-entropy loss on a test set is \(L=2.3\) nats:
Roughly speaking, the model is about as uncertain, on average, as if picking among 10 equally likely next tokens at each position.
Code
import torch
import torch.nn.functional as F
logits = torch.randn(1, 5, 10000) # (batch, sequence_length, vocab_size) -- a toy example
targets = torch.randint(0, 10000, (1, 5))
loss = F.cross_entropy(logits.view(-1, 10000), targets.view(-1))
perplexity = torch.exp(loss)
print(f"Loss: {loss.item():.4f}, Perplexity: {perplexity.item():.4f}")
Why Lower Perplexity Is Better
Since perplexity is a monotonically increasing function of cross-entropy loss, minimizing loss during training directly minimizes perplexity โ they're two ways of describing the exact same underlying optimization target, with perplexity simply presented on a more intuitively interpretable scale (an effective "branching factor" of uncertainty, rather than a raw log-probability number).
Common Mistakes
- Comparing perplexity values across models evaluated with different tokenization schemes (e.g. word-level vs subword/BPE tokenization) โ since perplexity is computed per-token, differing token granularity makes raw perplexity numbers not directly comparable across such models.
- Treating a "good" perplexity value as universal across tasks โ what counts as a strong perplexity score depends heavily on the specific dataset, domain, and vocabulary size.
Interview Relevance
Q: "What does a language model's perplexity intuitively represent?" It represents the effective number of equally likely choices the model is uncertain between, on average, when predicting each next token โ computed as \(e\) raised to the average cross-entropy loss. Lower perplexity means the model assigns higher probability to the actual next tokens in a test set, reflecting better predictive performance; perplexity and cross-entropy loss are monotonically related, so minimizing one minimizes the other.
Practice Question
If Model A has cross-entropy loss 1.5 nats and Model B has cross-entropy loss 3.0 nats on the same test set, compute both models' perplexities and state which model is performing better.
Related DL Notes
Want to go beyond the notes?
Join CodingNow 2.0's Deep Learning course โ live mentorship, real projects, and 100% placement support.
Enroll Now โ Free Demo Available