What is perplexity in a language model?

Authors
What is perplexity in a language model?

Perplexity is a number that measures how confused a language model is when it predicts the next word. Lower perplexity means the model predicts text better.

In this blog, we will learn about perplexity in a language model. We will also see how a model predicts the next word, why we need a number to grade a model, how perplexity works like a guessing game, a step-by-step example with its formula and code, and why it is called an intrinsic metric.

We will cover the following:

  • What does "predicting the next word" mean?
  • The problem: how do we grade a model?
  • What is perplexity?
  • Perplexity as a guessing game
  • A simple step-by-step example
  • The formula behind perplexity
  • Low perplexity vs high perplexity
  • Why perplexity is called an intrinsic metric

I am Amit Shekhar, Founder @ Outcome School, I have taught and mentored many developers, and their efforts landed them high-paying tech jobs, helped many tech companies in solving their unique problems, and created many open-source libraries being used by top companies. I am passionate about sharing knowledge through open-source, blogs, and videos.

I teach AI and Machine Learning at Outcome School.

Let's get started.

What does "predicting the next word" mean?

A language model predicts the next word in a sentence. When it reads a sentence, it does not just pick one single word. Instead, it gives a probability to every possible next word.

A probability is a number between 0 and 1 that tells us how likely something is. A probability of 1 means "fully sure", and a probability close to 0 means "very unlikely".

Consider the sentence "The sky is ___". The model gives probabilities like below:

Next wordProbability
blue0.70
clear0.15
falling0.05
banana0.001

Here, we can see that the model gives "blue" a high probability of 0.70, and it gives "banana" a tiny probability of 0.001.

This makes sense. "The sky is blue" is common, and "The sky is banana" is not.

So, the model is confident about "blue". It is not confused. Perplexity measures this confusion.

The problem: how do we grade a model?

Now, suppose we build ten different language models. Some are good and some are bad.

The big question is: how do we measure which model is better?

We need a single number that tells us how good a model is at predicting text. A low number should mean a good model, and a high number should mean a bad model.

So, here comes perplexity to the rescue.

What is perplexity?

Perplexity is a number that measures how confused a language model is when it predicts the next word.

It tells us how "surprised" the model is when it sees the real next word.

To "perplex" someone means to confuse them. So, perplexity is a measure of confusion.

  • A low perplexity means the model is confident and rarely surprised. This is good.
  • A high perplexity means the model is confused and often surprised. This is bad.

So, we want perplexity to be as low as possible.

Perplexity as a guessing game

The best way to learn this is by taking an example.

Let's say we are playing a guessing game. I show a sentence and hide the next word. We have to guess it.

Now, imagine perplexity as the number of options we are stuck between when we guess.

Perplexity = 1     ->  We know the exact answer. No confusion.
Perplexity = 2     ->  We are stuck between 2 equally likely words.
Perplexity = 10    ->  We are stuck between 10 equally likely words.
Perplexity = 1000  ->  We are stuck between 1000 words. Very confused.

Here, we can see that perplexity is like the average number of choices the model feels stuck between at each step.

If the model always knows the answer, its perplexity is close to 1. If the model is guessing between thousands of words, its perplexity is huge.

So, lower perplexity means fewer choices to be confused about, which means a better model.

Planning your move into AI?

No matter which tech domain you work in today, with your effort, you can transition to these roles:

  • AI Engineer
  • Forward Deployed Engineer
  • AI Agent Engineer
  • Harness Engineer
  • Voice AI Engineer
  • AI Architect
  • LLM Engineer
  • LLM Inference Engineer
  • Gen AI Engineer
  • MLOps Engineer
  • ML Engineer
  • Deep Learning Engineer

Our AI and Machine Learning Program at Outcome School takes you there.

A simple step-by-step example

Let's take a real sentence and see how the model reacts. Consider the sentence "I love my dog".

The model reads it word by word. At each step, it gives a probability to the real next word.

Assume that our model gives these probabilities:

StepPredicting next wordProbability
Step 1I0.5
Step 2love0.5
Step 3my0.5
Step 4dog0.5

Here, we can see that the model gave every real word a probability of 0.5. This means the model was, on average, choosing between 2 words at each step.

So, the perplexity here is 2.

Now, let's take a better model that gives each real word a probability of 1.0. That model is never surprised. Its perplexity is 1. This is a perfect model.

And, a very bad model that gives each real word a probability of 0.01 would be extremely surprised. Its perplexity would be 100. This model is very confused.

A quick note for you

