PPL
Imagine you read aloud and a friend guesses each next word. After “Good” he will almost certainly say “day”; after “I bought” he is lost. A language model does the same thing, except that before every token it honestly states the probability of all the options. Perplexity is the number that says how many equally likely options the model was choosing from on average. Six is like guessing a die roll, two is like a coin, one means the model knows the text by heart.
The transformers library documentation has a telling experiment. The GPT-2 large model can hold at most 1024 tokens at once, while the test text is three hundred times longer, so it is cut into pieces — and this is done in two ways.
The difference is eighteen percent, although the model and the text are the same. Why, and which answer is right, we work out in section 03.
Calculator
What did the program print?
Pick which number you have and type it in. Tokens per word and bytes per token are needed to bring perplexity to a common measure — bits per byte, which does not depend on the model’s vocabulary.
Where the model hesitated in its answer
Many services return logprobs on request — the natural logarithms of the probabilities with which the model chose each token of the answer. Paste them separated by spaces or commas; ordinary probabilities from zero to one work too. The tall red bars are the places where the model hesitated: check them first.
Etymology
Perplexity, a twist and the natural logarithm
The word “perplexity” sounds scholarly, but it means a simple human thing — bewilderment. It was taken up by IBM engineers who in the seventies taught a computer to recognise speech: they needed a number showing how lost the machine was before the next word. Two more words live next to it, without which perplexity cannot be computed: entropy, invented for steam engines, and the nat — a unit shortened from the word “natural”.
01 · Definition
A die with a fractional number of faces
When the real text reveals the correct token, we look at the probability the model gave it. If high, the surprise is small; if tiny, it is large. The measure of surprise is minus the logarithm of the probability: at probability ½ it is one bit, at ¼ two, at one thousandth almost ten.
The average surprise over the whole text is exactly the loss that training programs print. Perplexity turns it into an easy picture: raise e to the power of loss and you get the number of faces of a fair die whose roll would be just as surprising. A fractional number of faces is no problem: 5.8 means the model hesitates slightly less than between six equal options.
Why the perplexities of different models cannot be compared directly
Perplexity is computed per token, and different models have different tokens. A model with large tokens finds each piece hard to guess, but there are few pieces; a model with small tokens of three letters guesses each one more easily — finishing the middle of a word is not hard — but there are three times as many pieces. The fair comparison is in bits per byte: bits per token, multiplied by the number of tokens and divided by the number of bytes of text. The console at the start of the sheet does this for you, and the lm-evaluation-harness library prints such a measure itself — bits_per_byte.
Lab · a live model
A model that has read this sheet
When you opened the page, the browser collected all of its text and trained a small letter-level language model on it: it remembers which letters follow combinations of zero to five previous ones. Instead of billions of parameters it has a frequency table, but perplexity is computed exactly as for large models. In “Guessing game” walk through the text and see what the model expected. In “Memory” a long memory first helps, then starts to cram. In “Compression” the model competes with the gzip archiver.
03 · Evaluation window workshop
The riddle solved: 19.44 and 16.45
The model guesses a token at the start of the window almost blind: there is not a single word before it. A token at the end of the window sees a thousand predecessors and is guessed much more easily. When windows go back to back, all tokens are counted, and half of them see fewer than 512 neighbours. When the window is shifted by half, only its second half is counted, and the first serves as a hint.
Which number is right? Both are honest, but they answer different questions. 16.45 is closer to how the model works in real life — with a long history before every word; 19.44 is cheaper to compute. The most accurate estimate is to shift the window by one token, but then there is a thousand times more work. That is why a paper must always state the window and the stride along with the perplexity.
04 · Perplexity in code
What to compute with and what you get
Tools compute perplexity differently: per token or per word, with overlapping windows or back to back. The “applies to” column says what the printed number refers to. Tap a row to read the details.
A sliding window as in the Hugging Face documentation; perplexity of an answer from logprobs; a check of how much quantisation damaged the model.
{code}05 · Writing rules
06 · Neighbouring units
07 · Historical section
In 1951 Claude Shannon ran an experiment that would be called a language model test today — only the model was a human. The subject was shown the beginning of a phrase and asked to guess the next letter, and if wrong, to guess again. Almost everywhere the result was one; only at the start of words did the figures jump. From these records Shannon concluded that English text carries 0.6 to 1.3 bits per letter, although writing down one of twenty-seven letters takes almost five bits.
That is perplexity, long before the word existed: two to the power of one bit is two options per letter. An experienced reader hesitates between just a couple of letters, the rest is suggested by meaning. Play the same game in “Guessing game” above: there, instead of a human, is a small model that has read this sheet.
Series · AI
units of language modelsHow a neural network is measured
Tokens are what a model reads, parameters are what it memorises with, epochs and FLOP are how much work went into training, and perplexity shows what that work bought. On the left is the log of one check, where each line leads to its own sheet; on the right are the sheets of the series.
Catalogue of quantities
the whole index →The seven SI base units
A drop of acid changes the colour of the juice, salt colours the flame, water freezes at a tap. Below them are all Simetrium sheets, by discipline and alphabetically.
Open the index →





