Concepts / Cross-entropy loss

Cross-entropy loss

Cross-entropy loss is one measure used for this evaluation.

  • Programming

Judging a Model by Its Next-Token Predictions

A language model is evaluated by how well its predicted probability distribution matches the words that actually appear in a dataset. At each position, the model spreads probability across candidate next tokens, and the evaluation asks how much weight it gave to the token that really came next. Cross-entropy loss is one measure used for this evaluation. Perplexity is another way to read the same result.

What do you think happens?

Suppose the model assigns probability 0.9 to the token that actually comes next. Do you expect the perplexity to be close to 1 or close to 20?

  • Close to 1

  • Close to 20

Reveal answer
Answer:

Close to 1 (about 1.11)

A confident, correct prediction gives a loss of about 0.105, and exponentiating that loss gives a perplexity of about 1.11. Perplexity stays near 1 when the model is nearly certain. The value 20 would describe a model that is about as uncertain as choosing among 20 equally likely tokens.

How Next-Token Uncertainty Shows Up in Probabilities

Perplexity expresses the model's uncertainty about which token comes next. The clearest way to see that uncertainty is in the shape of the probability distribution. When the model puts most of its probability on one candidate, the distribution is peaked and the model is confident. When it spreads probability across many candidates, the distribution is flat and the model is uncertain. The diagram below compares these two cases for the same context.

catprobability 0.90catprobability 0.25dogprobability 0.05dogprobability 0.25other tokensprobability 0.05 combinedbirdprobability 0.25fishprobability 0.25
How does the probability mass over candidate next tokens change between a confident prediction and an uncertain one?

In the confident column, almost all the probability sits on one token, so a model that is right would have a small loss and a perplexity near 1. In the uncertain column, the probability is shared among four tokens, so the model is less sure which one comes next, and its perplexity is larger. The shape of the distribution, not any single number on its own, is what perplexity summarizes.

From Loss to Perplexity

Perplexity is calculated from loss. The source states that it is obtained by taking the exponential of the loss, written in PyTorch as torch.exp(loss). In mathematical notation, if the loss is $L$, then the perplexity is $e^{L}$. Because the exponential undoes the logarithm inside the loss, perplexity returns the result to a scale that is easier to read as a degree of uncertainty.

Probability ofcorrect tokenfor each positionNegativelog-probabilityone value per tokenAverage across tokensthis average is thecross-entropy lossExponentiatetorch.exp(loss)Perplexityeffective uncertaintytake log, negateaverageexpresult
What sequence of steps turns the probability given to each correct token into cross-entropy loss, and then into perplexity?

Read the pipeline from left to right. Each correct next token receives a probability. A higher probability means a smaller negative log-probability, so a confident correct prediction contributes little to the loss. The contributions are averaged across the sequence to give the cross-entropy loss. Exponentiating that loss gives the perplexity. A lower perplexity means the model's predictions sit closer to the distribution of the words actually found in the dataset.

Computing Perplexity from a Loss Value

The following example shows the calculation in code. Suppose a model gives the correct next token a probability of 0.05 at every position, which is what a model would do if it were choosing uniformly among 20 tokens. The per-token loss is then the negative natural logarithm of 0.05, which equals the natural logarithm of 20. Taking the exponential of that loss returns 20.

python
Output (expected)
2.9957
20.0
Worked example

Perplexity for a confident model

The model gives the correct next token probability 0.9 at every position. What is the cross-entropy loss and the perplexity?

  1. 1
    Negative log-probability

    The per-token loss is $-\ln(0.9)$, which is about 0.1054.

  2. 2
    Average loss

    Every position contributes the same value, so the average cross-entropy loss is also about 0.1054.

  3. 3
    Exponentiate

    The perplexity is $e^{0.1054}$, which is about 1.11.

  4. 4
    Interpret

    A perplexity near 1 reflects a nearly certain prediction, in contrast to the value 20 from the uniform case above.

Result

Cross-entropy loss is about 0.105 and perplexity is about 1.11.

The same code works for any sequence of correct-token probabilities. Only the list of probabilities changes. The average of the negative log-probabilities is the loss, and the exponential of that loss is the perplexity.

Reading Perplexity as Effective Vocabulary Size

Perplexity can be read as an effective vocabulary size. A perplexity of 20 means the model is about as uncertain about the next token as it would be if it were choosing among 20 equally likely tokens. This is a helpful way to think about uncertainty, but it is only an analogy. It does not mean that the model is literally considering exactly 20 candidate tokens, nor does it mean the model has any particular set of 20 tokens in mind.

Two distributions can have the same perplexity while looking different in detail. One model might put most of its probability on a few tokens with a long tail of small values, while another spreads the probability more evenly. Perplexity compresses both into one number that reflects overall uncertainty, which is why it should be read as an effective size rather than as a list of candidates.

Mistakes When Interpreting Perplexity

  • Common mistake

    Treating perplexity as the exact number of tokens the model considers

    What it looks like

    Reading a perplexity of 20 as meaning the model looks at exactly 20 candidate next tokens.

    Why it fails

    Perplexity is an effective-vocabulary interpretation of uncertainty, not a literal count. The model still assigns probability across its whole vocabulary.

    Do this instead

    Read 20 as meaning the uncertainty is about the same as choosing uniformly among 20 equally likely tokens.

  • Common mistake

    Reporting the cross-entropy loss as if it were perplexity

    What it looks like

    Stating a loss of 2.9957 as the perplexity of the model.

    Why it fails

    Perplexity is calculated from loss by exponentiation, so the two values are different quantities on different scales.

    Do this instead

    Apply torch.exp to the loss to obtain the perplexity, which gives 20.0 in the uniform example.

  • Common mistake

    Ignoring the shape of the distribution

    What it looks like

    Judging a prediction only by the probability of the single correct token and not by how the rest of the probability is spread.

    Why it fails

    Perplexity reflects the uncertainty expressed by the distribution as a whole, and a peaked and a spread-out distribution can differ sharply in how uncertain they are.

    Do this instead

    Compare the full distributions when you want to understand why perplexity is high or low.

  • Common mistake

    Assuming a perplexity of 1 is typical for any real language model, when it only corresponds to a case where the model is nearly certain at every position

Checking Your Reading of Perplexity

EASY

A model assigns probability 0.25 to the correct next token at every position of a sequence. Compute the cross-entropy loss and the perplexity, and then explain in one sentence what the perplexity value means.

Hints
  • The per-token loss is the negative natural logarithm of 0.25.
  • Perplexity is the exponential of the loss, so exponentiating the loss should return the reciprocal of the probability in this uniform case.
  • Compare your answer with the effective vocabulary reading: how many equally likely choices would produce the same uncertainty?

Key Takeaways

  • Perplexity evaluates a language model by expressing its uncertainty about the next token.
  • Perplexity is calculated by exponentiating the cross-entropy loss, written in PyTorch as torch.exp(loss).
  • Lower perplexity indicates predictions closer to the actual distribution of words in the dataset.
  • Perplexity can be read as an effective vocabulary size, which is an interpretation of uncertainty and not a literal count of candidate tokens.

Related concepts

More concepts starting with C