- Key idea
- Likelihood, log-likelihood, maximum likelihood estimation, negative log-likelihood loss, perplexity
- Real world
- A coin lands heads 7 times in 10. Of all possible biases, a 70% bias makes that result most likely. That’s the maximum-likelihood estimate.
- In an LLM
- Pre-training adjusts billions of weights to make the actual next tokens in trillions of words as likely as possible. The loss you see in every training log is minus the average log-likelihood per token.
Same formula, different question
So far a model was fixed and we asked how probable an outcome is. Likelihood flips this: the data is fixed (you’ve seen it) and you ask how well each possible model explains it.
For a coin with heads-probability , seeing 7 heads in 10 flips has probability
Read as a function of the data, this is a probability. Read as a function of , with the data held fixed, it is the likelihood of . Same formula, different question.
Maximum likelihood
The maximum-likelihood estimate (MLE) is the parameter value that makes the observed data most probable. For the coin, the peak of is at : the share of heads you saw. For a categorical distribution, the MLE of each probability is its observed frequency.
Try it
Logs, again
Likelihoods of many data points are products of many probabilities, which get tiny quickly. Maximising the log-likelihood gives the same answer (log only ever goes up, so it keeps the peak in the same place) and turns the product into a sum:
Training frameworks minimise rather than maximise, so they use the negative log-likelihood (NLL). Averaged per data point, the NLL is exactly the cross-entropy between the data and the model. That’s why the standard language-model loss is called both.
Flip a coin 10 times and see 7 heads. The MLE is 0.7. Notice the likelihood curve in the widget is wide: 0.5 is still quite plausible (its likelihood is 44% of the peak). Flip 100 times and see 70 heads, and the curve is much narrower; 0.5 is now very unlikely. The MLE alone doesn’t tell you how sure to be.
In a corpus, “Thank” is followed by “ you” in 7 of every 10 occurrences. A model that only sees this context minimises its loss by predicting “ you” with probability exactly 0.7. With real contexts it is the same principle, applied to every token in the training data at once:
Gradient descent moves the weights towards the maximum-likelihood fit. Supervised fine-tuning (SFT) is the same loss on a smaller, curated set of example answers.
Perplexity
Perplexity is the exponential of the average NLL per token:
It has a friendly reading: a perplexity of 10 means the model is, on average, as uncertain as if it were choosing uniformly among 10 tokens. A perfect model would have perplexity 1. Perplexity is only comparable between models that share a tokenizer, because a different tokenizer splits the same text into a different number of tokens.
Likelihood is not probability of the model
is the probability of the data if . It is not the probability that . The likelihood curve doesn’t add up to 1 over , and it ignores anything you knew beforehand (most coins are fair!). Turning likelihood into a belief about needs a prior and Bayes’ theorem: the next page.
Likelihoods everywhere
Once you see it, most training objectives are a likelihood of something:
- Pre-training and SFT: likelihood of the next token.
- Reward models: likelihood of the human preference labels under the Bradley–Terry model, .
- DPO: the same preference likelihood, with the reward written in terms of the policy: see DPO.
- Arena leaderboards: Elo-style ratings are a maximum-likelihood fit of Bradley–Terry to battle outcomes: see comparing models.
Where this shows up
- The training loss: cross-entropy.
- Distillation minimises a cross-entropy against a teacher instead of the data: distillation.
- Policy gradient is “likelihood, weighted by reward”: the score-function trick.
Definitions
For i.i.d. data and a model :
means “the value of at which the maximum is reached”, not the maximum itself.
For the Bernoulli case with successes in trials, setting the derivative of to zero gives .
MLE minimises KL to the data
Minimising the average NLL is the same as minimising the KL divergence from the data distribution to the model:
The entropy of the data is fixed, so lowering the loss can only lower the KL. This is forward KL, which punishes the model for missing anything that occurs in the data; that’s why pre-trained models spread probability over every plausible continuation. The contrast with reverse KL is on divergences.
Sequence-level form
For a language model on a sequence , by the chain rule,
Papers also report bits per byte or bits per character, which divide the NLL (in bits, using ) by the number of bytes rather than tokens. That makes models with different tokenizers comparable.
In papers
The SFT loss is written , with the prompt excluded from the loss. The reward-model loss is , where is the preferred answer and the rejected one. Both are negative log-likelihoods.