Skip to content
mentoor

Topic 3 of 5 · 5 min read

Logarithms, entropy and cross-entropy

Goal: Compute the cross-entropy and perplexity of a small prediction and explain why log probabilities are used.

A logarithm turns products into sums, and information theory uses that to measure surprise: a model that gave the true answer a low probability is very surprised, and that surprise is its loss. Cross-entropy and perplexity are just averages of that surprise.

Check yourself · 1 of 3

A model gives the correct token a probability of 1.0. What is the cross-entropy loss for that example?

Choose one answer

Why it matters

Training loss curves, "perplexity" in model cards and the loss value printed by a fine-tuning run are all log probabilities. If you can read them, you can tell whether a number such as 1.386 is good, and why a loss that falls from 2.3 to 0.7 matters.

How to do it

  1. Surprise of one outcome is -log(p). With the natural log, p = 0.5 gives 0.693, p = 0.25 gives 1.386 and p = 1 gives 0. A certain event is no surprise, and rarer events cost more.
  2. Why logs. The probability of a ten-token sentence is a product of ten small numbers, which underflows in floating point. Summing the logs gives the same information safely, and sums also give simpler gradients.
  3. Entropy is the average surprise of a distribution under itself. A fair coin has 1 bit (log base 2). A distribution of [0.5, 0.25, 0.25] has 0.51 + 0.252 + 0.25*2 = 1.5 bits.
  4. Cross-entropy is the average surprise when the truth follows one distribution but the model predicts another. For a single labelled example, it is -log of the probability the model gave to the correct answer. Classification loss in most libraries is exactly this.
  5. KL divergence is cross-entropy minus entropy: the extra surprise caused by using the wrong distribution. It is zero only when the model matches the truth.
  6. Perplexity is e raised to the cross-entropy (in nats). It reads as "the model is as unsure as if choosing uniformly among this many options". A cross-entropy of 1.386 nats gives perplexity e^1.386 = 4.

Before and after

Weak: "Our loss dropped from 2.30 to 1.39, that is a 40% better model." Loss is not a percentage score.

Fixed: perplexity fell from e^2.30 = 10 to e^1.39 = 4. The model went from hesitating among about ten tokens to about four.

What goes wrong

  • Mixing log bases. Bits (base 2) and nats (base e) differ by a factor of 0.693. Compare numbers only in the same base.
  • log(0). A predicted probability of exactly 0 for the true answer gives infinite loss. Libraries add a small epsilon or work from logits.
  • Comparing perplexity across tokenisers. Different vocabularies split text differently, so the numbers are not comparable.

Examples

One prediction, worked

A language model predicts the next token with probabilities cat 0.25, dog 0.50, bird 0.25. The true token is cat.

Step Value
Probability given to truth 0.25
Cross-entropy (nats) -ln(0.25) = 1.386
Perplexity e^1.386 = 4

The model behaves as if guessing among four options even though dog was its favourite.

Why not multiply

import math
probs = [0.01] * 200                     # 200 tokens, each 1% likely
prod = 1.0
for p in probs: prod *= p
print(prod)                              # 0.0 (underflow)
print(sum(math.log(p) for p in probs))   # -921.03

The product rounds to zero in a 64-bit float; the log sum keeps the information.

Key points

  • Surprise is -log(p): zero for a certain event and growing as the event gets rarer.
  • Summing log probabilities replaces products of tiny numbers and avoids floating-point underflow.
  • Cross-entropy for a labelled example is -log of the probability the model gave the correct answer.
  • KL divergence is cross-entropy minus entropy, the extra cost of using the wrong distribution.
  • Perplexity is e to the cross-entropy in nats, the effective number of equally likely choices.

Common mistakes

  • Quoting a loss difference as a percentage improvement instead of converting to perplexity.
  • Comparing a loss in bits with a loss in nats.
  • Passing probabilities that can reach exactly 0 to a log without a safeguard.
  • Comparing perplexity between models that use different tokenisers or test sets.

Check yourself · 2 of 3

A model has cross-entropy of ln 8, about 2.08 nats, on a test set. What is its perplexity?

Choose one answer

Check yourself · 3 of 3

Why do training codes sum log probabilities instead of multiplying probabilities?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic