
Image by Author
If you’ve read about perplexity, you’ve already encountered a number that tells you how well a language model predicts text. What you may not have seen is the quantity sitting one step behind it, the loss function the model spends its entire training life minimizing. That quantity is cross-entropy loss.
Cross-entropy loss is the bridge between raw prediction and learning. Every time a model guesses the wrong word with high confidence, the loss spikes. Every time it assigns a high probability to the correct word, the loss drops. Over millions of training steps, minimizing this single number is what teaches a language model to generate coherent text.
By the end of this article, you’ll understand what cross-entropy loss measures, how it’s constructed, how it connects to perplexity, and why it’s the natural objective for next-word prediction.
What “Loss” Means in This Context
In machine learning, a loss function measures how wrong a model’s prediction is. You want this number to be small. When it’s large, the model is making poor predictions. When it approaches zero, the model is doing well.
For a language model, the task is next-word prediction. Given the words that came before, the model assigns a probability to every word in its vocabulary. One of those words is the correct next word. The loss function’s job is to penalize the model based on how much probability it assigned to that correct word.
This is a cleaner problem than it might sound. You don’t need to evaluate every possible completion, just one number: how likely did the model think the right answer was?
Building Cross-Entropy: Penalizing Low Probability on the Correct Word
The formula for cross-entropy loss on a single prediction is:
Loss = −log(p)
where p is the probability the model assigned to the correct word.
Take a moment to think through the shape of this. If the model assigned the correct word a probability of 1.0 (perfect certainty), then −log(1.0) = 0. No loss. If the model assigned it 0.5, then −log(0.5) ≈ 0.69. Some loss. If the model assigned it 0.01 (nearly ignoring the right answer), then −log(0.01) ≈ 4.6. Substantial loss.
The negative sign is what makes this work as a minimization objective. Probabilities live between 0 and 1, and the log of a number in that range is always negative. Negating it gives a positive loss value that rises as confidence in the correct answer falls.
The core intuition: cross-entropy loss punishes the model in proportion to how surprised it was by the correct answer. High surprise means high loss. Low surprise means low loss.
A Worked Example
Suppose your model is predicting the next word in “The cat sat on the ___”, and the correct answer is “mat”. Your vocabulary contains only three candidates: “mat”, “floor”, and “table”.
Your model outputs this probability distribution:
mat: 0.70 floor: 0.20 table: 0.10
The loss for this prediction is −log(0.70) ≈ 0.36. The model was fairly confident and mostly right, so the loss is low.
Now suppose the model had instead assigned:
mat: 0.10 floor: 0.50 table: 0.40
The loss becomes −log(0.10) ≈ 2.30. The model was confident in the wrong answers, so the loss is high.
This is what the training process responds to. When the loss is high, the model’s parameters get nudged in a direction that makes the correct answer more likely next time.
Averaging Across the Corpus
A single prediction gives you a single loss value. But a model trains on millions of sentences containing billions of words. To get a single training objective, you average the loss across every prediction in your training set:
Average Loss = (1/N) × Σ −log(pi)
where N is the total number of predictions and pi is the probability assigned to the correct word at each step.
This average is what the model actually minimizes during training. Optimizing it is equivalent to maximum likelihood estimation, finding the parameter settings that make the observed training data as probable as possible. Cross-entropy loss isn’t an arbitrary choice; it’s the natural consequence of asking a model to assign high probability to the text it was trained on.
The Connection to Perplexity
You can now see exactly where perplexity comes from. As we covered in The Concise Guide to Perplexity and Understanding Perplexity as a Statistical Measure of Language Model Performance, perplexity is defined as the exponential of average cross-entropy loss:
Perplexity = exp(Average Loss)
That’s the complete relationship. Perplexity and cross-entropy loss carry exactly the same information, lower loss equals lower perplexity equals a better model. Perplexity gets reported instead of raw loss mostly for interpretability: it reads as the effective vocabulary size the model is choosing from at each step, whereas a raw loss of 3.4 is harder to contextualize.
When you see a model’s perplexity score, you’re seeing cross-entropy loss in a different unit. And when a model trains, it’s minimizing cross-entropy loss to push that perplexity down.
Why Lower Loss Is Not Always Better
Here’s an important caveat: a model can drive its training loss very close to zero by essentially memorizing the training data. On text it’s never seen before, though, that model may perform poorly. It learned the surface of its training corpus rather than the underlying patterns of language.
This is overfitting, and it’s why measuring loss on a held-out validation set matters as much as tracking training loss. A model that’s genuinely learning generalizable patterns will show falling loss on both sets. A model that’s memorizing will show low training loss and rising validation loss.
That tension between fitting well and generalizing well is the next topic in this series.
Conclusion
Cross-entropy loss is the number a language model spends its entire training process trying to reduce. It measures how surprised the model was by the correct next word, rising sharply when the model was confident in the wrong answer, falling when it was confident in the right one. Averaging this across a training corpus gives the objective the model optimizes, and exponentiating that average gives perplexity, the evaluation metric covered in our earlier guides. Understanding cross-entropy is understanding what training a language model actually means.
