Skip to content
The Daily Triptych004 / 365
Cross-entropy loss

Loss drops steeply as the model assigns more probability to the correct token, approaching zero at certainty and rising without bound as probability approaches zero.

II · THE IDEA · ARTIFICIAL INTELLIGENCE

Loss: How a Model Knows It Is Wrong

Foundations · Cross-entropy, negative log-likelihood · Single scalar per prediction

▶ Listen · narrated

A model produces a distribution over fifty thousand possible next tokens. The right one appears. How do you convert that moment into a number you can descend?

At a glance

What it measures
How surprised the model should be when the actual token appears
Range
Zero (perfect certainty on the right answer) to infinity (assigned zero probability)
Calculated from
Negative logarithm of the probability the model gave to the correct token
Used for
Computing gradients that adjust every weight in the network

Imagine you are guessing which card someone will draw from a deck. If you say the ace of spades and you are right, you feel no surprise. If you said it was impossible and the ace appears, you are shocked. Loss measures that surprise as a number. The model produces a probability for every possible next token. Loss looks at the probability it gave to the token that actually came next, takes the logarithm, and flips the sign. High probability on the right answer means low loss. Low probability means high loss. That single number is what training tries to reduce. Every weight in the network is adjusted to make the model a little less surprised next time.

Look closer

  1. The calculation is simpler than the name suggests

    Suppose the model outputs a probability distribution and assigns 0.7 to the token that actually comes next. The loss for that prediction is negative log of 0.7, which is roughly 0.36. If it had assigned 0.9, the loss would be negative log of 0.9, about 0.11. If it assigned 0.01, the loss would be negative log of 0.01, about 4.6. The logarithm grows without bound as the probability approaches zero, so confidently predicting the wrong thing is penalised severely. Confidently predicting the right thing yields a loss near zero.

  2. The model never sees anything but this single number

    After each prediction, the loss is a scalar. That scalar is differentiated with respect to every weight in the network, hundreds of millions or billions of them, producing a gradient for each. Those gradients are what the optimiser uses to nudge the weights. The model has no access to the semantic content of what it got wrong, no representation of the gap between what it said and what the text required. It receives only the magnitude of its surprise, encoded as a single float, and the calculus that connects that float back through the computation graph.

  3. Average loss across a dataset is the number everyone watches

    A single prediction yields a single loss value, but training involves billions of predictions. The figure reported during training is typically the mean loss across a batch, or across an entire evaluation set. A model with average loss of 3.5 is performing worse than one with average loss of 2.1, in the sense that it is on average more surprised by the next token. When loss stops decreasing, training has usually stalled. The absolute value is less informative than the trend, because loss magnitude depends on vocabulary size and the nature of the task.

The story

Cross-entropy loss, also called negative log-likelihood, is the function that converts a model's predictions into the single number that drives learning. It is not the only loss function in use across machine learning, but it is the dominant choice for language models, and it has been since the statistical language models that preceded neural networks.

The setup is this. The model produces a probability distribution over its entire vocabulary, tens of thousands of possible next tokens. One of those tokens is the actual next token in the training text. The loss measures how much probability mass the model placed on that correct token. Specifically, it takes the negative logarithm of that probability.

Why the logarithm? Two reasons, one mathematical and one intuitive. Mathematically, probabilities multiply when you want the joint probability of a sequence, but gradients are easier to work with when terms add. Taking the log converts products into sums. Intuitively, the logarithm compresses the range. A probability of 0.1 and a probability of 0.01 feel like they should be punished differently, but not on a linear scale. The log makes the penalty grow smoothly as confidence in the wrong answer increases.

Here is the arithmetic with real numbers. Suppose the correct next token is the word "stone" and the model assigns it a probability of 0.6. The loss is negative log of 0.6, which equals approximately 0.51. Now suppose the model had been more confident and assigned 0.9. The loss drops to about 0.11. If the model had assigned only 0.05, the loss climbs to about 3.0. If it assigned 0.001, the loss reaches about 6.9. The function is steepest where it matters most: when the model is confidently wrong.

During training, this scalar is differentiated with respect to every parameter in the network. The chain rule connects the loss backward through every layer, producing a gradient for each weight. Those gradients are what the optimiser follows. The model never receives a symbolic correction, never sees the token it should have predicted. It receives only this: a number indicating how surprised it should have been, and the calculus that shows which weights, if adjusted, would have reduced that surprise.

The choice of loss function is a choice about what you are asking the model to learn. Cross-entropy asks the model to match the conditional probability distribution of the next token given the context. It does not ask the model to match human judgments of quality, coherence, or usefulness. Those attributes, if they emerge, emerge because predicting the next token well requires building representations that capture them. The loss is a proxy, and everything downstream depends on whether the proxy is well aligned with what you actually want.

Why it mattered then

Cross-entropy has been the standard loss for probabilistic models since at least the 1990s, when statistical language models based on n-grams were the dominant approach. It was the natural choice because those models were explicitly trying to estimate probability distributions, and cross-entropy is the information-theoretic measure of how well one distribution approximates another. When neural language models arrived in the early 2000s, they inherited the same loss function, because they were solving the same problem: predict the next word given the previous words. The mathematics were well understood, the gradients were straightforward to compute, and the function had the right properties: it penalised confident wrong answers more than hesitant ones, and it rewarded probability mass on the correct answer without requiring the model to be certain.

Why it matters now

Cross-entropy remains the loss function for nearly all contemporary language models, from small models trained on single GPUs to the largest models trained on thousands of accelerators. It is what GPT models optimise, what LLaMA optimises, what every autoregressive transformer optimises during pretraining. The choice has consequences. Because the loss measures only next-token prediction accuracy, models learn to predict text, not to follow instructions or to refuse harmful requests. Those behaviours require additional training stages with different objectives: supervised fine-tuning on demonstrations, reinforcement learning from human feedback, or other alignment techniques. The base model, trained only on cross-entropy loss, is a probability distribution over text, nothing more. The loss also explains certain failure modes. A model trained to minimise cross-entropy will learn to hedge when the training data is ambiguous, producing middling probabilities across several plausible next tokens. That hedging is exactly what the loss asks for, but it is not always what users want. The loss is a choice, and the choice propagates through everything the model becomes.

The surprising detail

Cross-entropy loss does not require the model to be right. It requires only that the model assign some probability to the correct answer. A model that assigns 0.01 to the right token and spreads the remaining 0.99 across everything else will have high loss, but it will still produce usable gradients, and training will still proceed. A model that assigns exactly zero probability to the correct token produces infinite loss, and the gradient becomes undefined. In practice this almost never happens, because the final layer uses a softmax activation, which guarantees that every token receives some non-zero probability, however small. But it is a reminder that the loss is not measuring correctness in the binary sense. It is measuring calibration: how much probability mass you placed on the thing that happened.

Remember this

Loss is not a score of quality. It is a measure of surprise, converted into a number that can be differentiated.

Test yourself

A model assigns probability 0.8 to the correct next token. You then modify the model so it assigns 0.4 to that same token and spreads the rest more evenly across plausible alternatives. The loss increases. Does this mean the second model is worse?

Go deeper

Image: Original diagram, The Daily Triptych. Licence: Original work. Source.

← Back to day 4