Evaluation Methodology

Understanding Language Modeling Metrics

How entropy, cross entropy, perplexity, and BPC/BPB measure a language model's predictive accuracy and how they relate.

Understanding Language Modeling Metrics

Foundation models evolved out of language models. Many foundation models still have language models as their main components. For these models, the performance of the language model component tends to be well correlated to the foundation model's performance on downstream applications (Liu et al., 2023). Therefore, a rough understanding of language modeling metrics can be quite helpful in understanding downstream performance.
While there's a strong correlation, language modeling performance doesn't fully explain downstream performance. This is an active area of research.

Why Language Modeling Metrics Matter

As discussed in Chapter 1, language modeling has been around for decades, popularized by Claude Shannon in his 1951 paper "Prediction and Entropy of Printed English". The metrics used to guide the development of language models haven't changed much since then. Most autoregressive language models are trained using cross entropy or its relative, perplexity. When reading papers and model reports, you might also come across bits-per-character (BPC) and bits-per-byte (BPB); both are variations of cross entropy.

All four metrics — cross entropy, perplexity, BPC, and BPB — are closely related. If you know the value of one, you can compute the other three, given the necessary information.

Cross Entropy

Most autoregressive language models are trained using cross entropy.

Perplexity

A relative of cross entropy. Most autoregressive language models are trained using cross entropy or its relative, perplexity.

Bits-per-Character (BPC)

A variation of cross entropy you might come across in papers and model reports.

Bits-per-Byte (BPB)

Another variation of cross entropy you might come across in papers and model reports.
While I refer to them as language modeling metrics, they can be used for any model that generates sequences of tokens, including non-text tokens.

Recall that a language model encodes statistical information (how likely a token is to appear in a given context) about languages. Statistically, given the context "I like drinking __", the next word is more likely to be "tea" than "charcoal". The more statistical information that a model can capture, the better it is at predicting the next token.

In ML lingo, a language model learns the distribution of its training data. The better this model learns, the better it is at predicting what comes next in the training data, and the lower its training cross entropy. As with any ML model, you care about its performance not just on the training data but also on your production data. In general, the closer your data is to a model's training data, the better the model can perform on your data.

Compared to the rest of the book, this section is math-heavy. If you find it confusing, feel free to skip the math part and focus on the discussion of how to interpret these metrics.
Even if you're not training or finetuning language models, understanding these metrics can help with evaluating which models to use for your application. These metrics can occasionally be used for certain evaluation and data deduplication techniques, as discussed throughout this book.

Entropy

Entropy measures how much information, on average, a token carries. The higher the entropy, the more information each token carries, and the more bits are needed to represent a token.

As discussed in Chapter 1, a token can be a character, a word, or part of a word. When Claude Shannon introduced entropy in 1951, the tokens he worked with were characters. Here's entropy in his own words:

The entropy is a statistical parameter which measures, in a certain sense, how much information is produced on the average for each letter of a text in the language. If the language is translated into binary digits (0 or 1) in the most efficient way, the entropy is the average number of binary digits required per letter of the original language.

Let's use a simple example to illustrate this. Imagine you want to create a language to describe positions within a square, as shown in Figure 3-4.

Figure 3-4. Two languages describe positions within a square. Compared to the language on the left (a), the tokens on the right (b) carry more information, but they need more bits to represent them.

Two Tokens — Entropy 1

If your language has only two tokens, shown as (a) in Figure 3-4, each token can tell you whether the position is upper or lower. Since there are only two tokens, one bit is sufficient to represent them. The entropy of this language is, therefore, 1.

Four Tokens — Entropy 2

If your language has four tokens, shown as (b) in Figure 3-4, each token can give you a more specific position: upper-left, upper-right, lower-left, or lower-right. However, since there are now four tokens, you need two bits to represent them. The entropy of this language is 2. This language has higher entropy, since each token carries more information, but each token requires more bits to represent.

Intuitively, entropy measures how difficult it is to predict what comes next in a language. The lower a language's entropy (the less information a token of a language carries), the more predictable that language. In our previous example, the language with only two tokens is easier to predict than the language with four (you have to predict among only two possible tokens compared to four). This is similar to how, if you can perfectly predict what I will say next, what I say carries no new information.

Cross Entropy

When you train a language model on a dataset, your goal is to get the model to learn the distribution of this training data. In other words, your goal is to get the model to predict what comes next in the training data. A language model's cross entropy on a dataset measures how difficult it is for the language model to predict what comes next in this dataset.

A model's cross entropy on the training data depends on two qualities:

Training Data Predictability

Measured by the training data's entropy.

Distribution Divergence

How the distribution captured by the language model diverges from the true distribution of the training data.

