Concepts / Cross Entropy

Cross Entropy

Perplexity is the exponential of entropy or cross entropy, not a relabeled version of them.

  • Programming

Why Perplexity Gets Confused with Cross Entropy

When a language model is evaluated, one of the most frequently reported figures is perplexity. Reading that figure with confidence requires knowing what it measures and how it connects to cross entropy, the quantity it is most often confused with. This article starts from that confusion, tests it, and then builds the correct relationship from the ground up.

What do you think happens?

Is perplexity simply another name for cross entropy, the same measurement with a different label?

  • Yes, they are the same number

  • No, perplexity is derived from cross entropy by exponentiation

Reveal answer
Answer:

No, perplexity is derived from cross entropy by exponentiation, so the two values are generally different numbers.

Both describe how well a model predicts the next token, which makes them feel like the same quantity. But perplexity applies an exponential to cross entropy, so a cross entropy of 2 bits corresponds to a perplexity of 4, not 2.

Cross Entropy as Prediction Difficulty

Imagine you are shown the first part of a sentence from a large collection of text and asked to guess the next word. Some sentences make the next word obvious, while 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.

Definition

Cross entropy measures how difficult it is for a language model to predict what comes next in a dataset. Written H(P, Q), it captures the difficulty of predicting the next token when the data follows the true distribution P and the model has learned the distribution Q.

The idea is useful in two directions. 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 of these ideas in view at once.

Entropy of the Data and the KL Term

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$ is the true distribution of the training data.

Notice that 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 the model changes the true distribution of the data.

Now bring in the model. It has learned its own distribution, $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, 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

Read the equation as a sum of two costs. The first term, $H(P)$, is the unavoidable amount of uncertainty the data carries. The second term, $D_{KL}(P \| Q)$, is the Kullback-Leibler divergence, 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.

Cross entropyH(P,Q)what the model paysData entropy H(P)fixed by the dataKL divergenceD_KL(P||Q)reducible by learningplusplus
Which part of cross entropy is fixed by the data, and which part can the model reduce through learning?

Worked Example with Two Outcomes

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)$, meaning it strongly believes A is coming. Measure everything in bits, which means base-2 logarithms.

Worked example

Splitting Cross Entropy for the Two-Outcome Case

Find the data entropy H(P), the cross entropy H(P,Q), and the KL divergence, then check that H(P,Q) = H(P) + D_KL(P||Q).

  1. 1
    Data entropy

    $H(P) = -(0.5\log_2 0.5 + 0.5\log_2 0.5) = 1$ bit. This is the uncertainty the data carries, and no model can remove it.

  2. 2
    Cross entropy

    $H(P,Q) = -(0.5\log_2 0.9 + 0.5\log_2 0.1) \approx 1.737$ bits. The data is scored using the model's beliefs.

  3. 3
    KL divergence

    $D_{KL}(P\|Q) = H(P,Q) - H(P) \approx 1.737 - 1 = 0.737$ bits. This is the extra cost from the model's overconfidence in A.

  4. 4
    Check the split

    $1 + 0.737 = 1.737$, which matches $H(P,Q)$. The split holds.

Result

Cross entropy is about 1.737 bits, made of 1 bit of unavoidable data entropy plus about 0.737 bits of KL divergence.

Notice what the example reveals. The data has an entropy of 1 bit that no model can remove. The model's overconfidence in A adds roughly 0.737 bits on top. A model that learned Q equal to P would add nothing, and its cross entropy would sit right at the data's entropy.

From Cross Entropy to Perplexity

Perplexity keeps the same measurement as cross entropy but expresses it as an exponential. Given a dataset with true distribution $P$, the perplexity is the base 2 exponential of the entropy $H(P)$. For a language model with learned distribution $Q$ on that same dataset, the perplexity is the base 2 exponential of the cross entropy $H(P,Q)$.

Rule

Because the exponential grows quickly, a cross entropy of 2 bits corresponds to a perplexity of 4, a cross entropy of 3 bits corresponds to a perplexity of 8, and each additional bit doubles the perplexity. The relationship is one of exponentiation, not of relabeling.

Cross entropy3 bitsApply base-matchedexponential2 raised to the valuePerplexity8exponentiateresult
What steps convert a cross entropy value into a perplexity value?

Reading Perplexity as a Count of Options

The reason exponentiation is the right operation becomes clear when you think about options. Uncertainty about the next token means there are several candidates the model could pick. A useful test case is a model trained to encode four position tokens perfectly, and then asked to predict a position inside a square. Each bit distinguishes two unique values, so the number of equally likely options grows as a power of 2 when cross entropy grows in bits.

Token 1probability 0.25Token 2probability 0.25Token 3probability 0.25Token 4probability 0.25
If perplexity is 4, the model is as uncertain as choosing uniformly among four next tokens.

