Skip to main content

Likelihood and log-likelihood

Scoring observed tokens and connecting likelihood to loss

Lesson objective. Compute continuation likelihood by multiplying conditional probabilities, then recover its log-likelihood and NLL.

Lesson skeleton

See the path

Stage connections explained on this page

From hidden states to the predictive distribution → Score observed tokens

From hidden states to the predictive distribution → Score observed tokens

Show that the same conditional distribution can be used either to select a token for generation or to read the probability of an observed target and build a likelihood. Scoring follows the reference prefix: it selects no token and does not update parameters.

Review this lesson

Likelihood measures the probability of observed data for fixed parameters. In autoregressive language modeling, it is built from the conditional probabilities of each target token.

TipThe one-minute mental model
  • Scoring: the model receives the reference prefix and assigns probability to the observed target; no generated token is fed back and parameters remain fixed.
  • Sequence: the chain rule gives \(P_\theta(x_{1:T})=\prod_t P_\theta(x_t\mid x_{<t})\); the logarithm turns this product into a sum.
  • Objective: NLL is \(-\log P_\theta(x_{1:T})\). For the same observations and positions, maximizing log-likelihood is equivalent to minimizing NLL.
  • Comparison: state the tokenizer, included positions, and sum or per-token mean; these conventions change the scale.

Quick question: why is scoring an observed sequence neither generation nor a parameter update?

1. Observed target 2. Probability 3. Product of probabilities or sum of their logs 4. Likelihood or log-likelihood

In the running example, the likelihood of the continuation is built from the conditional probabilities of its successive tokens: each probability is evaluated using the prefix available at that step, extended with the previously observed tokens.

Autoregressive factorization

The derivation of this factorization from the chain rule, and the interpretation of conditional probabilities, are developed in the mathematical formalization of language modeling. Here, the focus is on likelihood as an evaluation criterion for observed sequences and on its connection to the loss.

For a sequence \(x_{1:T}\):

\[ P_\theta(x_{1:T})=\prod_{t=1}^{T}P_\theta(x_t\mid x_{<t}). \]

Scoring an observed sequence: the prefix is known

At each position, the model receives the observed prefix \(x_{<t}\) and assigns a probability to the target token \(x_t\). This conditioning on reference tokens is also used as teacher forcing during training (see training examples). Here, it is used only to score the sequence: no generated token is selected, no model prediction is fed back, and the parameters \(\theta\) remain fixed.

This differs from generation, which feeds the model-selected token back before the next step. It also differs from training: scoring computes no gradients and makes no parameter updates.

Compare the prefixes used for generation and scoring.

For a sequence that fits within the context window, the model can compute logits for all positions in one forward pass: observed prefixes are supplied together, while the causal mask prevents each position from reading later tokens. Beyond that window, scoring requires a windowing or stride strategy and an explicit convention for the prefixes actually available and the targets counted.

For multiple independent dataset sequences, total likelihood is the product of each sequence probability. Products of many numbers at most one become tiny quickly, so we use log-likelihood:

\[ \log P_\theta(x_{1:T})= \sum_{t=1}^{T}\log P_\theta(x_t\mid x_{<t}). \]

The logarithm turns products into sums, which are more stable and convenient to compute.

Example: scoring the continuation

Continue the illustrative sequence “They built roads”. Suppose the conditional probabilities assigned to its three successive observed tokens are \(0.6\), \(0.5\), and \(0.8\). Each value is evaluated after extending the observed prefix with the preceding reference tokens; they are not independent probabilities.

\[ P(\text{continuation})=0.6\times0.5\times0.8=0.24. \]

From likelihood to loss

Negative log-likelihood \(\operatorname{NLL}\) is:

\[ \operatorname{NLL}(\theta)=-\log P_\theta(x_{1:T}). \]

The log-likelihood is \(\log(0.24)\approx-1.427\) nats, and the sequence NLL is \(-\log(0.24)\approx1.427\) nats. Dividing by three gives an average of approximately \(0.476\) nats per token. These are illustrative values chosen to keep the arithmetic visible, not outputs from a trained model.

For fixed sequences and the same included positions, maximizing log-likelihood is equivalent to minimizing NLL. Averaging per token is a common scaling, useful for comparing sequences of different lengths, but it is not the same as the raw sum.

Perplexity

For mean NLL computed with the natural logarithm, perplexity is \(\operatorname{PPL}=\exp(\operatorname{NLL}_{\mathrm{mean}})\). It is the exponential of mean loss, or the geometric mean of the inverse probabilities assigned to observed tokens. It summarizes probabilistic fit over the units defined by the tokenizer; it does not measure overall answer quality or truth. Two perplexity values are directly comparable only when the corpus, tokenizer, evaluated positions, and reduction convention are compatible. For one-hot targets, mean cross-entropy computed over the same valid positions with the same reduction convention coincides with this mean NLL. If the mean is measured in bits rather than nats, use \(2^{\\bar{\\ell}}\), with \(\\bar{\\ell}\) measured in bits. The Loss lesson revisits its use as a metric derived from the training objective.

WarningMisconceptions
  • “High likelihood proves the model is true.” No: it indicates probabilistic fit to selected observations relative to a model, tokenizer, and data distribution.
  • “Token probabilities are independent.” They are conditional on the prefix; the product follows from the chain rule, not independence.
  • “NLL is comparable without specifying the tokenizer.” Not always: different segmentations change the number and granularity of evaluated events.

Questions and exercises

The next page decomposes this NLL at one target position and generalizes the one-hot case to a target distribution: cross-entropy.

Supplementary resources Experiment, explore further, and practise the ideas from this lesson.

Further reading