Entropy and cross entropy share the same mathematical notation, HH. Let PP be the true distribution of the training data, and QQ be the distribution learned by the language model. Accordingly, the following is true:

  • The training data's entropy is, therefore, H(P)H(P).
  • The divergence of QQ with respect to PP can be measured using the Kullback–Leibler (KL) divergence, which is mathematically represented as DKL(PQ)D_{\text{KL}}(P \parallel Q).
  • The model's cross entropy with respect to the training data is therefore:

H(P,Q)=H(P)+DKL(PQ)H(P, Q) = H(P) + D_{\text{KL}}(P \parallel Q)

Cross entropy isn't symmetric. The cross entropy of QQ with respect to PPH(P,Q)H(P, Q) — is different from the cross entropy of PP with respect to QQH(Q,P)H(Q, P).

A language model is trained to minimize its cross entropy with respect to the training data. If the language model learns perfectly from its training data, the model's cross entropy will be exactly the same as the entropy of the training data. The KL divergence of QQ with respect to PP will then be 00. You can think of a model's cross entropy as its approximation of the entropy of its training data.

Bits-per-Character and Bits-per-Byte

One unit of entropy and cross entropy is bits. If the cross entropy of a language model is 66 bits, this language model needs 66 bits to represent each token.

Since different models have different tokenization methods — for example, one model uses words as tokens and another uses characters as tokens — the number of bits per token isn't comparable across models. Some use the number of bits-per-character (BPC) instead. If the number of bits per token is 66 and on average, each token consists of 22 characters, the BPC is 6/2=36/2 = 3.

One complication with BPC arises from different character encoding schemes. For example, with ASCII, each character is encoded using 77 bits, but with UTF-8, a character can be encoded using anywhere between 88 and 3232 bits. A more standardized metric would be bits-per-byte (BPB), the number of bits a language model needs to represent one byte of the original training data. If the BPC is 33 and each character is 77 bits, or 7/87/8 of a byte, then the BPB is 3/(7/8)=3.433 / (7/8) = 3.43.

Cross entropy tells us how efficient a language model will be at compressing text. If the BPB of a language model is 3.433.43, meaning it can represent each original byte (88 bits) using 3.433.43 bits, this language model can compress the original training text to less than half the text's original size.

Perplexity

Perplexity is the exponential of entropy and cross entropy. Perplexity is often shortened to PPL. Given a dataset with the true distribution PP, its perplexity is defined as:

PPL(P)=2H(P)\text{PPL}(P) = 2^{H(P)}

The perplexity of a language model (with the learned distribution Q) on this dataset is defined as:

PPL(P,Q)=2H(P,Q)\text{PPL}(P, Q) = 2^{H(P, Q)}

If cross entropy measures how difficult it is for a model to predict the next token, perplexity measures the amount of uncertainty it has when predicting the next token. Higher uncertainty means there are more possible options for the next token.

Consider a language model trained to encode the 4 position tokens, as in Figure 3-4 (b), perfectly. The cross entropy of this language model is 2 bits. If this language model tries to predict a position in the square, it has to choose among 22=42^2 = 4 possible options. Thus, this language model has a perplexity of 4.

So far, I've been using bit as the unit for entropy and cross entropy. Each bit can represent 2 unique values, hence the base of 2 in the preceding perplexity equation.

Popular ML frameworks, including TensorFlow and PyTorch, use nat (natural log) as the unit for entropy and cross entropy. Nat uses the base of ee, the base of natural logarithm.

One reason many people might prefer natural log over log base 22 is because natural log has certain properties that makes its math easier. For example, the derivative of natural log ln(x)\ln(x) is 1/x1/x.

If you use nat as the unit, perplexity is the exponential of ee:

PPL(P,Q)=eH(P,Q)\text{PPL}(P, Q) = e^{H(P, Q)}

Due to the confusion around bit and nat, many people report perplexity, instead of cross entropy, when reporting their language models' performance.

Perplexity Interpretation and Use Cases

As discussed, cross entropy, perplexity, BPC, and BPB are variations of language models' predictive accuracy measurements. The more accurately a model can predict a text, the lower these metrics are. In this book, I'll use perplexity as the default language modeling metric. Remember that the more uncertainty the model has in predicting what comes next in a given dataset, the higher the perplexity.

What's considered a good value for perplexity depends on the data itself and how exactly perplexity is computed, such as how many previous tokens a model has access to. Here are some general rules:

More Structured Data Gives Lower Expected Perplexity

More structured data is more predictable. For example, HTML code is more predictable than everyday text. If you see an opening HTML tag like <head>, you can predict that there should be a closing tag, </head>, nearby. Therefore, the expected perplexity of a model on HTML code should be lower than the expected perplexity of a model on everyday text.

The Bigger the Vocabulary, the Higher the Perplexity

Intuitively, the more possible tokens there are, the harder it is for the model to predict the next token. For example, a model's perplexity on a children's book will likely be lower than the same model's perplexity on War and Peace. For the same dataset, say in English, character-based perplexity (predicting the next character) will be lower than word-based perplexity (predicting the next word), because the number of possible characters is smaller than the number of possible words.

