Jump to content

Language model loss: Difference between revisions

From Wikibase
Align notation with Category:Artificial neural network: loss is the cost function C (as in backpropagation, gradient descent), freeing L (via update-page on MediaWiki MCP Server)
No edit summary
Line 1: Line 1:
[[Category:Machine learning]]
[[Category:Machine learning]]
[[Category:Large language models]]
[[Category:Large language models]]
A '''language model loss''' is the quantity minimised during training of a [[large language model (LLM)|language model]]: it measures how little probability the model assigns to the tokens that actually occur. In the notation of [[artificial neural network (ANN)|ANN]] training it is the '''cost function''' $C$ of the network. For a token sequence $x_1, x_2, \cdots, x_T$ and model parameters $\theta$, the loss is the mean negative log-likelihood of the observed tokens,
 
 
The '''language model loss''' measures how "surprised" a [[large language model (LLM)]] is by the true values of tokens it attempts to predict.
 
== Formal definition ==
 
With [[model parameters]] $\theta$, the '''language model loss''' on training sample $\vec x=(x_1,\cdots,x_T)$:


$$
$$
C(\theta)
C(\theta)
=
=
-\frac{1}{T}\sum_{t=1}^{T}\log P_\theta(x_t \mid x_{1:t-1}),
-\frac{1}{T}\sum_{t=1}^{T}\log_b P_\theta(x_t \mid x_{1:t-1})
$$
$$


where $P_\theta(x_t \mid x_{1:t-1})$ is the probability the model assigns to token $x_t$ given the tokens that precede it, so that a single training example $x$ contributes $C_x$.
<blockquote>
$\vec x=(x_1,\cdots,x_T)$ is a text [[token]] sequence of length $T$ (e.g., <code>('I','am','myself','.')</code>).
</blockquote>


== Units ==
=== Unit ===


The logarithm fixes the unit. The natural logarithm gives nats per token, and rescaling gives bits per token,
The unit of the language model loss is determined by $b$.


$$
{| class="wikitable"
\begin{cases}
|-
C_{\text{nat}}(\theta) = -\frac{1}{T}\sum_{t=1}^{T}\log P_\theta(x_t \mid x_{1:t-1}) \\
! $b$
C_{\text{bit}}(\theta) = \frac{C_{\text{nat}}(\theta)}{\ln 2}
! Unit
\end{cases}
|-
$$
| 2
| Bit
|-
| e
| Nat
|-
| 10
| Hartley
|}


<blockquote>
Training reports conventionally quote $C_{\text{nat}}$.
Training reports conventionally quote $C_{\text{nat}}$.
</blockquote>


== Perplexity ==
== Perplexity ==
Line 33: Line 51:


and reads as the effective number of equally likely tokens the model is choosing between. A loss of $1$ nat per token therefore corresponds to a perplexity of $e \approx 2.718$.
and reads as the effective number of equally likely tokens the model is choosing between. A loss of $1$ nat per token therefore corresponds to a perplexity of $e \approx 2.718$.
== Relation to the pretraining objective ==
The loss is the negated, length-normalised form of the conditional log-likelihood that pretraining maximises,
$$
\log P_\theta(x_1, x_2, \cdots, x_T)
=
\sum_{t=1}^{T}\log P_\theta(x_t \mid x_{1:t-1})
=
-T\,C(\theta).
$$
Minimising the loss and maximising the likelihood are the same operation, and [[large language model (LLM)|scaling laws]] are stated in terms of the loss because it is the quantity whose behaviour is measured across model sizes.


== Irreducible loss ==
== Irreducible loss ==


