ICS 582Lecture 03Part 09
Recursive and modified Kneser-Ney
The recursive interpolated Kneser-Ney form for higher-order n-grams, the KN count that switches to continuation counts at lower orders, termination at a uniform distribution, modified KN with three discounts, and why neural LMs came next.
- Concepts
- 6
- Slides
- 95-101
- Reading
- 36 min
Why this part matters
Part 08 built Kneser-Ney for bigrams. Real systems use trigrams, 4-grams and 5-grams, so the method has to work at any order. This part turns the bigram formula into a recursion, fixes exactly which count each level uses, shows where the recursion stops, and upgrades the single discount to the three discounts of modified Kneser-Ney.
This matters in three places. Almost every strong n-gram model you will build or meet in a paper, from KenLM features in machine translation to rescoring in speech recognition and baselines in language-model papers, is interpolated modified Kneser-Ney. Exams ask what c_KN means at each level and why there are three discounts. And Bengio's neural language model was measured against modified Kneser-Ney (its backoff form, built with SRILM), and its learned word vectors are the idea behind the word embeddings of lecture 04.
By the end you can
- Write the recursive interpolated Kneser-Ney equation and the backoff weight that keeps every level normalized.
- Say which count c_KN uses at the highest order and at each lower order, and compute continuation counts from a corpus.
- Carry a trigram probability down to the uniform floor and give the probability of <UNK>.
- Compute the modified Kneser-Ney discounts D1, D2 and D3+ from counts of counts and explain why there are three.
- Name the toolkits and flags that build modified KN models and the two limits that pushed the field to neural LMs.
Start from the bigram formula of Part 08: a discounted bigram term plus λ(h) times a lower-order distribution. Now lift it one level. For a trigram model and the history "her reading", the probability of the next word is the discounted trigram term plus λ(her reading) times P_KN(w | reading). That bigram probability is itself a Kneser-Ney estimate, so it in turn is a discounted bigram term plus λ(reading) times the unigram level. One formula, applied to shorter and shorter histories, covers every order.
The slide leaves λ undefined for the general form, but it is forced by the same argument as in part 08. The Absolute discounting step removes d from every n-gram type that follows the history h, and the backoff weight must hand exactly that mass to the shorter history:
Because λ(h) equals the removed mass, each level sums to 1 on its own, provided the level below is a proper distribution. The proof is the three-line argument from Part 08, applied once per level. This is Linear interpolation with weights that are computed rather than tuned, and it is what Chen and Goodman found works best: Heafield and colleagues summarize their result as the recursion p(w_n | w_1^(n−1)) = u(w_n | w_1^(n−1)) + b(w_1^(n−1)) p(w_n | w_2^(n−1)), where u is the discounted term and b the backoff weight. The SRILM manual writes the same thing as p(a_z) = g(a_z) + bow(a_) p(_z) and adds that each n-gram order uses a different discounting constant.
| Aspect | Interpolated KN | Backoff KN |
|---|---|---|
| When the lower order is used | Always, for every word, seen or unseen | Only when the n-gram is unseen |
| Weight on the lower order | λ(h) = d · N1+(h•) / Σ_v c_KN(hv) | A backoff weight α(h) rescaled so that only unseen words share the freed mass |
| Seen n-gram probability | Discounted count plus a share of the lower order | Discounted count only |
| SRILM flags | -kndiscount -interpolate | -kndiscount |
Recall
Write the recursive interpolated KN equation and give λ(h).
The recursion writes c_KN everywhere, and the whole method depends on what that count means at each level. Take a small corpus of seven sentences, with no boundary tokens to keep the arithmetic short:
The toy corpus for this part
- Sentence 1 and 2 (twice)
- she wore her reading glasses
- Sentence 3
- he lost his reading glasses
- Sentence 4
- she wore her sun glasses
- Sentence 5
- he visited hong kong
- Sentence 6
- she visited hong kong
- Sentence 7
- he loves hong kong
- Vocabulary V
- 13
The bigram "reading glasses" occurs 3 times: twice after "her" and once after "his". At the bigram level of a trigram model, though, Kneser-Ney does not use that raw count. It uses the number of distinct words seen immediately to its left, which is 2 ({her, his}). The two copies of "her reading glasses" count once. The same rule applies one level further down: "glasses" occurs 4 times but follows only 2 distinct words ({reading, sun}), and "kong" occurs 3 times but always after "hong", so its count is 1.
The Continuation count is the number of unique single-word contexts, which for a bigram reads:
The denominators switch too. At the bigram level the history "reading" has Σ_v c_KN(reading v) = 2, not C(reading) = 3, because every term in the sum is a continuation count. At the unigram level, dividing c_KN(w) by Σ_w c_KN(w) gives back the Continuation probability of Part 08 (before discounting), so the bigram model there was simply the two-level case of this recursion. SRILM's manual puts the rule in one sentence: the modified probability for a lower-order n-gram is proportional to the number of unique words that precede it.
| N-gram | Level | Raw count | c_KN | Distinct left neighbours |
|---|---|---|---|---|
| reading glasses | bigram | 3 | 2 | her, his |
| glasses | unigram | 4 | 2 | reading, sun |
| hong | unigram | 3 | 2 | visited, loves |
| kong | unigram | 3 | 1 | hong |
| she | unigram | 4 | 0 | none (always first) |
The last row of the table shows why the boundary edge case matters. "she" begins every sentence it is in, so nothing ever precedes it and its continuation count is 0. In a real model with Sentence boundary tokens, every sentence starts with <s>, and nothing can precede <s> either. If n-grams starting with <s> used continuation counts, they would all collapse to zero. Both KenLM and SRILM therefore keep raw counts for n-grams that start with <s>: Heafield defines the adjusted count as a(w) = c(w) when the order is the highest or w_1 = <s>, and SRILM says only the highest-order n-grams and n-grams that start with <s> keep their regular counts. The slide omits this exception.
Recall
In the toy corpus, why is C(reading glasses) = 3 but c_KN(reading glasses) = 2?
Quick check
In an interpolated trigram KN model, what does c_KN(reading glasses) count?
Part 08 ended with P_KN(Hong | reading) = 0, because Hong never appeared as a second word: a word that never appears after anything has continuation probability 0, so no history can ever predict it. The recursion has to end somewhere, and Kneser-Ney ends it in a way that removes this last zero. The Unigram level is itself discounted and interpolated, with the uniform distribution over the vocabulary:
Every word, seen or not, now receives at least λ(ε)/V at the unigram level, which ends the Zero-probability problem for good. That includes the unknown word: <UNK> token is treated as an ordinary vocabulary entry whose count is 0, so the first term vanishes and its probability is exactly λ(ε)/V. Heafield and colleagues describe the same design in KenLM: recursion terminates when unigrams are interpolated with the uniform distribution, and the unknown word has count zero, so its probability is b(ε)/|vocabulary|.
Worked example
P_KN(glasses | her reading) on the toy corpus, d = 0.75 at every level
Unigram level
V = 13. The continuation counts sum to 15 (one per bigram type), and 11 words have c_KN > 0, so λ(ε) = 0.75 × 11 / 15 = 0.55. Then P(glasses) = 1.25 / 15 + 0.55 / 13 = 0.0833 + 0.0423 = 0.1256 (exactly 49/390) and P(kong) = 0.25 / 15 + 0.0423 = 0.0590 (exactly 23/390). "she" and "he" have continuation count 0, so they get only the floor, 0.0423. In this corpus she plays Hong's role and is rescued by the floor.Bigram level, history reading
Σ_v c_KN(reading v) = 2 with 1 type, so λ(reading) = 0.75 × 1 / 2 = 0.375. P(glasses | reading) = 1.25 / 2 + 0.375 × 0.1256 = 0.625 + 0.0471 = 0.6721 and P(kong | reading) = 0 + 0.375 × 0.0590 = 0.0221.Trigram level, history her reading
Raw C(her reading glasses) = 2, the history total is 2 with 1 type, so λ(her reading) = 0.375. P(glasses | her reading) = 0.625 + 0.375 × 0.6721 = 0.625 + 0.2520 = 0.8770 and P(kong | her reading) = 0 + 0.375 × 0.0221 = 0.0083.Result
An unseen trigram, "her reading kong", still gets a non-zero and sensible probability, and kong stays low because its continuation count is 1. Each level sums to 1 over the vocabulary (checked with exact fractions).
Seven sentences, V = 13. The top level uses raw trigram counts; the bigram and unigram levels use continuation counts (distinct left neighbours), denominators included. Each row adds its discounted term to λ times the row below it.
- trigramraw countmax(2 − d, 0) / 2 = 0.625; λ(her reading) = d × 1 / 2 = 0.375P(glasses) = 0.8770
- bigramcontinuation countmax(2 − d, 0) / 2 = 0.625; λ(reading) = d × 1 / 2 = 0.375P(glasses) = 0.6721
- unigramcontinuation countmax(2 − d, 0) / 15 = 0.083; λ(ε) = d × 11 / 15 = 0.55P(glasses) = 0.1256
- uniform1 / V = 1 / 130.0769
Try history "wore her" with target "she" in the simulator. No level has ever seen "she" after anything, yet the probability is about 0.016, all of it trickling down from the uniform floor through three λ weights. Then push d up: every λ grows, the top level trusts its own counts less, and the floor rises.
Recall
What probability does <UNK> receive in KN and why is it non-zero?
Quick check
At the end of the KN recursion, the unigram distribution is interpolated with what?
Look again at the held-out experiment behind d = 0.75. Church and Gale counted bigrams in 22 million words of AP newswire and checked how often each appeared in another 22 million. Bigrams seen twice appeared on average 1.25 times (they shrink by 0.75), those seen three times 2.24 times (by 0.76), and those seen four times 3.23 times (by 0.77). But bigrams seen once appeared only 0.448 times, a shrink of 0.55. One discount cannot fit both.
The Church and Gale numbers show that a single d is a compromise: it over-discounts singletons and slightly under-discounts the rest. Jurafsky and Martin already hint at the fix, suggesting perhaps keeping a separate second discount value of 0.5 for the bigrams with counts of 1. Chen and Goodman made it systematic. Modified Kneser-Ney uses D1 for n-grams seen once, D2 for those seen twice and D3+ for three or more (the slide writes d1, d2, d3+), and Jurafsky and Martin call it the best-performing version of Kneser-Ney smoothing.
Estimating the three discounts
The discounts are not guessed. They come in closed form from the counts of counts of each order: n_k is the number of distinct n-grams of that order seen exactly k times. The original single-discount Kneser-Ney already used this idea, with d = n1 / (n1 + 2 n2) from Ney et al. (JM Eq. C.2, SRILM -ukndiscount). Modified Kneser-Ney keeps that quantity as Y and derives three discounts from it:
Worked example
Discounts from counts of counts
Counts of counts
Suppose an order has n1 = 1000, n2 = 300, n3 = 150 and n4 = 90.Y
Y = 1000 / (1000 + 600) = 0.625.The three discounts
D1 = 1 − 2 × 0.625 × 300/1000 = 0.625, D2 = 2 − 3 × 0.625 × 150/300 = 1.0625, D3+ = 3 − 4 × 0.625 × 90/150 = 1.5.Result
The discount grows with the count bucket: singletons lose 0.625, frequent n-grams lose 1.5.
The reserved mass changes with it
The structure stays the same: discount, then interpolate. What changes is the mass a history reserves for the lower order. Instead of d times the number of continuation types, each type contributes the discount of its own bucket, where N_k(h•) is the number of words seen exactly k times after h:
Worked example
Reserved mass for one history
The history
A history h is followed by four word types with counts 1, 1, 2 and 5, so Σ c(hw) = 9.Reserved mass
γ(h) = (0.625 × 2 + 1.0625 × 1 + 1.5 × 1) / 9 = 3.8125 / 9 = 0.4236. A single d = 0.75 would reserve 0.75 × 4 / 9 = 0.333.Discounted terms
Count 5: 3.5 / 9 = 0.3889. Count 2: 0.9375 / 9 = 0.1042. Each count 1: 0.375 / 9 = 0.0417.Result
The discounted terms sum to 0.5764, and adding γ(h) = 0.4236 gives exactly 1. Because D2 and D3+ exceed 0.75, the continuations seen 2 and 5 times free more mass, so this history reserves more for the lower order than a single d = 0.75 would.
| Aspect | Kneser-Ney | Modified Kneser-Ney |
|---|---|---|
| Discounts per order | 1 (d) | 3 (D1, D2, D3+) |
| How the discount is estimated | d = n1 / (n1 + 2 n2) | Dk = k − (k+1) Y n(k+1) / n(k) |
| Reserved mass for history h | d · N1+(h•) / Σ c(hw) | (D1 N1 + D2 N2 + D3+ N3+) / Σ c(hw) |
| SRILM flag | -ukndiscount | -kndiscount |
| KenLM | Not offered | lmplz (default) |
Recall
Give the modified KN discount formulas.
Quick check
Why does modified Kneser-Ney use three discounts?
Quick check
With D1 = 0.625, D2 = 1.0625 and D3+ = 1.5, a history followed by four types with counts 1, 1, 2, 5 reserves how much mass?
Suppose you need a 5-gram language model for a translation or speech system. You do not implement the recursion yourself. You run one of two toolkits, and both build interpolated modified Kneser-Ney:
lmplz -o 5 <corpus.txt >model.arpa
ngram-count -order 5 -text corpus.txt -kndiscount -interpolate -lm model.arpaThe discounts are hyperparameters, but rarely hand-tuned ones. Both toolkits estimate them in closed form from the counts of counts of the training data, a separate set per order. They can also be tuned on a development set by minimizing perplexity, which is what the slide means by smoothing parameters tuned on held-out data. KenLM's documentation states that it estimates unpruned language models with modified Kneser-Ney smoothing, and its --discount_fallback option supplies 0.5, 1.0 and 1.5 for singletons, doubletons and higher counts when the counts of counts are unusable, for instance on tiny or deduplicated data.
KenLM and SRILM at a glance
- KenLM smoothing
- Interpolated modified Kneser-Ney, unpruned by default
- KenLM command
- lmplz -o 5 <text >model.arpa
- KenLM fallback discounts
- --discount_fallback uses 0.5, 1.0 and 1.5 when counts of counts are unusable
- KenLM scale
- 126 billion tokens in 2.8 days on one machine with 140 GB RAM
- SRILM command
- ngram-count -order 5 -kndiscount -interpolate
- SRILM variants
- -kndiscountn modified, -ukndiscountn original, -interpolaten interpolated, per order n
Kneser-Ney shines at high orders. In a 5-gram model most 5-word contexts are seen once or never, so most predictions lean on the lower levels, and that is where continuation counts make the difference. This is the sparsity problem in its sharpest form. Chen and Goodman's study showed the advantages of modified interpolated Kneser-Ney, which became the standard n-gram baseline around the turn of the century. Goodman later reported that a combination of techniques including interpolated Kneser-Ney gave a 38% to 50% perplexity reduction against a Katz trigram, and an 8.9% word error rate reduction. At scale, Heafield and colleagues estimated an unpruned model on 126 billion tokens in 2.8 days on one machine with 140 GB of RAM, gaining 0.8 BLEU, and on 302 million tokens KenLM used 7.7% of SRILM's RAM and 14.0% of its wall time.
Quick check
Which SRILM option set builds the interpolated modified Kneser-Ney model taught in this part?
With a vocabulary of V = 50,000, a trigram model has V³ = 1.25 × 10^14 possible trigrams. Bengio and colleagues put it more starkly: modelling 10 consecutive words with V = 100,000 means 10^50 − 1 free parameters. And even a perfectly smoothed N-gram model that has read "the cat is walking in the bedroom" learns nothing about "a dog was running in a room", because none of its n-grams match.
These are the two walls. First, n-gram parameters grow exponentially in n, so longer contexts are almost never observed and smoothing has to do more and more of the work. Second, n-grams generalize only through exact matches: cat and dog are just different strings, so evidence about one never transfers to the other. Kneser-Ney makes the best of counts, but it cannot fix either problem, because both come from treating words as atomic symbols.
A Neural language model maps each word into a continuous vector space where similar words, and similar contexts, get similar vectors. A sentence about a cat then raises the probability of the same sentence about a dog, and the number of parameters grows only linearly with V and n. Bengio et al. reported test perplexity about 24% lower on Brown and about 8% lower on AP News than the best n-gram model on each corpus: a class-based model on Brown and modified Kneser-Ney backoff on AP News. Mixing the neural model with a trigram always helped further. Lecture 04 develops this idea of words as vectors: word embeddings. Transformer language models, later in the course, are the descendants of this idea.
| Aspect | n-gram KN | Neural LM |
|---|---|---|
| Parameters | Up to V^n counts, exponential in n | Grow linearly with V and n |
| Generalization | Only through exact matches of the context | Through similar vectors for similar words and contexts |
| Context length | Fixed and short (3 to 5 words in practice) | Longer windows; later models use whole documents |
| Compute | Counting and lookup, very cheap | Training by gradient descent, far more expensive |
Recall
Why can no smoothing method, however good, let evidence about cat help predict dog?
Recap
If you remember nothing else
- P_KN(w|h) = max(c_KN(hw) − d, 0) / Σ_v c_KN(hv) + λ(h) P_KN(w|h'), with λ(h) = d × (distinct continuations of h) / Σ_v c_KN(hv).
- c_KN is the raw count at the highest order and the continuation count (distinct left contexts) at every lower order, denominators included. N-grams that start with <s> keep raw counts.
- The recursion ends with the unigram interpolated with uniform 1/V. <UNK> is a count-0 word and gets λ(ε)/V.
- Modified KN uses D1, D2 and D3+ per order: Y = n1/(n1 + 2n2), Dk = k − (k+1) Y n_{k+1}/n_k. Each continuation type is discounted by the D of its count bucket, so the reserved mass depends on how often h's continuations were seen (D1 always equals the single-discount Y = n1/(n1 + 2n2)).
- Interpolated modified KN is the standard n-gram baseline in KenLM (lmplz) and SRILM (-kndiscount -interpolate).
- n-grams have exponentially many parameters and no generalization across similar words. Neural LMs fix both with continuous representations, beating the best n-gram model by about 24% perplexity on Brown (a class-based model) and 8% on AP News (modified KN backoff) in Bengio et al. 2003.
Sources
- Speech and Language Processing, Appendix C: Kneser-Ney SmoothingBookJurafsky and Martin, Stanford (draft of Aug 2026)Eq. C.1 to C.11, the Church and Gale table (Fig. C.1), the 0.5 singleton suggestion, and modified Kneser-Ney.(opens in a new tab)
- Speech and Language Processing, Chapter 3: N-gram Language ModelsBookJurafsky and Martin, StanfordModified interpolated KN as the standard baseline, SRILM and KenLM, and the two n-gram problems.(opens in a new tab)
- Scalable Modified Kneser-Ney Language Model EstimationPaperHeafield, Pouzyrevsky, Clark and Koehn, ACL 2013Adjusted counts, D_n(k), the backoff b, uniform termination and <unk>, plus the 126 billion token and SRILM comparison results.(opens in a new tab)
- KenLM estimation (lmplz) documentationDocsKenneth HeafieldModified Kneser-Ney; discount_fallback defaults 0.5, 1.0 and 1.5.(opens in a new tab)
- SRILM ngram-discount(7)DocsSRI InternationalKN lower-order counts, interpolated and backoff formulas, Y, D1, D2 and D3+.(opens in a new tab)
- SRILM ngram-count(1)DocsSRI InternationalThe -kndiscount, -ukndiscount and -interpolate flags.(opens in a new tab)
- An Empirical Study of Smoothing Techniques for Language Modeling (TR-10-98)PaperChen and Goodman, Harvard University, 1998Origin of modified Kneser-Ney.(opens in a new tab)
- An Empirical Study of Smoothing Techniques for Language ModelingPaperChen and Goodman, Computer Speech and Language 13(4), 1999(opens in a new tab)
- Improved backing-off for M-gram language modelingPaperKneser and Ney, ICASSP 1995(opens in a new tab)
- A Bit of Progress in Language ModelingPaperGoodman, 200138% to 50% perplexity reduction against a Katz trigram baseline, 8.9% WER reduction.(opens in a new tab)
- A Neural Probabilistic Language ModelPaperBengio, Ducharme, Vincent and Jauvin, JMLR 3, 200310^50 − 1 parameters; about 24% lower perplexity on Brown than the best n-gram (class-based) and 8% on AP News than modified KN backoff.(opens in a new tab)