Understanding Language Modeling Metrics
Understanding Language Modeling Metrics
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
Perplexity
Bits-per-Character (BPC)
Bits-per-Byte (BPB)
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.
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.
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
Four Tokens — Entropy 2
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
Distribution Divergence
Entropy and cross entropy share the same mathematical notation, . Let be the true distribution of the training data, and be the distribution learned by the language model. Accordingly, the following is true:
- The training data's entropy is, therefore, .
- The divergence of with respect to can be measured using the Kullback–Leibler (KL) divergence, which is mathematically represented as .
- The model's cross entropy with respect to the training data is therefore:
Cross entropy isn't symmetric. The cross entropy of with respect to — — is different from the cross entropy of with respect to — .
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 with respect to will then be . 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 bits, this language model needs 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 and on average, each token consists of characters, the BPC is .
One complication with BPC arises from different character encoding schemes. For example, with ASCII, each character is encoded using bits, but with UTF-8, a character can be encoded using anywhere between and 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 and each character is bits, or of a byte, then the BPB is .
Cross entropy tells us how efficient a language model will be at compressing text. If the BPB of a language model is , meaning it can represent each original byte ( bits) using 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 , its perplexity is defined as:
The perplexity of a language model (with the learned distribution Q) on this dataset is defined as:
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 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 , the base of natural logarithm.
If you use nat as the unit, perplexity is the exponential of :
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
<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
The Longer the Context Length, the Lower the Perplexity
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.
| Model | LAMBADA (PPL) | LAMBADA (ACC) | CBT-CN (ACC) | CBT-NE (ACC) | WikiText2 (PPL) | PTB (PPL) | enwiki8 (BPB) | text8 (BPC) | WikiText103 (PPL) | 1BW (PPL) |
|---|---|---|---|---|---|---|---|---|---|---|
| SOTA | 99.8 | 59.23 | 85.7 | 82.3 | 39.14 | 46.54 | 0.99 | 1.08 | 18.3 | 21.8 |
| 117M | 35.13 | 45.99 | 87.65 | 83.4 | 29.41 | 65.85 | 1.16 | 1.17 | 37.50 | 75.20 |
| 345M | 15.60 | 55.48 | 92.35 | 87.1 | 22.76 | 47.33 | 1.01 | 1.06 | 26.37 | 55.72 |
| 762M | 10.87 | 60.12 | 93.45 | 88.0 | 19.93 | 40.31 | 0.97 | 1.02 | 22.05 | 44.575 |
| 1542M | 8.63 | 63.24 | 93.30 | 89.05 | 18.34 | 35.76 | 0.93 | 0.98 | 17.48 | 42.16 |
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
Training Data Deduplication
Abnormal Text Detection
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 , and a sequence of tokens , 's perplexity for this sequence is:
where denotes the probability that assigns to the token given the previous tokens .
Challenges of Evaluating Foundation Models
Why evaluating foundation models is harder than traditional ML — intelligence, open-ended outputs, black boxes, saturating benchmarks, and expanding scope.
Exact Evaluation
How functional correctness, similarity against reference data, and embeddings produce exact scores for open-ended model outputs.