Cross-Entropy Loss › Defining Cross-Entropy
Language AI HandbookAll chapters Cross-Entropy Loss › Defining Cross-EntropyOne section · Sections in this chapter3 / 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
Cross-entropy combines the entropy of the true distribution with the KL divergence between true and predicted distributions into a single quantity that we can compute from data. For distributions (true) and (model prediction), cross-entropy is:
where:
- : the cross-entropy between the true distribution and the predicted distribution
- : the probability of outcome under the true distribution (empirical distribution from data)
- : the probability of outcome assigned by the model
- : an outcome from the set of possible events
When represents the empirical distribution from training data, is 1 for the actual observed token and 0 for all others. This simplifies cross-entropy dramatically because the sum collapses to a single term involving only the true token:
where:
- : the observed token (the single outcome where )
- : the probability assigned by the model to the true token
This is the negative log-likelihood of the true token under the model's predicted distribution. It penalizes the model proportionally to how surprised it was by the actual observation: if the model assigns high probability to the correct token, the loss is small; if the model assigns low probability, the loss is large, approaching infinity as the predicted probability approaches zero.
The simplification from the full sum to a single term has an important practical consequence. During training, we do not need to compare probability distributions element by element across the entire vocabulary. We only need to look up how much probability the model assigned to the one token that appeared next. This makes gradient computation efficient: backpropagation only needs to update the output neuron corresponding to the true token, though intermediate layers receive gradients from all vocabulary items through the softmax normalization.

Cross-entropy decomposes into entropy plus KL divergence:
where:
- : the cross-entropy between the true distribution and the predicted distribution
- : the entropy of the true distribution (constant with respect to model parameters)
- : the KL divergence from the model distribution to the true distribution
Since is constant with respect to model parameters, minimizing cross-entropy is equivalent to minimizing KL divergence. This decomposition reveals that cross-entropy captures both the inherent uncertainty in the data (the entropy term) and the additional penalty for using a suboptimal model (the KL divergence term). During optimization, the entropy term acts as a constant offset, so gradient descent on cross-entropy effectively minimizes the KL divergence between our model and reality. The lowest achievable cross-entropy on any dataset equals the true entropy of the language, which represents the irreducible uncertainty stemming from inherent ambiguity in text.
Cross-Entropy as Maximum Likelihood Estimation
The connection between cross-entropy and maximum likelihood estimation (MLE) explains the statistical basis for using cross-entropy to train probabilistic language models. Given a dataset of observed tokens , MLE seeks the model parameters that maximize the probability of the observed data:
where:
- : the maximum likelihood estimate of the model parameters
- : the model's probability of the -th observed token given its context
- : the product over all tokens in the dataset
Because products of many small probabilities rapidly underflow to zero numerically, we take the logarithm of both sides. This converts the product into a sum, which is numerically stable:
where:
- : the log-likelihood of the observed tokens
- : the negative log-likelihood, normalized by dataset size
The final expression is exactly the cross-entropy loss averaged over the training set. Minimizing cross-entropy is mathematically identical to performing maximum likelihood estimation. This means we have a strong statistical justification for using cross-entropy: it is the objective that, as grows large, causes our model to converge to the true underlying data distribution (or the closest approximation within our model family). Any other loss function would not have this convergence guarantee in the same form.
The Language Modeling Objective
In autoregressive language modeling, we predict the next token given previous tokens . This sequential factorization allows us to model complex joint distributions over sequences by breaking them down into manageable conditional probabilities. The probability of a sequence factorizes as:
where:
- : the joint probability of the entire sequence
- : the conditional probability of token given all preceding tokens
This factorization is exact: the chain rule of probability guarantees it without any independence assumptions. The cross-entropy loss for a sequence of length is:
where:
- : the average cross-entropy loss over the sequence
- : the length of the target sequence (number of tokens)
- : the token at position in the sequence
- : all tokens preceding position (the context)
- : the model parameters
- : the model's predicted probability of token given previous tokens
This formulation reveals why cross-entropy is called the log-loss: we sum the negative log probabilities of each token. Lower probability assigned to the correct token results in higher loss, with the loss approaching infinity as the predicted probability approaches zero. This creates strong gradients when the model is confidently wrong, pushing the parameters aggressively to correct major errors.
The averaging by (sequence length) ensures that longer sequences do not automatically incur higher loss simply because they contain more tokens. This normalization allows us to compare loss values across sequences of different lengths and to aggregate loss across batches during training. Without this normalization, a long document would always appear to be modeled worse than a short sentence, even if the model's per-token accuracy was identical.
A necessary practical point: in transformer training, the model processes an entire sequence in a single forward pass, computing predicted probability distributions at every position simultaneously. For a sequence of length , this produces losses in parallel. The positions at the beginning of the sequence receive less context than positions near the end, so the model must predict early tokens with limited information. This variability in difficulty across positions is averaged away by the uniform weighting in the loss, which treats all positions equally. Some researchers have proposed weighting later positions more heavily, since they benefit from richer context and thus provide cleaner learning signal, but this remains an active area of investigation.