Concepts / Evaluating Language Model Performance

Evaluating Language Model Performance

Entropy never refers to the model. It belongs to the data. Two different models trained on the same dataset would face the same entropy value, because nothing about a model changes the true distribution of the data.

  • Programming

Guessing the Next Word

Imagine you are shown the first part of a sentence taken from a large collection of text and asked to guess the next word. Some sentences make the next word obvious. Others leave you with many reasonable options. A language model faces this same task every time it is trained on a dataset. Its job is to become good at guessing what comes next, and cross entropy is the number that tells you how hard that guessing job is for the model on a given dataset.

Cross entropy is useful in two directions at once. It describes a property of the data itself, namely how predictable the data is. It also describes how well the model's guesses line up with that data. Learning to read cross entropy means keeping both ideas in view together. This article builds that skill step by step, tracing the numbers at each stage so you can see exactly where each part of the score comes from.

What the Data Alone Tells You

Start with the data alone, before any model enters the picture. If the next item in a dataset is almost always the same, the data is very predictable and there is little uncertainty to deal with. If the next item could be any of many options with roughly equal likelihood, the data is hard to predict. The quantity that captures this predictability is the entropy of the training data, written $H(P)$, where $P$ stands for the true distribution of the training data.

Definition

The entropy of the training data, written H(P), is the amount of uncertainty the data itself carries about what comes next. It is computed from the true distribution P over the possible next items.

Formula

Read the formula one outcome at a time. For each possible next item x, take its true probability P(x), multiply it by the base-2 logarithm of that probability, add all of these terms together, and flip the sign so the result is positive. Outcomes that are very likely contribute little uncertainty, while spread-out probabilities produce a larger total.

Key point

Entropy never refers to the model. It belongs to the data. Two different models trained on the same dataset would face the same entropy value, because nothing about a model changes the true distribution of the data.

Bringing in the Model's Beliefs

Now bring in the model. It has learned its own distribution, which we call Q, over the possible next items. When Q assigns high probability to what actually comes next, the model is doing well. When Q spreads its probability onto options that rarely occur in the data, or puts little weight on what really happens, the model pays for it. Cross entropy measures this full cost: the difficulty of predicting the data, judged using the model's own beliefs Q rather than the true distribution P.

Formula
  1. 1
    Pick one possible next item x, and look up how often it truly occurs in the data, P(x).
  2. 2
    Look up how much probability the model gives that same item, Q(x).
  3. 3
    Take the base-2 logarithm of the model's probability, log2 Q(x). A small model probability gives a large negative logarithm, which means a large cost.
  4. 4
    Multiply that logarithm by the true frequency P(x), so common outcomes count more than rare ones.
  5. 5
    Repeat for every outcome, add the terms together, and flip the overall sign to get a positive score.

The steps show why the weights come from the data while the logarithms come from the model. Outcomes that happen often in the data are weighted heavily, and the model is penalized most when it gives those frequent outcomes little probability. This is the mechanism behind a confident wrong guess raising the score.

The Split into Data Entropy and KL Divergence

The key relationship ties the three quantities together. The cross entropy of the model equals the entropy of the data plus a second term that measures how far the model's distribution has drifted from the true one. That second term is the Kullback-Leibler divergence, written $D_{KL}(P \| Q)$, which measures how far the model's distribution Q is from the true distribution P.

Formula

Read the equation as a sum of two costs. The first term, H(P), is the uncertainty the data carries, and no model can remove it. The second term, the KL divergence, is the extra cost caused by the model's distribution differing from the truth. Whatever the model does, it cannot push cross entropy below the entropy of the data. The KL term is the only part the model can shrink through learning.

This gives a helpful way to read cross entropy. You can think of a model's cross entropy as its approximation of the entropy of its training data. A cross entropy that sits well above the data's entropy signals a gap that the model has not yet closed.

Tracing the Two-Outcome Example

Suppose the next item in a dataset is one of only two outcomes, call them A and B. In the true data, A and B each occur half the time, so P = (0.5, 0.5). A model has learned Q = (0.9, 0.1), which means it strongly believes A is coming. All quantities are measured in bits, which means base-2 logarithms are used throughout.

Worked example

Splitting cross entropy for a two-outcome dataset

Compute the entropy of the data H(P), the cross entropy H(P,Q), and the KL divergence D_KL(P || Q) for P = (0.5, 0.5) and Q = (0.9, 0.1), then check that H(P,Q) = H(P) + D_KL(P || Q).

  1. 1
    Entropy of the data

    H(P) = -(0.5 log2 0.5 + 0.5 log2 0.5) = -(0.5 times -1 + 0.5 times -1) = 1 bit. The data has one bit of uncertainty that no model can remove.

  2. 2
    Cross entropy with the model's beliefs

    H(P,Q) = -(0.5 log2 0.9 + 0.5 log2 0.1). Since log2 0.9 is about -0.152 and log2 0.1 is about -3.322, this is 0.5 times 0.152 plus 0.5 times 3.322, which is about 0.076 + 1.661 = 1.737 bits.

  3. 3
    KL divergence of the model from the truth

    D_KL(P || Q) = 0.5 log2(0.5/0.9) + 0.5 log2(0.5/0.1) = 0.5 times (-0.848) + 0.5 times 2.322, which is about -0.424 + 1.161 = 0.737 bits.

  4. 4
    Check the split

    H(P) + D_KL(P || Q) = 1 + 0.737 = 1.737, which matches H(P,Q). The split holds for this example.

