Part III · Ch. 12 — Measuring Wrongness

Part III · Chapter 12 of 21

Measuring Wrongness

Loss: turning 'bad guess' into a single number


A guessing machine needs a report card

The model assigns shares to mat, couch, and keyboard. How good was that prediction? The downhill rule needs one height. A loss, written L, supplies it.

Grade the observed next token

Reuse the shares 0.665,0.245,0.090 from Chapter 9. The target is mat because that is what came next in the training text.

A left-to-right grading pipeline. Left: five context chips reading the, cat, sat, on, the, labeled 'the question'. An arrow leads to a small three-bar chart labeled 'the model's bet (Ch. 9)' with bars mat () 0.665, couch () 0.245, keyboard () 0.090. A badge reading 'answer key: mat' points down at the mat bar; the couch and keyboard bars are faded to show they are ignored, noted 'ignored — belief here already came out of mat's share'. An arrow carries a label p = 0.665 into a box reading L = minus ln p, then a final arrow to the output L = 0.41.
The true token selects the probability.

For this loss, read the target's probability, 0.665. The other shares affect it through normalization: allocating more belief elsewhere leaves less for the target.

Designing the wrongness ruler

We want zero penalty at p=1, increasing penalty as p falls, and additive scores for a sequence. The negative log probability supplies those properties.

A line chart. Horizontal axis is p, the belief given to the truth, from near 0 to 1. Vertical axis is the penalty from 0 to about 7. A line labeled '1 minus p (too gentle)' slopes gently from 1 down to 0. A curve labeled 'minus ln p' dives from the top-left down to 0 at p equals 1. Callouts note that at p = 0.1 the gentle ruler charges 0.90 while the log ruler charges 2.3, and at p = 0.001 the gentle ruler charges 0.999 while the log ruler charges 6.9. A dashed line marks that the gentle ruler can never exceed 1.
Log loss punishes confident mistakes.
$$L = -\ln p(\text{correct token} \mid \text{context})$$

In words: the loss is minus the natural log of the probability the model gave the correct token, given the context.

Compare two candidate loss rules
Rulep=0.1p=0.001Behavior
1−p0.900.999Bounded by one; limited penalty for extreme misses.
−ln p2.36.9Unbounded as target probability approaches zero.

The log also connects sequence probabilities to total loss. Conditional probabilities multiply along a sequence; their negative logs add. Two successive probabilities of 0.5 give 0.25 together and total loss about 1.39.

A single curve for L equals minus ln p, dropping from the upper left down to zero at p equals 1. Six checkpoint dots sit on the curve at p = 1 (L = 0), p = 0.9 (0.11), p = 0.665 (0.41, ringed in and marked 'our mat grade'), p = 0.5 (0.69), p = 0.1 (2.3), and p = 0.01 (4.6). The right, high-p region is labeled 'comfortable country'; the steep left region is labeled 'the cliff — confident wrongness'. Dashed guide lines run from the p = 0.1 dot to both axes.
Near-zero belief makes log loss soar.
$$L = -\ln 0.5 = 0.69 \qquad L = -\ln 0.1 = 2.3 \qquad L = -\ln 0.01 = 4.6$$

In words: half-belief in the truth costs 0.69, one-in-ten costs 2.3, one-in-a- hundred costs 4.6. Every division of belief by ten adds the same 2.3 to the penalty, which is Chapter 4's each-÷10-subtracts-2.3 pattern with the sign flipped.

Additional checkpoints: p=0.9 costs about 0.11; p=0.37 costs about 1. The natural-log unit is a nat. We can read loss as surprise.

Grading the cat

Use the log quotient rule to avoid calculating a new logarithm for each candidate:

$$\ln\!\left(\frac{a}{b}\right) = \ln a - \ln b$$

In words: the log of a divided by b is the log of a minus the log of b. It is Chapter 4's $\ln(a \cdot b) = \ln a + \ln b$ with division in place of multiplication.

Grade mat by hand

We know $e^2\approx7.39$, so $\ln7.39\approx2$. Take one calculator value, $\ln11.11\approx2.41$.

$$L = -\ln 0.665 = \ln 11.11 - \ln 7.39 \approx 2.41 - 2 = 0.41$$

In words: the penalty is the log of everyone's total minus the log of mat's own share, about 0.41.

For couch, subtract 1 instead, giving 1.41. For keyboard, subtract zero, giving 2.41.

The three possible grades

The loss for each possible next token
if the truth were…$p$loss $L$
mat0.6650.41
couch0.2451.41
keyboard0.0902.41

These three grades differ by the logit gaps because they score the same fixed distribution against different targets. Raising one logit also changes the common normalization total, so it does not generally reduce its own loss by exactly that amount.

The whole exam

A mean lets us compare exams of different lengths. Each token contributes its own loss.

A bar chart with three bars for a three-question exam. Question 1 (p = 0.5) is 0.69, question 2 (p = 0.9) is 0.11, question 3 (p = 0.1) is a tall 2.30. A dashed horizontal line at 1.03 is labeled 'exam average'. An arrow points at the tall third bar with the note 'one bad surprise outweighs the other two combined'.
Average the individual prediction losses.

Average three prediction rounds

Correct-token probabilities 0.5,0.9,0.1 produce losses about 0.69,0.11,2.3.

$$L = \frac{0.69 + 0.11 + 2.3}{3} = \frac{3.10}{3} \approx 1.03$$

In words: add the three penalties and divide by three, giving an exam score of about 1.03.

The largest surprise contributes more than the other two combined. That does not alone determine the parameter gradient; the backward computation does.

Cross-entropy gives this score. Our cross-entropy loss averages it over N questions. The parameters $\theta$, “theta,” determine the predicted shares $p_i$:

$$L(\theta) = \frac{1}{N} \sum_{i=1}^{N} \big( -\ln p_i \big)$$

In words: the loss of the parameters $\theta$ is the average, over all $N$ questions, of minus the log of the belief the model gave each right answer.

Different numbers answer different questions
MeasureWhat it asks
Cross-entropyHow much probability reached the observed targets?
AccuracyHow often did the top choice match?
Mean target probabilityWhat is the arithmetic average of the shares?

These measures differ. Exponentiating minus mean log loss gives the geometric mean target probability, not the arithmetic mean.

See it move

Watch (1:27): what to notice. The grade ignores every bar but the truth's, then rides the red ruler: 0 at $p = 1$, the 0.69 and 2.3 landmarks, the cliff at the left wall. It ends on $L(\theta)$, one number that moves when the dials do.

Why the LLM cares

Loss gives each parameter setting a height. Gradient descent uses its slopes to improve the model's predictions. A uniform distribution over V=50,000 tokens has loss $\ln50000\approx10.8$, roughly where an untrained model starts.

A mean loss of 2.3 corresponds to a geometric mean target probability near 0.1. We can now read a training curve with that interpretation in mind.

What you now know

  • Loss gives training one objective to reduce.
  • Next-token log loss scores the observed token's probability.
  • −ln p strongly penalizes near-zero probability on the target.
  • Our three candidates cost 0.41,1.41,2.41 against the same prediction.
  • Mean loss is not an error count or an arithmetic mean probability.

Where we're headed

The loss defines the hill. Next we'll choose how to step across terrain less friendly than our first parabola.