No matter which tech domain you work in, get familiar with these topics:

  • LLM
  • RAG
  • MCP
  • Agent
  • Fine-tuning
  • Quantization

We put it all together in one video:

AI Engineering Explained: LLM, RAG, MCP, Agent, Fine-Tuning, and Quantization

No need to stop reading - bookmark it and watch later when you get time. Future you will thank you.

Now, let's get back to the topic.

The formula behind perplexity

Perplexity is built on one idea: take the probability the model gave to the real words, and turn it into a "number of choices".

The formula is written like below:

Perplexity = (1 / P)^(1/N)

Here:

  • P is the probability the model gave to the whole real text.
  • N is the number of words (tokens) in the text.

Here, we can see two parts.

First, 1 / P flips the probability. If the probability is small, then 1 / P becomes large. A small probability means the model was surprised, so a large value is correct.

Second, the ^(1/N) part takes a kind of average across all the words. This is why perplexity feels like an "average number of choices per word", no matter how long the text is.

In practice, we can compute it in code like below:

import math

# probabilities the model gave to each real next word
probs = [0.5, 0.5, 0.5, 0.5]

n = len(probs)

# add up the log of each probability
log_sum = 0
for p in probs:
    log_sum = log_sum + math.log(p)

# average log probability, then undo the log
avg_log = log_sum / n
perplexity = math.exp(-avg_log)

print(perplexity)   # prints 2.0

Here, we have taken the four probabilities. We add their logarithms, take the average, and flip the sign. Then math.exp turns it back into a normal number.

It prints 2.0, which matches our earlier example. The model was choosing between 2 words on average. It works perfectly.

To learn LLM Fundamentals and LLM Internals, and build a Large Language Model (LLM) from scratch, check out our AI and Machine Learning Program at Outcome School.

Low perplexity vs high perplexity

Let me tabulate the difference between low perplexity and high perplexity for your better understanding.

PropertyLow PerplexityHigh Perplexity
Model confidenceHighLow
Model surpriseRareOften
Prediction skillStrongWeak
MeaningGood modelBad model

So, when we compare two models on the same text, the model with the lower perplexity is the better one at predicting that text. The comparison is fair only when both models are tested on the same text and split the text into the same pieces.

For example, if Model A has a perplexity of 20 and Model B has a perplexity of 45 on the same test text, then Model A is better. It is less confused.

A low perplexity does not always mean the model is good at a real task like answering questions. For that, we also use task-based tests, which we will see in the next section.

Why perplexity is called an intrinsic metric

Now, let's understand one more important word. Perplexity is called an intrinsic metric.

An intrinsic metric measures the model by itself, without using any outside task.

In simple words, we do not ask the model to answer questions or write emails to judge it. We only check one thing: how well does it predict the text we give it.

Let's compare the two kinds of testing like below:

Intrinsic test (perplexity):
   Model  ->  predict the next word  ->  measure confusion
   No real-world task needed.

Extrinsic test (task based):
   Model  ->  do a real task (translation, chat)  ->  measure task score
   A real-world task is needed.

Here, we can see the difference. An intrinsic test looks inside the model at its core skill of prediction. An extrinsic test checks how the model performs on a real job.

Perplexity is intrinsic because it only measures the prediction skill.

Now we must have understood what perplexity is, how it is calculated, and why a lower value means a model predicts text better.

Stay updated: Subscribe to our newsletter to get our latest AI and Machine Learning blogs straight to your inbox.

Frequently Asked Questions

Is a lower perplexity always better?

Yes, when we compare models on the same text. A lower perplexity means the model is less confused and rarely surprised, so it predicts that text better. For example, if Model A has a perplexity of 20 and Model B has a perplexity of 45 on the same test text, then Model A is better.

What is the lowest possible perplexity?

The lowest possible perplexity is 1. A perplexity of 1 means the model gives every real word a probability of 1.0, so it is never surprised. This is a perfect model that always knows the exact next word.

Can we compare perplexity across any two models?

No. The comparison is fair only when both models are tested on the same text and split the text into the same pieces. If the test text or the way it is split into pieces is different, the perplexity numbers cannot be compared directly.

Does a low perplexity mean the model is good at real tasks?

No. A low perplexity does not always mean the model is good at a real task like answering questions. Perplexity is an intrinsic metric that only measures the prediction skill. To check real jobs like translation or chat, we also use extrinsic, task-based tests that measure a task score.

That's it for now.

Thanks

Amit Shekhar
Founder @ Outcome School

You can connect with me on:

Follow Outcome School on:

Read all of our high-quality blogs here.