The Longer the Context Length, the Lower the Perplexity

The more context a model has, the less uncertainty it will have in predicting the next token. In 1951, Claude Shannon evaluated his model's cross entropy by using it to predict the next token conditioned on up to 10 previous tokens. As of this writing, a model's perplexity can typically be computed and conditioned on between 500 and 10,000 previous tokens, and possibly more, upperbounded by the model's maximum context length.
For reference, it's not uncommon to see perplexity values as low as 3 or even lower. If all tokens in a hypothetical language have an equal chance of happening, a perplexity of 3 means that this model has a 1 in 3 chance of predicting the next token correctly. Given that a model's vocabulary is in the order of 10,000s and 100,000s, these odds are incredible.

Other than guiding the training of language models, perplexity is useful in many parts of an AI engineering workflow. First, perplexity is a good proxy for a model's capabilities. If a model's bad at predicting the next token, its performance on downstream tasks will also likely be bad. OpenAI's GPT-2 report shows that larger models, which are also more powerful models, consistently give lower perplexity on a range of datasets, as shown in Table 3-1. Sadly, following the trend of companies being increasingly more secretive about their models, many have stopped reporting their models' perplexity.

Table 3-1. Larger GPT-2 models consistently give lower perplexity on different datasets. Source: OpenAI, 2018.

ModelLAMBADA (PPL)LAMBADA (ACC)CBT-CN (ACC)CBT-NE (ACC)WikiText2 (PPL)PTB (PPL)enwiki8 (BPB)text8 (BPC)WikiText103 (PPL)1BW (PPL)
SOTA99.859.2385.782.339.1446.540.991.0818.321.8
117M35.1345.9987.6583.429.4165.851.161.1737.5075.20
345M15.6055.4892.3587.122.7647.331.011.0626.3755.72
762M10.8760.1293.4588.019.9340.310.971.0222.0544.575
1542M8.6363.2493.3089.0518.3435.760.930.9817.4842.16
Perplexity might not be a great proxy to evaluate models that have been post-trained using techniques like SFT and RLHF. Post-training is about teaching models how to complete tasks. As a model gets better at completing tasks, it might get worse at predicting the next tokens. A language model's perplexity typically increases after post-training. Some people say that post-training collapses entropy. Similarly, quantization — a technique that reduces a model's numerical precision and, with it, its memory footprint — can also change a model's perplexity in unexpected ways.If you're unsure what SFT (supervised finetuning) and RLHF (reinforcement learning from human feedback) mean, revisit Chapter 2. Quantization is discussed in Chapter 7.

Recall that the perplexity of a model with respect to a text measures how difficult it is for this model to predict this text. For a given model, perplexity is the lowest for texts that the model has seen and memorized during training. Therefore, perplexity can be used to detect whether a text was in a model's training data.

Data Contamination

If a model's perplexity on a benchmark's data is low, this benchmark was likely included in the model's training data, making the model's performance on this benchmark less trustworthy.

Training Data Deduplication

Perplexity can also be used for deduplication of training data: e.g., add new data to the existing training dataset only if the perplexity of the new data is high.

Abnormal Text Detection

Perplexity is the highest for unpredictable texts, such as texts expressing unusual ideas (like "my dog teaches quantum physics in his free time") or gibberish (like "home cat go eye"). Therefore, perplexity can be used to detect abnormal texts.

Perplexity and its related metrics help us understand the performance of the underlying language model, which is a proxy for understanding the model's performance on downstream tasks. The rest of the chapter discusses how to measure a model's performance on downstream tasks directly.

How to Use a Language Model to Compute a Text's Perplexity

A model's perplexity with respect to a text measures how difficult it is for the model to predict that text. Given a language model XX, and a sequence of tokens [x1,x2,,xn][x_1, x_2, \dots, x_n], XX's perplexity for this sequence is:

P(x1,x2,,xn)1n=(1P(x1,x2,,xn))1n=(i=1n1P(xix1,,xi1))1nP(x_1, x_2, \dots, x_n)^{-\frac{1}{n}} = \left( \frac{1}{P(x_1, x_2, \dots, x_n)} \right)^{\frac{1}{n}} = \left( \prod_{i=1}^n \frac{1}{P(x_i \mid x_1, \dots, x_{i-1})} \right)^{\frac{1}{n}}

where P(xix1,,xi1)P(x_i \mid x_1, \dots, x_{i-1}) denotes the probability that XX assigns to the token xix_i given the previous tokens x1,,xi1x_1, \dots, x_{i-1}.

To compute perplexity, you need access to the probabilities (or logprobs) the language model assigns to each next token. Unfortunately, not all commercial models expose their models' logprobs, as discussed in Chapter 2.
Copyright © 2026