All courses

Loss per token and perplexity

Convert a loss in nats per token into perplexity exp(𝓛) and interpret it as an effective branching factor. Explain why perplexities from different tokenizers are not directly comparable.

Builds on: The next-token cross-entropy objective

▶ Watch this lesson Start the whole course

What the animation says

What does 2.3 nats mean?

Training prints a loss of 2.3 nats per token. Is that good? A nat is hard to picture. Here's a handle. Imagine a model that guesses fairly among ten tokens, ten percent each. Its surprise on every token is the log of ten, which is 2.3 nats. So a loss of 2.3 feels like picking fairly among ten options. Now take the extreme: a loss of zero nats. How many options is that?

Perplexity = e to the loss

One option. e to the zero is one, so zero loss means certainty. That's the rule: raise e to the loss. The result is perplexity, an effective count of fair options. Climb the ladder. 0.69 nats is two options, 1.39 is four, 2.3 is ten. It never drops below one. Fair guessing over the vocabulary gives its size; a worse model goes above. Now another model scores 3.0, 0.7 nats worse. What's its perplexity?

Four tokens, four surprises

Twenty. Loss gaps become perplexity ratios: 0.7 more nats doubles the options. Now real numbers. A model predicts four tokens, giving the correct ones 0.5, 0.25, 0.8 and 0.1. Each token's surprise is minus the log of its probability. That gives 0.69, 1.39, 0.22 and 2.30 nats. Tap the token that hurts this model most.

Average, then exponentiate

The 0.1 token: 2.3 nats, half the total. Add the four surprises: 4.61 nats. Divide by four tokens: a loss of 1.15 nats per token. Raise e to that: about 3.16. Each step felt like a fair pick among about three options. Check: the probabilities multiply to 0.01. One over its fourth root is 3.16 again. So perplexity is one over the geometric mean probability. Not the average of one over p, which gives 4.31. It's an average; no single token got probability one in 3.16.

Two tokenizers, one text

Same 100-character text, two different tokenizers. Tokenizer A cuts it into 25 tokens. Its model's loss is 3.0 nats per token, perplexity about 20. Tokenizer B cuts it into 50 smaller tokens. Loss 1.8, perplexity about 6. Per token, B looks more than three times better. But which model gives the whole text higher probability?

Compare per character

It's A. Multiply out: A's total is 25 times 3, which is 75 nats. B's is 50 times 1.8, which is 90. Less total surprise means higher probability for the text. A wins. Small pieces are easy to guess one at a time, so per-token perplexity flatters B. The fix: divide total nats by characters or bytes. A scores 0.75 per character, B scores 0.90. So perplexity is e to the loss, per token, and only comparable when the tokenizer is the same.

What does a loss of 2.3 actually mean?

Training reports a number like $\mathcal{L} = 2.3$ nats per token. Is that good? A nat is hard to picture. A lower loss is better, but it's hard to feel *how much* better one model is than another. Two more questions follow. Can we turn the loss into something concrete, like "the model is torn between about 10 options"? And can we compare that number across models that split text into tokens differently?

Perplexity: the size of an equivalent fair choice

Recall that a uniform guess over $V$ tokens has loss $\log V$. Run that backwards. A loss of $\mathcal{L}$ is exactly the loss you'd get by guessing uniformly among $e^{\mathcal{L}}$ options. That count can be fractional, like 3.16 options. So $e^{\mathcal{L}}$ is an **effective branching factor**. On average, the model is as surprised as someone picking fairly among that many equally likely next tokens. Lower loss means fewer effective options.

Perplexity = exp(loss)

$$\text{PPL} = e^{\mathcal{L}} = \Big(\prod_{t=1}^{T-1} p_\theta(x_{t+1}\mid x_{\le t})\Big)^{-1/(T-1)}$$ - $\mathcal{L}$: mean cross-entropy in nats per token, averaged over the $T-1$ predicted tokens. - The right side is $1/(\text{geometric mean of the correct-token probabilities})$. - Range: PPL $\ge 1$. PPL $=1$ means probability 1 on every correct token. A uniform guess gives PPL $= V$. A model worse than uniform can exceed $V$. Loss differences become perplexity *ratios*: $e^{\mathcal{L}_B}/e^{\mathcal{L}_A} = e^{\mathcal{L}_B-\mathcal{L}_A}$.

Four tokens, by hand

The model gives the correct next tokens probabilities $0.5, 0.25, 0.8, 0.1$. Surprisals $-\log p$: $0.693, 1.386, 0.223, 2.303$. Sum $=4.605$. $\mathcal{L} = 4.605/4 = 1.151$, so $\text{PPL}=e^{1.151}\approx 3.16$. Check: the product is $0.01$, and $0.01^{-1/4} = 100^{1/4} \approx 3.16$. This is *not* the average of $1/p$, which would be $(2+4+1.25+10)/4 = 4.31$.

Why tokenizers break the comparison

The same text is 100 characters long. - Tokenizer A uses 25 tokens, $\mathcal{L}=3.0$, PPL $\approx 20$. Total: $25\times3.0=75$ nats. - Tokenizer B uses 50 smaller tokens, $\mathcal{L}=1.6$, PPL $\approx 5$. Total: $50\times1.6=80$ nats. B looks four times better per token, yet it gives the *text* lower probability. Smaller pieces are easier to guess individually. A fair comparison divides total nats by characters or bytes: A gets 0.75, B gets 0.80.

Remember

Perplexity $= e^{\mathcal{L}}$: the number of equally likely options that would produce the same average surprise. It is at least 1. A uniform guess gives $V$, and a model worse than uniform scores above $V$. It is a *per-token* measure. It is only comparable between models that use the same tokenizer. Otherwise, compare total nats per character or per byte of text.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.