Cross-Entropy Loss › Worked Example
Language AI HandbookAll chapters Cross-Entropy Loss › Worked ExampleOne section · Sections in this chapter9 / 15 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Let's walk through a concrete calculation to solidify these concepts. Consider a tiny language model with a vocabulary of four tokens: {the, cat, sat, mat}. The model predicts the next token given the context "the cat". This simplified scenario allows us to trace through the calculations manually and verify that our mathematical understanding matches our computational implementation.
Token Probability Distribution
Suppose our model outputs the following probabilities for the next token:
| Token | Model Probability | True? |
|---|---|---|
the | 0.1 | No |
cat | 0.05 | No |
sat | 0.7 | Yes |
mat | 0.15 | No |
The true next token is sat.

The true next token is sat. The cross-entropy loss is simply:
where:
- : the cross-entropy loss for this single prediction
- : the model's predicted probability for the true token (
sat)
Converting to bits (dividing by ):
where:
- : the cross-entropy measured in bits
- : the model's predicted probability for the true token
The perplexity is:
where:
- : the exponential function with base
- : the cross-entropy in nats from the previous calculation
Or in base-2:
where:
- : the base of the logarithm (corresponding to bits)
- : the cross-entropy in bits from the previous calculation
This low perplexity indicates the model is quite confident and correct. If the model had predicted uniform probabilities (0.25 each):
where:
- : the cross-entropy loss for uniform prediction
- : the uniform probability for each of the 4 tokens
where:
- : the calculated perplexity value, equal to the vocabulary size for uniform distribution
This matches our intuition: uniform random guessing among 4 items yields perplexity 4. The model with 0.7 probability on the correct answer is effectively as uncertain as if it were choosing among only 1.43 equally likely options. This shows its high confidence.
Sequence-Level Calculation
Now consider a complete sentence: "the cat sat mat" (we omit the final punctuation for simplicity). The model processes this autoregressively, conditioning each prediction on all previous tokens:
- Context:
<bos>, True:the, Prob: 0.4, Loss: - Context:
the, True:cat, Prob: 0.3, Loss: - Context:
the cat, True:sat, Prob: 0.7, Loss: - Context:
the cat sat, True:mat, Prob: 0.5, Loss:
The average cross-entropy is:
where:
- : the average cross-entropy loss over the sequence
- : the individual token losses (negative log probabilities) for each position
- : the number of tokens in the sequence
Notice how token 2 ("cat") has the highest individual loss despite a moderate probability of 0.3. This reflects higher uncertainty at that position: many words could follow "the" in English. Token 3 ("sat") benefits from richer context and receives a much better prediction. This position-by-position view illustrates how the context window gradually reduces uncertainty as the model accumulates information.
The perplexity is:
where:
- : the exponential function with base
- : the average cross-entropy in nats
If the text contains 15 characters (including spaces), the bits per character would be:
where:
- : bits per character, the compression efficiency metric
- : the average cross-entropy loss in nats per token
- : the number of tokens in the sequence
- : the conversion factor from nats to bits ()
- : the total number of characters in the original text
Note the conversion factor: 1 nat = bits. This conversion is necessary because our initial cross-entropy was computed using natural logarithms, but BPC is traditionally reported in bits.