The loss is bounded below by the conditional entropy of the text itself: no model predicts next tokens better than the true distribution generates them. Scaling-law fits write this floor as $C_\infty$ and add reducible terms for the size of the model and for the size of the training set.<ref>{{#cite:Q3141}}</ref>
The loss is bounded below by the [[conditional entropy]] of the text itself, since no model predicts next tokens better than the true distribution generates them.
 
== Concrete examples ==
 
For a single predicted token the loss is $-\log P_\theta$, so it depends only on the probability the model gave the token that occurred:
 
{| class="wikitable"
|+ Loss of a single predicted token
! $P_\theta$
! $C_{\text{nat}}$
! $C_{\text{bit}}$
! $\operatorname{PP}$
|-
| $0.9$
| $0.105$
| $0.152$
| $1.11$
|-
| $0.5$
| $0.693$
| $1$
| $2$
|-
| $10^{-2}$
| $4.61$
| $6.64$
| $10^{2}$
|-
| $10^{-5}$
| $11.5$
| $16.6$
| $10^{5}$
|}
 
A model that is uniform over a vocabulary of $10^{5}$ tokens, that is $P_\theta = 10^{-5}$ for every token, therefore sits at $11.5$ nats per token, the no-knowledge baseline against which trained models are compared. Averaging over a sequence mixes such terms: a 2-token sequence predicted at $P_\theta = 0.9$ and then at $P_\theta = 0.5$ has a loss of
 
$$
C
=
-\frac{1}{2}\left(\log 0.9 + \log 0.5\right)
\approx 0.399 \text{ nats}
\approx 0.576 \text{ bits}.
$$


== Scaling of the loss ==
== Loss and compute ==


Empirically the loss falls as a power law in training compute $F$, measured in floating-point operations,
It is empirically determined


$$
$$
C(F)=aF^{-\alpha},
C(F) \prop F^{-\alpha},
\qquad \alpha \approx 0.05.
\qquad \alpha \approx 0.05.
$$
$$


The exponent governs how expensive further improvement is: halving the loss requires roughly $10^{6}$ times more compute, which is why the gap between $2$ nats per token and $1$ nat per token, a perplexity fall from $7.389$ to $2.718$, is very large in practice.<ref>{{#cite:Q3141}}</ref>
The exponent governs how expensive further improvement is: halving the loss requires roughly $10^{6}$ times more compute, which is why the gap between $2$ nats per token and $1$ nat per token, a perplexity fall from $7.389$ to $2.718$, is very large in practice.<ref>{{#cite:Q3141}}</ref>

Revision as of 16:15, 11 November 2026

Languages: English · français · Esperanto


The language model loss measures how "surprised" a large language model (LLM) is by the true values of tokens it attempts to predict.

Formal definition

With model parameters $\theta$, the language model loss on training sample $\vec x=(x_1,\cdots,x_T)$:

$$ C(\theta) = -\frac{1}{T}\sum_{t=1}^{T}\log_b P_\theta(x_t \mid x_{1:t-1}) $$

$\vec x=(x_1,\cdots,x_T)$ is a text token sequence of length $T$ (e.g., ('I','am','myself','.')).

Unit

The unit of the language model loss is determined by $b$.

$b$ Unit
2 Bit
e Nat
10 Hartley

Training reports conventionally quote $C_{\text{nat}}$.

Perplexity

Perplexity is the exponential of the loss in nats,

$$ \operatorname{PP}(\theta) = \exp\big(C_{\text{nat}}(\theta)\big), $$

and reads as the effective number of equally likely tokens the model is choosing between. A loss of $1$ nat per token therefore corresponds to a perplexity of $e \approx 2.718$.

Irreducible loss

The loss is bounded below by the conditional entropy of the text itself, since no model predicts next tokens better than the true distribution generates them.

Loss and compute

It is empirically determined

$$ C(F) \prop F^{-\alpha}, \qquad \alpha \approx 0.05. $$

The exponent governs how expensive further improvement is: halving the loss requires roughly $10^{6}$ times more compute, which is why the gap between $2$ nats per token and $1$ nat per token, a perplexity fall from $7.389$ to $2.718$, is very large in practice.[1]