Result

The data entropy is 1 bit, the cross entropy is about 1.737 bits, and the KL divergence is about 0.737 bits. The model's overconfidence in A adds roughly 0.737 bits on top of the unavoidable 1 bit.

Notice what the example reveals. A model that learned Q equal to P would add nothing to the 1 bit of data entropy, and its cross entropy would sit right at the data's entropy. Here the model's miscalibration is visible as the extra 0.737 bits.

Why the Order of Arguments Matters

A tempting shortcut is to assume that the cross entropy between two distributions is the same no matter which one you treat as the true distribution. It is not. Cross entropy is not symmetric. The cross entropy written H(P,Q) differs from the cross entropy written H(Q,P). The order of the arguments tells you whose beliefs are being scored against whose.

What do you think happens?

Using the same two-outcome numbers, P = (0.5, 0.5) and Q = (0.9, 0.1), what do you expect H(Q,P) to equal when the roles are swapped? Make your guess before reading the answer.

Reveal answer
Answer:

H(Q,P) = -(0.9 log2 0.5 + 0.1 log2 0.5) = 1 bit.

The weights now come from Q while the logarithms come from P. Since P assigns 0.5 to both outcomes, every logarithm is -1, so the result is 0.9 plus 0.1, which is exactly 1 bit. This differs from H(P,Q), which was about 1.737 bits, so the order of the arguments carries real meaning.

The order tells you whose beliefs are being scored against whose. H(P,Q) asks how well the model Q explains data distributed as P. H(Q,P) swaps those roles. Because the two expressions weight the terms differently, they produce different numbers, so writing the arguments in the wrong order changes the meaning of the score.

Mistakes Beginners Make with Cross Entropy

  • Common mistake

    Treating cross entropy as a property of the model alone

    What it looks like

    Saying that a different model trained on the same dataset must have a different data entropy.

    Why it fails

    Entropy belongs to the data. The true distribution P does not change when the model changes.

    Do this instead

    Attribute H(P) to the dataset, and attribute the KL term to the mismatch between the model Q and the data P.

  • Common mistake

    Assuming H(P,Q) equals H(Q,P)

    What it looks like

    Computing H(P,Q) as about 1.737 bits and then reporting the same value for H(Q,P).

    Why it fails

    Cross entropy is not symmetric. For the two-outcome example, H(Q,P) equals 1 bit.

    Do this instead

    Keep the argument order fixed and label which distribution supplies the weights and which supplies the logarithms.

  • Common mistake

    Expecting cross entropy to fall below the data's entropy

    What it looks like

    Believing a very good model could score 0.8 bits on data whose entropy is 1 bit.

    Why it fails

    The entropy of the data is the lower bound on cross entropy, and the KL divergence is never negative in this setup, so the total cannot drop below H(P).

    Do this instead

    When a score sits near the data entropy, the remaining gap is the KL divergence, which is the only part the model can still shrink.

  • Common mistake

    Forgetting that the rare outcomes still count through the logarithm

    What it looks like

    Ignoring outcome B because it has only a 0.1 model probability in the example.

    Why it fails

    Outcome B occurs half the time in the data, so the model's small probability on it contributes 1.661 bits, the largest single term.

    Do this instead

    Compute every outcome's term, weighted by how often it truly occurs.

Checking the Split on a Certain Case

MEDIUM

Take the same two outcomes with P = (0.5, 0.5). Suppose a model learns Q = (0.5, 0.5) exactly. Without computing every logarithm, predict the value of D_KL(P || Q) and of H(P,Q). Then check your answer by computing H(P,Q) = -(0.5 log2 0.5 + 0.5 log2 0.5) and confirm that the split H(P,Q) = H(P) + D_KL(P || Q) still holds.

Hints
  • Think about what a perfect model looks like. Training pushes Q toward P, and a model that has learned the data perfectly makes the KL divergence exactly 0.
  • When Q equals P, the cross entropy should equal the data's entropy. Compare your value with the H(P) you computed in the worked example.
  • Each term in H(P,Q) is -0.5 times log2 0.5, and log2 0.5 equals -1.
HARD

Using P = (0.5, 0.5) and Q = (0.9, 0.1), decide which of the two outcomes contributes more to the KL divergence and explain why in one sentence. Then state whether the model's score would be closer to the data entropy if the model had put 0.5 on outcome B instead of 0.1.

Hints
  • Look at the size of the terms 0.5 log2(0.5/0.9) and 0.5 log2(0.5/0.1) from the worked example.
  • A model that assigns 0.5 to each outcome matches the data exactly.

Key Takeaways

  • Cross entropy measures how difficult it is for a language model to predict what comes next in a dataset, using the model's own beliefs Q against the true distribution P of the data.
  • Cross entropy splits into the entropy of the training data, H(P), plus the KL divergence of the model's distribution from the true one, D_KL(P || Q). The entropy term is an irreducible floor, and the KL term is the part training can shrink.
  • For P = (0.5, 0.5) and Q = (0.9, 0.1), the entropy is 1 bit, the cross entropy is about 1.737 bits, and the KL divergence is about 0.737 bits, and these values satisfy the split.
  • Cross entropy is not symmetric, so H(P,Q) and H(Q,P) can differ; in the same example H(Q,P) equals 1 bit, so the order of the two distributions must be stated explicitly.

Related concepts

More concepts starting with E