Higher perplexity indicates more uncertainty, meaning more possible options for the next token. A perplexity of 4 means the model faces the equivalent of four equally likely choices, as in the diagram above. A model that is uncertain across many more options will report a larger perplexity.

Bits, Nats, and Matching the Base

So far the base has been 2, which matches the idea that each bit distinguishes two unique values. The unit, however, is a convention rather than a law of nature. Popular machine learning frameworks, including TensorFlow and PyTorch, measure entropy and cross entropy in nats. A nat uses the base of e, the base of the natural logarithm. When nats are the unit, perplexity becomes the base e exponential of cross entropy.

Rule

Unit of cross entropyBase of perplexity exponentialPerplexity for the two-outcome model
Bits22 raised to 1.737 is about 3.33
Natsee raised to 1.204 is about 3.33
Nats, but base 2 used by mistake22 raised to 1.204 is about 2.3, which is wrong

The same model gives the same perplexity only when the base matches the unit.

python
Output (expected)
1.204
3.33
3.33
2.3
Notes on the code
  • Converting bits to nats multiplies by the natural log of 2.
  • Base 2 applied to the bit value gives the correct perplexity.
  • Base e applied to the nat value gives the same perplexity.
  • Base 2 applied to the nat value gives a different, wrong number.

Why the Order of the Distributions 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 of Q with respect to P, written H(P,Q), differs from the cross entropy of P with respect to Q, written H(Q,P). The order of the arguments tells you whose beliefs are being scored against whose.

You can verify this with the same two-outcome numbers. Scoring the data P using the model's beliefs Q gave about 1.737 bits. Swapping the roles gives H(Q,P) = -(0.9 log2 0.5 + 0.1 log2 0.5) = 1 bit. The two values are different, so the notation carries real meaning.

H(P,Q)about 1.737 bitsH(Q,P)1 bit
Which distribution supplies the weights, and what does each ordering score?

The standard convention places the true data distribution first, so $H(P,Q)$ means the data is weighted by its own probabilities while the model's beliefs supply the scoring. Keeping the order fixed is what makes the split into data entropy plus KL divergence valid.

Common Mistakes with Cross Entropy and Perplexity

  • Common mistake

    Treating perplexity and cross entropy as the same number

    What it looks like

    Reporting a cross entropy of 3 bits as if the perplexity were also 3.

    Why it fails

    Perplexity is the exponential of cross entropy, so 3 bits corresponds to a perplexity of 8.

    Do this instead

    Apply the exponential with the matching base to convert cross entropy into perplexity.

  • Common mistake

    Mixing units when computing perplexity

    What it looks like

    Taking 1.204 nats and raising 2 to that power to get about 2.3.

    Why it fails

    The base must match the unit. Nats require base e, so the same model gives about 3.33.

    Do this instead

    Check whether the cross entropy is in bits or nats before choosing the base.

  • Common mistake

    Assuming cross entropy is symmetric

    What it looks like

    Believing H(P,Q) and H(Q,P) must be equal for any pair of distributions.

    Why it fails

    In the two-outcome example, H(P,Q) is about 1.737 bits while H(Q,P) is 1 bit.

    Do this instead

    Keep the argument order fixed, with the true data distribution first.

  • Common mistake

    Expecting a model to push cross entropy below the data entropy

    Why it fails

    Cross entropy equals the data entropy plus a KL term, so the data entropy acts as a floor that no model can go beneath.

    Do this instead

    Treat the data entropy as the best achievable value and measure the model's progress through the KL term.

Practice Applying the Split and the Exponential

EASY

A language model reports a cross entropy of 3 bits per token on a dataset. What is its perplexity, and how many equally likely options does that correspond to?

Hints
  • Perplexity is 2 raised to the cross entropy when the unit is bits.
  • Each additional bit doubles the perplexity.
MEDIUM

For P = (0.5, 0.5) and Q = (0.9, 0.1), a second model learns Q = (0.5, 0.5). Using bits, what is the KL divergence, and what is the cross entropy? Check that the split holds.

Hints
  • When Q equals P, the KL divergence is exactly 0.
  • The cross entropy should then equal the data entropy of 1 bit.

Key Takeaways

  • Perplexity is the exponential of cross entropy, so 2 bits corresponds to a perplexity of 4 and 3 bits to a perplexity of 8. It is not the same number as cross entropy.
  • Cross entropy equals the entropy of the training data plus the KL divergence of the model from the true distribution. The data entropy is a floor, and the KL term is what learning reduces.
  • The base of the exponential must match the unit: base 2 for bits and base e for nats, otherwise the same model gives a wrong perplexity.
  • A perplexity value can be read as the number of equally likely options the model faces when predicting the next token.
  • Cross entropy is not symmetric, so H(P,Q) and H(Q,P) generally differ, and the order of the distributions must be kept fixed.

Related concepts

More concepts starting with C