Language Models and Loss
A language model predicts a distribution over the next token. Its training objective measures how much probability it assigns to the continuations that actually occur. These definitions connect prediction, compression, and scaling.
Before you begin
Finite probability distributions and logarithms are enough to begin.
A language model is a conditional distribution
Tokenization encodes text as a sequence of elements of a finite vocabulary . A token can be a word, part of a word, or punctuation. Given the preceding tokens (the context ), a model with parameters produces
Here counts model parameters; counts possible next tokens. They are different quantities. The dot in leaves the token argument open, so the expression denotes a whole distribution. Filling in a token gives a number: for the context “The capital of France is”, an illustrative value is , assuming Paris is one token.
During training, the parameters change. During generation, the model selects a next token, appends it to the context, and repeats. Sampling from the distribution is one generation rule; selecting a high-probability token is another.
The observed next token is the training target
A dataset of prediction positions is written
The target is the next token actually present in the text after context , not a token sampled from the model. The average negative log-likelihood is
The logarithm is natural, so the loss is measured in nats per token. A model is penalized for assigning low probability to what actually occurred:
| Probability assigned to the observed token | Negative log-likelihood |
|---|---|
Why logarithms and cross-entropy appear
For a token sequence with its preceding tokens as context, the chain rule gives
Taking a logarithm turns this product into a sum. Minimizing its negative therefore maximizes the sequence likelihood; dividing by expresses the result per token. No independence assumption between successive tokens is needed for this chain-rule factorization.
At a particular prediction position, write the observed target as a one-hot distribution . Its cross-entropy with the model distribution is
This explains why the average loss is also called cross-entropy. A one-hot training target records the observed token; it does not assert that the underlying language distribution has only one possible continuation.
What lower loss tells us
Training approximately minimizes loss on the training text. Evaluation measures the same quantity on held-out text that was excluded from training. Lower test loss means better average probabilistic prediction on that text; it does not by itself establish safety or improved performance on every downstream task.
This supplies the response variable for scaling laws: how does held-out loss change as model size , training tokens , and training compute grow?
Continue reading
Read scaling and compute to study the trade-off between parameters and training data. Generalization and learning dynamics asks why performance on the training data transfers to new inputs.