Anfragen

Understand LLMs from Zero#30

Loss Function and Cross-Entropy: Measuring an LLM's Mistakes

Last time, logits became probabilities. This time we measure how far off those probabilities are, with a single number. Only one probability is used: the one the model gave to the correct token. Its logarithm, with a minus sign, is the loss (the cross-entropy). Move the probability and watch what the loss does.

  • POINT 1The correct answer is the next token in the text. Nobody has to prepare answers: any text can be used to check the predictions. We look only at the probability the model gave to the correct token.
  • POINT 2loss = −log(probability of the correct token). At 100% it is 0. Every division of the probability by e (about 2.72) adds 1. Close to 0 it grows without limit.
  • POINT 3For a whole text, probabilities multiply and losses add up. The mean of the tokens' losses is the loss on that text. On this text Gemma scored 3.45; blind guessing would score 12.48.

TRY 1

Move the probability of the correct token and watch the loss

The 3D picture is the sea. Depth is loss. The golden lead sinks to the depth “−log(probability)”; the blue one stops at “100% − probability”. Use the slider to move the probability the model gave to the correct token. The red line is the depth of blind guessing.

probability given to the answer89%
loss = −log(probability)0.12
subtraction (1 − probability)0.111

Gemma's real probabilities (the continuations of last time's sentence)

pick a probability

−log(89%) = 0.12

Just below the surface. The loss is less than 1: as long as the probability is above 37%, we have not even divided by e once.

The three buttons are the probabilities Gemma 3 4B really gave to continuations of “To keep the milk cold, put it in the”. The red line (12.48) is the loss when all 262,144 tokens get the same probability. log is the natural logarithm (base e).

TRY 2

As a curve: the logarithm and the subtraction

Probability goes across, loss goes down. The golden curve is −log(probability); the blue dashed line is “100% − probability”. Move the dot and see how different the two are. At the right edge (100%) both are 0.

0%25%50%75%100%2468101214blind guess 12.48● −log(probability)● 100% − probability
probability given to the answer1.0%
loss = −log(probability)4.61
subtraction (1 − probability)0.990
probability of the answerloss (logarithm)subtraction
100%0.000.00
50%0.690.50
10%2.300.90
1.0%4.610.99
0.010%9.210.999900
0.00010%13.820.999999

Where the probability is small, the subtraction is stuck near 1. The logarithm tells 1% and 0.0001% clearly apart (4.61 and 13.82).

TRY 3

The loss of every token of a text Gemma read

We let Gemma 3 4B read a text one token at a time and measured, each time, the probability it had given to the token that really came next. In the picture, the length of a line is the loss. Tap a token to see Gemma's top three guesses. You can also switch to the same tokens in a random order, or to other texts.

pick a text

mean 3.45blind guess 12.48
sum of the 34 losses117.3
mean3.45
tokens deeper than blind guessing3

token 33, “penguin”: probability given to the answer 0.000013% → loss = −log(probability) 15.86 (number 2,549 in Gemma's ranking)

Gemma's guessprobabilityloss if that were the answer
1. cold25%1.40
2. dark17%1.78
3. cloudy12%2.13

The loss of “penguin” is larger than that of blind guessing (12.48). Gemma had put 25% on “cold” and left almost nothing for “penguin”.

The text: “To keep the milk cold, put it in the refrigerator. Then boil some water and make a cup of tea. Outside the window, it is raining on a penguin.” Measured as in the video (logits taken straight from llama.cpp, turned into probabilities). The first tokens of a text have a large loss: there is nothing to go on yet. By the way: e to the power of the mean loss of the video's text, 3.45, is about 32. It means “as hard as guessing blindly among about 32 candidates every time” and is called perplexity (episode 68).

TRY 4

Multiplying and adding

A parable. The model has 10,000 coins. At every token it bets all of them, split according to its probabilities, and keeps only what it put on the correct token. Press “next token” and compare the coins with the sum of the losses.

9.0 coins

correct tokenprobability givencoins leftlosssum of losses
refrigerator× 89%8,891+ 0.120.12
.× 85%7,543+ 0.160.28
Then× 0.12%9.0+ 6.737.01

10,000 ÷ e7.01 = 9.0 (the same as the product)

After 3 tokens, 9.0 coins are left. The sum of the losses is 7.01. Divide 10,000 by e 7.01 times and you get the same number.

The probabilities are measured on Gemma 3 4B. The coins are only a parable. “Coins left ÷ 10,000” is the probability the model gave to the whole text so far.

CHECK

Quick quiz

Three questions to check what you have learned.

Q1Which of the 262 thousand probabilities does the loss (cross-entropy) use?

Q2The model gave the correct token 100%. What is the loss?

Q3Why the logarithm and not “100% − probability”?

Probabilities and losses: Gemma 3 4B (measured with llama.cpp). The coins and the 3D sea are illustrations.

FINISH

Finished the episode?

COMMENTS

Comments & questions

Something unclear, something you want to know more about, or just your thoughts: write it here. We answer quickly, and other learners can reply too.

Loading…