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
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.
Perplexity: the size of an equivalent fair choice
Perplexity = exp(loss)
Four tokens, by hand
Why tokenizers break the comparison
Remember
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.
Bytewise
All courses