Algorithm Search⌘K

Definitionintro

Perplexity

Also called language model perplexity.

The exponential of a model's per-token cross-entropy on held-out text, exp(H) in nats or 2^H in bits, which is the inverse geometric mean of the token probabilities. Lower is better.

Perplexity is the standard number for how well a language model predicts a text that it was not trained on. It is the inverse of the geometric mean of the probabilities that the model gives the tokens of the text, so a perplexity of 100 means that the model was, on average, as uncertain as if it chose each token from 100 equally likely ones. Lower is better. It is the exponential of the per-token cross-entropy: eHe^{H} when the cross-entropy HH is in nats, and 2H2^{H} when it is in bits, and the two give the same number. Jelinek, Mercer, Bahl and Baker introduced it in 1977 as a measure of the difficulty of speech recognition tasks, and it is also used for models of DNA, protein and music sequences.

Definition

Let PP be a language model and let w1,…,wNw_1, \ldots, w_N be the predicted tokens of a test text, with the end marker </s> of each sentence counted as a token and the start markers <s> not counted. The per-token cross-entropy of PP on the text, in nats, is

H=−1N∑i=1Nln⁡P(wi∣w1,…,wi−1),H = -\frac{1}{N} \sum_{i=1}^{N} \ln P(w_i \mid w_1, \ldots, w_{i-1}),

where the history of each token goes back to the start of its sentence, or further if the model uses more context. The perplexity is

PP=eH=P(w1,…,wN)−1/N=(∏i=1N1P(wi∣w1,…,wi−1))1/N.\mathrm{PP} = e^{H} = P(w_1, \ldots, w_N)^{-1/N} = \Big( \prod_{i=1}^{N} \frac{1}{P(w_i \mid w_1, \ldots, w_{i-1})} \Big)^{1/N}.

With logarithms to base 2 the cross-entropy is H2=H/ln⁡2H_2 = H / \ln 2 bits per token, and 2H2=eH2^{H_2} = e^{H}. The value of the perplexity does not depend on the base, but the cross-entropy does, so a report of HH must state its unit.

Some properties follow at once.

  • PP≥1\mathrm{PP} \ge 1, with equality only when every token gets probability 1.
  • If one token gets probability 0, then H=∞H = \infty and PP=∞\mathrm{PP} = \infty. This is why language models are smoothed.
  • If PP is uniform on a vocabulary VV, then PP=∣V∣\mathrm{PP} = |V| on every text. Perplexity is often read as an effective number of choices per token.
  • Minimizing perplexity is the same as maximizing the likelihood of the test text, and the same as minimizing the mean cross-entropy loss.

Perplexity is a value per token of a stated kind. Per-word, per-subword and per-character perplexities of the same model on the same text are different numbers. Since all of them come from the same total log probability ln⁡P(text)\ln P(\text{text}), a perplexity per NN tokens converts to one per CC characters by PPC=PPNN/C\mathrm{PP}_C = \mathrm{PP}_N^{N/C}.

Examples and non-examples

Uniform over ten. A model that gives each of 10 tokens probability 1/101/10 has H=ln⁡10=2.302585H = \ln 10 = 2.302585 nats =3.321928= 3.321928 bits per token on any text, and PP=10\mathrm{PP} = 10.

Four tokens. A model gives the four tokens of a text the probabilities 1/21/2, 1/41/4, 1/81/8 and 1/81/8. Then H=14(ln⁡2+2ln⁡2+3ln⁡2+3ln⁡2)=94ln⁡2=1.559581H = \frac14 (\ln 2 + 2\ln 2 + 3 \ln 2 + 3 \ln 2) = \frac94 \ln 2 = 1.559581 nats, which is 2.252.25 bits, and PP=22.25=e1.559581=4.756828\mathrm{PP} = 2^{2.25} = e^{1.559581} = 4.756828. The arithmetic mean of the four probabilities is 1/41/4, but the perplexity is not 4: it is set by the geometric mean, which weights the small probabilities more.

A DNA model. A model of DNA that gives each base probability 1/41/4 has perplexity 4 per base. A model with perplexity 3.6 per base on held-out sequence uses about log⁡23.6=1.85\log_2 3.6 = 1.85 bits per base, against 2 bits for the uniform model.

Not: perplexity on the training text. The value on the text used to estimate the model rewards memorization. An unsmoothed maximum likelihood n-gram model has a low training perplexity and an infinite test perplexity as soon as one test n-gram is new.

Not: comparing different tokenizations by per-token perplexity. A model over subwords predicts more tokens per sentence than a model over words, so its per-token perplexity is lower for the same total probability of the text. To compare models with different tokenizations, use bits per character or bits per byte, the total −log⁡2P(text)-\log_2 P(\text{text}) divided by the number of characters or bytes.

Not: comparing different vocabularies. A closed-vocabulary model that maps rare words to <unk> predicts the easy token <unk> in place of each rare word. A smaller vocabulary then gives a lower perplexity without a better model. Compare models only on the same vocabulary and the same test tokens.

Not: the entropy of the language. A perplexity is a property of one model on one text. It is an upper estimate of the entropy rate of the source, in the sense below, not the entropy rate itself.

  • Cross-entropy. For a true distribution pp and a model qq on the same outcomes, H(p,q)=−∑xp(x)log⁡q(x)H(p, q) = -\sum_x p(x) \log q(x). The per-token HH above is the empirical form of the cross-entropy rate on a sample. The cross-entropy loss used to train neural language models is the same mean, in nats, so the perplexity is elosse^{\text{loss}}.
  • Entropy and entropy rate. The Shannon entropy H(p)=−∑xp(x)log⁡p(x)H(p) = -\sum_x p(x) \log p(x) is the cross-entropy of pp with itself. For a stationary ergodic source, the entropy rate lim⁡n→∞1nH(X1,…,Xn)\lim_{n \to \infty} \frac1n H(X_1, \ldots, X_n) is at most the expected cross-entropy rate of any model, so on average no model reaches a perplexity below eHe^{H} of the true source, with HH the entropy rate in nats.
  • KL divergence. H(p,q)=H(p)+D(p ∥ q)H(p, q) = H(p) + D(p \,\|\, q), where DD is the Kullback-Leibler divergence. A lower perplexity on text from pp means a model qq closer to pp in this divergence.
  • Word error rate. In speech recognition, a lower perplexity of the language model often goes with a lower word error rate, but not always, since the error rate also depends on which words the acoustic model confuses.
  • Branching factor. For a grammar that allows a fixed set of next words at each point, the branching factor is the number of allowed words. Perplexity is the corresponding value for a model that gives the next words different probabilities.

References

  1. Frederick Jelinek, Robert L. Mercer, Lalit R. Bahl and James K. Baker, Perplexity, a measure of the difficulty of speech recognition tasks, Journal of the Acoustical Society of America 62(S1), S63, 1977, the abstract of the talk that introduced perplexity as a measure of the difficulty of a recognition task
  2. C. E. Shannon, A mathematical theory of communication, Bell System Technical Journal 27(3), 1948, entropy of a source and the entropy per symbol of a sequence
  3. Daniel Jurafsky and James H. Martin, Speech and Language Processing, third edition draft, chapter N-gram Language Models (draft of January 2026), evaluating language models with perplexity

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/QH9B1T/text.yaml