Smoothing and Backoff
Every finite corpus is missing good word sequences it simply never saw, so a raw n-gram model assigns them probability zero and breaks. Smoothing repairs the zeros: add-one and add-k shave mass off seen events, backoff and interpolation fall back on shorter contexts, and Kneser-Ney — worked here by hand — replaces raw frequency with how many contexts a word completes.
╌╌╌╌
This builds on N-Gram Language Models, which set up the model — factor a sequence by the chain rule, truncate the history with the Markov assumption, estimate each conditional by counting, and score with perplexity. That model works until the test set contains a word sequence the training corpus never included; then the count is zero, the probability is zero, and perplexity is undefined. This lesson is about that failure and its repair.
Generalization and the zero problem
An n-gram model is a mirror of its training corpus. That has two consequences. Higher models the training text more and more faithfully — sample from a -gram trained on Shakespeare and you get fragments that are nearly verbatim Shakespeare, because with word types there are possible bigrams and his entire corpus is only words, so most n-grams that do occur occur exactly once and have only one continuation. The model has memorized rather than generalized.
The flip side is fragility across genres. An n-gram grammar trained on Shakespeare generates nothing resembling the Wall Street Journal, and vice versa. A model only works on text like its training data, which is why matching the genre and dialect of the training corpus to the intended task matters.
But the deeper problem is sparsity: any finite corpus is missing perfectly good word sequences that simply never appeared. Consider the bigram denied the in a news corpus, with the continuations actually seen:
denied the allegations 5denied the speculation 2denied the rumors 1denied the report 1If the test set contains denied the offer or denied the loan, the model estimates — a zero. These zeros are harmful for two reasons. They understate the probability of everything that could legitimately occur, hurting any downstream task. And worse, a single zero anywhere in the test set makes the whole test-set probability zero, so perplexity is undefined — you cannot divide by zero. A usable model cannot assign zero probability to unseen but possible events.
Unknown words
A related gap is words never seen at all — out-of-vocabulary (OOV) words. In a
closed vocabulary task (say, a speech recognizer with a fixed pronunciation
dictionary) this cannot arise. Otherwise we build an open vocabulary by adding a
pseudo-word <UNK>: fix a vocabulary in advance (or keep only words above a frequency
threshold), replace every other word in the training data with <UNK>, and then
estimate <UNK>'s probability from its counts like any ordinary word. Because a model
can lower its perplexity by shrinking the vocabulary and loading probability onto
<UNK>, perplexities are only comparable across models with identical vocabularies.
Smoothing
Smoothing (or discounting) is the family of fixes for the zero problem: shave a little probability mass off the events the model has seen and redistribute it to the events it has not, so nothing gets exactly zero.1 The name comes from the picture — flattening a spiky count distribution so the sharp zeros fill in.
Laplace (add-one) smoothing
The simplest scheme adds one to every count before normalizing. For unigrams, the MLE becomes, after adding to each of the word counts and adjusting the denominator by so the probabilities still sum to one,
For bigrams, the prefix count in the denominator must likewise be augmented by , because every one of the possible following words has gained a phantom count:
It helps to see the effect through the adjusted count — the smoothed count that, divided by the original denominator, reproduces the smoothed probability. For add-one bigrams,
On the Berkeley corpus this adjustment is drastic. The count falls from to an adjusted , and drops from to — a discount of . Because zero cells each gain a phantom count, add-one moves far too much mass onto the zeros. It is a useful baseline and a real method for text classification, but too blunt for serious language modeling.
Add-k smoothing
A gentler variant adds a fraction (say , , ) instead of a full :
Choosing is itself a tuning problem, done on the development set. Add-k is better than add-one for some tasks, but still generates poorly-shaped counts and awkward discounts for language modeling. Better methods use a different idea.
Backoff and interpolation
The idea behind the better methods: when a high-order n-gram has no evidence, fall back on a lower-order one that does. If a trigram was never seen, its bigram or even its unigram probably was. Two ways to use this hierarchy:2
- Backoff. Use the trigram if its count is nonzero; otherwise back off to the bigram; otherwise to the unigram. Only the highest order with evidence is consulted.
- Interpolation. Always mix all orders, weighting each by a . For the trigram case,
The s sum to one so the result is a proper distribution; they are set to maximize the likelihood of a held-out corpus. In the more refined version each is itself conditioned on the context, so a context with plenty of trigram evidence leans harder on the trigram term. For backoff to stay a valid distribution, the higher orders must be discounted to free up mass for the lower orders — otherwise the borrowed probability pushes the total above one. Backoff with discounting is Katz backoff.
Kneser-Ney, in one idea
The best-performing classical smoother is interpolated Kneser-Ney.3 It starts from absolute discounting: subtract a fixed constant (empirically near ) from every nonzero count, which — as Church and Gale found by comparing a corpus to a held-out copy — closely matches how counts actually shrink from one sample to the next, then interpolate with a lower-order term.
Kneser-Ney's own contribution is the lower-order term. A standard unigram estimates how frequent is, but frequency is the wrong quantity for a fallback. The word Kong is frequent, yet only ever appears after Hong; glasses is less frequent but appears in many contexts. As a novel continuation for an unseen bigram, glasses is the better bet. So Kneser-Ney replaces raw frequency with a continuation probability: how many distinct contexts a word completes.
The numerator counts the distinct words that precede ; the denominator counts all distinct bigram types. Kong, seen in only one context, gets a low continuation probability despite its high raw frequency. Combining absolute discounting on the higher order with continuation probability on the lower gives the interpolated Kneser-Ney bigram estimate:
with a normalizing weight that redistributes exactly the mass the discount removed. The recursion bottoms out at the unigram, interpolated with the uniform distribution . The production variant, modified Kneser-Ney, uses three separate discounts for counts of one, two, and three-or-more.
Why : the Church-Gale held-out experiment
The absolute discount is measured, not tuned. Church and Gale built a bigram model from million words of AP newswire and then looked up, for every bigram that occurred times in that corpus, how often the same bigram occurred in a second, independent -million-word sample.3 The held-out count is what the training count should have been if the corpus were unbiased, and it is almost exactly the training count minus a constant near .
| Count in training | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
|---|---|---|---|---|---|---|---|---|---|---|
| Avg. count in held-out | 0.0000 | 0.45 | 1.25 | 2.24 | 3.23 | 4.21 | 5.23 | 6.21 | 7.21 | 8.26 |
Read across from onward: the held-out average sits about below the training count in every column (, , , ). The high counts are barely touched — subtracting from a count of changes little — while the small counts, the ones we trust least, are pulled down hardest in relative terms. That single measured constant is the discount absolute discounting subtracts.
The normalizing weight is not arbitrary either. It must give the lower-order term exactly the mass the discount removed from the higher order. Each of the distinct words that can follow contributed one discount of , so the total mass freed is times the number of such continuation types, all divided by the prefix count :
The first factor is the normalized discount; the second counts how many times the discount was applied — the number of word types seen after . Multiply them and is precisely the probability mass to redistribute, so still sums to one over the vocabulary.
A full Kneser-Ney computation
For a worked example, take the same three-sentence corpus from the bigram example, without the sentence markers for brevity:
I am SamSam I amI do not like green eggs and hamSet the discount to and estimate . Two ingredients are needed: the discounted higher-order term, and the continuation-probability lower-order term.
Higher-order term. The word I occurs three times (), and the
bigram I am occurs twice (). Absolute discounting shaves
off that count:
Continuation probability of am. Ask in how many distinct contexts am
appears as the second word of a bigram. Scanning the corpus, am is preceded only by
I — twice, once in each of the first two sentences — and by no other word, so it
completes exactly one distinct bigram type: , size . The denominator is the number of
distinct bigram types in the corpus. Listing them — I am, am Sam, Sam I,
I do, do not, not like, like green, green eggs, eggs and, and ham —
gives distinct types (note I am appears twice but counts once as a type):
The weight. After I we see two distinct word types (am and do), so the
discount was applied twice:
Combine. The Kneser-Ney estimate mixes the two:
Compare the plain MLE, . Kneser-Ney has
pulled the estimate down from to , moving the freed of mass onto
all the words that could plausibly follow I — weighted by how broadly each word
appears as a continuation, not by raw frequency. The figure traces the two streams
into the final estimate.
Stupid backoff and web-scale n-grams
Kneser-Ney is the best classical smoother, but the counts it smooths can come from very large corpora. Google's Web 1 Trillion 5-gram release holds n-grams up to length five drawn from over words of English web text; its Google Books n-grams add hundreds of billions more tokens across many languages.4 At that scale two things change: the model is too big to hold naively, and the normalization Kneser-Ney requires stops being worth its cost.
Storing a huge model
A trillion-word model cannot keep its n-grams as plain strings and its probabilities as -byte floats — the table would not fit in memory. Production toolkits make three compromises.4
- Hashing. Each word is stored not as a string but as a -bit hash number, with the string forms kept on disk. The n-grams themselves live in reverse tries, a prefix tree read from the last word back, which shares structure across n-grams with common suffixes.
- Quantization. A probability is stored in only to bits rather than a full float. The precision lost is negligible against the size saved.
- Pruning. Drop the n-grams that carry little information — for instance, keep only those with a count above a threshold (Google's release used a count of ), or prune by an entropy criterion that discards the n-grams whose removal least perturbs the model. Approximate structures like Bloom filters shrink the model further at the cost of occasional false positives.
Toolkits like KenLM combine a backoff weight and a probability into a single stored value and build the tables with a few merge-sort passes over the corpus, so that even web-scale Kneser-Ney models are buildable. But at that size a much cruder scheme often suffices.
The stupid-backoff rule
Stupid backoff gives up on being a true probability distribution altogether.5 It does no discounting: if the full-order n-gram has a nonzero count, use its relative frequency directly; otherwise fall back to the next-lower order, multiplied by a fixed, context-independent weight . Because the result is not normalized, it is written rather than :
The recursion bottoms out at the unigram, scored by its own relative frequency . Brants et al. found works well. Contrast this with Katz backoff earlier in this lesson: Katz discounts the higher orders to free up exactly the right mass for the lower ones, keeping the total a valid distribution. Stupid backoff skips all of that — it just scales the lower order by a constant and never renormalizes.
Dropping normalization is what saves the cost. Renormalizing a backoff model means, for every context, summing over the vocabulary to redistribute the discounted mass — prohibitive when the model has billions of contexts. Stupid backoff skips that sum entirely. The scores no longer sum to one, so they cannot be compared as probabilities across contexts, but for ranking candidate continuations — which is what a downstream system such as machine translation actually needs — a monotonic score is enough. With a large enough corpus the volume of evidence outweighs the crudeness of the estimate, and stupid backoff comes within reach of full Kneser-Ney at a fraction of the engineering.
Beyond n-grams: neural and self-attention language models
Every smoothing scheme in this lesson responds to sparsity the same way: back off to
a shorter context. That is a workaround, not a solution. The
n-gram treats cat and dog as unrelated atoms, so a bigram model that has seen
the cat sat learns nothing about the dog sat. The models that
displaced the n-gram all address this one weakness, and all originate in named,
public work that postdates the counting-and-smoothing view above.
Distributed representations (Bengio et al., 2003)
The neural probabilistic language model of Bengio, Ducharme, Vincent, and Jauvin (JMLR, 2003) keeps the n-gram's prediction task verbatim — estimate from a fixed window — but changes how the context is represented. Each word is mapped to a learned dense vector, its embedding, and the network predicts the next word from the concatenated embeddings of the context rather than from the raw word identities. Words that behave alike are pushed to nearby vectors during training, so evidence shares across them. Having seen the cat gets fed, the model raises its estimate of fed after the dog gets as well, because cat and dog sit close in embedding space. Bengio et al. framed this as breaking the curse of dimensionality: an n-gram must store a separate parameter for each of the exponentially many word combinations, while the neural model shares a small set of embedding dimensions across all of them.6 Smoothing stitches together backoff levels after the fact; the neural model never manufactures the zeros in the first place, because similarity, not exact match, drives its estimate.
Recurrent and self-attention models (Mikolov 2010; Vaswani et al., 2017)
Bengio's model still truncates history to a fixed window, inheriting the n-gram's Markov assumption. Two later architectures removed even that limit. The recurrent neural network language model (Mikolov et al., Interspeech 2010) carries a hidden state forward token by token, in principle conditioning each prediction on the entire preceding sentence rather than a window of words. The transformer (Vaswani, Shazeer, Parmar, Uszkoreit, Jones, Gomez, Kaiser, and Polosukhin, NeurIPS 2017) replaced recurrence with self-attention: at each position the model computes a weighted sum over the representations of all earlier positions, the weights learned per pair of positions, so any prior word can directly influence the current prediction regardless of distance. Self-attention parallelizes across positions in a way recurrence cannot, which is what made training on billions of tokens practical and put the transformer under every large language model that followed.
The n-gram vocabulary is fully preserved in these models. They still assign a probability to the next word, still factor a sequence by the chain rule, still report perplexity, and still contend with rare and unseen words — the very problems set up in this lesson. What changed is only the estimator: from counting and backing off to a learned function that shares statistical strength across similar contexts. On the Penn Treebank and larger benchmarks, neural models cut perplexity far below the best Kneser-Ney n-gram, at the cost of far heavier training. For a small task on a small corpus, a smoothed n-gram is still the right tool and often the faster one; the tradeoff is accuracy against complexity, not one paradigm rendering the other wrong.
Where n-grams lead
The n-gram's prediction task — assign a probability to the next word given a context — is verbatim the task a neural language model solves; the difference is only in how the conditional is estimated. The n-gram counts and smooths; the neural model learns a distributed representation of the context and computes the conditional with a network, which sidesteps sparsity by sharing statistical strength across similar contexts instead of stitching together backoff levels. The vocabulary set up here — sequence probability, the Markov approximation, maximum likelihood, perplexity, the zero problem — is the vocabulary those models inherit. We pick the thread up with vector semantics, which learns to represent a word's meaning as a vector, and then with neural language models, which use those vectors to predict the next word.
Footnotes
- Jurafsky & Martin, §3.5 — Smoothing: Laplace (add-one) and add-k smoothing, the adjusted count and discount, worked on the Berkeley bigrams. ↩
- Jurafsky & Martin, §3.5.3; §3.6 — Backoff and Interpolation, and Katz backoff: using less context when a higher-order n-gram lacks evidence, with discounting to preserve a valid distribution. ↩
- Jurafsky & Martin, §3.6 — Kneser-Ney Smoothing: absolute discounting and the continuation-probability lower-order term, interpolated into , with modified Kneser-Ney as the production variant. ↩ ↩2
- Jurafsky & Martin, §3.7 — Huge Language Models and Stupid Backoff: the Google Web 1 Trillion 5-gram and Books n-gram corpora, and the efficiency measures for web-scale models — -bit word hashes, reverse tries, –-bit probability quantization, count- and entropy-based pruning, Bloom filters, and toolkits like KenLM. ↩ ↩2
- Jurafsky & Martin, §3.7 — Stupid Backoff (Brants et al. 2007): the un-normalized score that uses the full-order relative frequency when the count is nonzero and otherwise backs off to the lower order scaled by a fixed , terminating in the unigram ; it abandons discounting and normalization but matches Kneser-Ney at web scale. ↩
- Neural successors to the n-gram model. Bengio, Ducharme, Vincent, and Jauvin,
A Neural Probabilistic Language Model,
Journal of Machine Learning Research 3 (2003) — learned word embeddings and a feedforward network that estimates the next-word probability from the context embeddings, breaking the curse of dimensionality by sharing statistical strength across similar words. Mikolov, Karafiát, Burget, Černocký, and Khudanpur,Recurrent Neural Network Based Language Model,
Interspeech (2010) — a hidden state carried across the whole prefix, removing the fixed-window Markov assumption. Vaswani, Shazeer, Parmar, Uszkoreit, Jones, Gomez, Kaiser, and Polosukhin,Attention Is All You Need,
NeurIPS (2017) — the transformer, which replaces recurrence with self-attention over all prior positions and underlies modern large language models. All three keep the n-gram's prediction task, chain-rule factorization, and perplexity metric, changing only the estimator. ↩
╌╌ END ╌╌