Perplexity is a metric used to evaluate how well a language model predicts a sequence of tokens. It measures the model's uncertainty when predicting the next token based on the preceding context. A lower perplexity generally means the model assigns higher probabilities to the actual tokens in the sequence, indicating better predictive performance on that evaluation data.
- Prediction Performance: Lower perplexity means the model assigns higher probabilities to the actual tokens in the evaluation data.
- Model Comparison: Perplexity can help compare models when they are evaluated on the same dataset and under the same tokenization setup.
- Training & Fine-Tuning: Monitoring perplexity can help track changes in a model's predictive performance during training or fine-tuning.

Calculation
Perplexity is calculated from the probabilities that a language model assigns to the actual tokens in a sequence. The main steps are:
- Predict the Next Token: The language model assigns probabilities to possible next tokens based on the preceding context.
- Calculate Log Probability: The logarithm of the probability assigned to the actual next token is calculated.
- Compute Average Negative Log-Likelihood: The negative log probabilities are averaged across the evaluated tokens.
- Exponentiate the Result: The exponential of the average negative log-likelihood gives the perplexity.
For a sequence of (N) tokens, perplexity is calculated as:
\exp\left( -\frac{1}{N} \sum_{i=1}^{N} \log p(w_i \mid w_1,w_2,\ldots,w_{i-1}) \right)
where:
p(w_i \mid w_1,\ldots,w_{i-1}) is the probability assigned to the (i)th token.- (N) is the total number of evaluated tokens.
When natural logarithms are used, perplexity is the exponential of the average negative log-likelihood. A lower value means the model assigned higher probabilities to the actual tokens in the sequence.
Calculating Perplexity for LLM Evaluation
Step 1: Import Required Libraries
The following libraries are used to load GPT-2 and perform tensor calculations.
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
Step 2: Load Pre-Trained GPT-2 Model and Tokenizer
In this step, we load the pre-trained GPT-2 model and tokenizer.
- AutoTokenizer.from_pretrained(): Loads the tokenizer associated with GPT-2.
- AutoModelForCausalLM.from_pretrained(): Loads GPT-2 for causal language modeling.
- tokenizer.pad_token = tokenizer.eos_token: Uses the end-of-sequence token as the padding token because GPT-2 does not have a default padding token.
model_name = "gpt2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
# Assign the EOS token as the padding token
tokenizer.pad_token = tokenizer.eos_token
Step 3: Define the Perplexity Calculation Function
The following function calculates perplexity for each input text. Padding tokens are excluded using the attention mask.
def compute_perplexity_for_batch(input_texts):
inputs = tokenizer(
input_texts, return_tensors="pt", padding=True, truncation=True
)
input_ids = inputs["input_ids"]
attention_mask = inputs["attention_mask"]
with torch.no_grad():
outputs = model(input_ids, attention_mask=attention_mask)
logits = outputs.logits
shift_logits = logits[:, :-1, :]
shift_labels = input_ids[:, 1:]
log_probs = torch.nn.functional.log_softmax(shift_logits, dim=-1)
target_log_probs = log_probs.gather(
dim=-1, index=shift_labels.unsqueeze(-1)
).squeeze(-1)
target_log_probs = target_log_probs * attention_mask[:, 1:].to(log_probs.dtype)
negative_log_likelihood = -target_log_probs.sum(dim=-1) / attention_mask[:, 1:].sum(dim=-1)
perplexities = torch.exp(negative_log_likelihood)
mean_perplexity_score = torch.mean(perplexities)
return {
"perplexities": perplexities.tolist(),
"mean_perplexity": mean_perplexity_score.item()
}
The logits and labels are shifted by one position because a causal language model uses the preceding tokens to predict the next token. The function then extracts the log probability assigned to each actual token and converts the average negative log-likelihood into perplexity.
Step 4: Run the Example
We can now calculate perplexity for two sample texts.
example_texts = [
"Once upon a time, there was a brave knight.",
"In a galaxy far, far away, a new adventure began."
]
# Compute perplexity scores for the batch of input texts
results = compute_perplexity_for_batch(example_texts)
print(f"Perplexity scores for each text: {results['perplexities']}")
print(f"Mean perplexity score: {results['mean_perplexity']}")
Output:
Perplexity scores for each text: [25.607038497924805, 18.611230850219727]
Mean perplexity score: 22.109134674072266
Note: The exact values may vary depending on the model and library versions.
Interpreting the Results
- Text 1: A perplexity of 25.61 indicates that GPT-2 assigned lower probabilities, on average, to the actual tokens in this sequence compared with Text 2.
- Text 2: A perplexity of 18.61 indicates that GPT-2 predicted the tokens in this sequence with lower uncertainty.
- Mean Perplexity: The mean of the two individual perplexity scores is 22.11.
A lower perplexity between the two texts indicates better predictive performance for that particular text. It should not, however, be interpreted as a direct measure of overall text quality.
You can download the complete source code from here.
Using Perplexity with Other Evaluation Metrics
- BLEU and ROUGE: Measure similarity between generated text and reference text in tasks such as machine translation and summarization.
- Human Evaluation: Assesses qualities such as coherence, relevance, fluency and helpfulness.
- Factuality Evaluation: Checks whether generated information is accurate and supported by reliable sources.
- Task-Specific Benchmarks: Measure capabilities such as question answering, reasoning and instruction following.
- Safety and Bias Evaluation: Identifies harmful, biased or otherwise unsafe model behavior.
Applications
- Language Modeling: Evaluates how well a model predicts tokens in unseen text.
- Domain Adaptation: Compares a model's predictive performance across different domains or types of text.
- Fine-Tuning Analysis: Helps determine how well a model adapts to a target dataset.
- Model Development: Helps researchers analyze changes in language-model performance during experimentation.
- Language Generation Research: Serves as one metric for studying and comparing language generation models.
Limitations
- Does Not Measure Understanding: A low perplexity score does not mean that a model understands the meaning of the text.
- Does Not Measure Overall Quality: Perplexity does not directly assess factuality, relevance, helpfulness, reasoning or safety.
- Sensitive to Tokenization: Scores depend on the tokenizer, making comparisons between models with different tokenization schemes difficult.
- Depends on Evaluation Data: Perplexity can vary considerably across datasets and domains.
- Limited for Task-Specific Evaluation: It does not directly evaluate capabilities such as question answering or instruction following.