ICS 582Lecture 03Part 08
Kneser-Ney smoothing for bigrams
Why backing off to raw unigram frequency fails (reading Kong versus reading glasses), the continuation probability that counts distinct contexts, the interpolated Kneser-Ney bigram formula, and a full step-by-step toy computation.
- Concepts
- 6
- Slides
- 81-94
- Reading
- 36 min
Why this part matters
Kneser-Ney is the payoff of the whole smoothing story. It keeps the discount from absolute discounting but changes what the model backs off to: instead of asking how often a word occurs, it asks in how many different contexts the word has been seen. Modified Kneser-Ney was the standard n-gram baseline for about two decades after Chen and Goodman's 1998 study, and it is still what KenLM and SRILM build and what shallow-fusion decoders in speech recognition and translation plug in.
The part follows one toy corpus from start to finish: 30 copies of "Hong Kong" and four different words before "glasses". First we watch frequency-based backoff make the wrong choice, then we build the continuation probability that fixes it, write the full bigram formula, derive its weight λ from first principles, and finally compute every number by hand. That last computation is the one exams ask for, so expect to do it on paper.
By the end you can
- Explain why backing off to raw unigram frequency ranks Kong above glasses after "reading".
- Define the continuation count and P_cont and compute them from a list of bigram types.
- Write the interpolated Kneser-Ney bigram formula and say what each of its two terms does.
- Derive lambda(h) from the discounted mass and prove that P_KN(. | h) sums to 1.
- Compute P_KN for any word and history in the toy corpus, including the Hong sanity check.
Fill the gap: "I can't see without my reading ___." Every English speaker says "glasses". Now suppose a Bigram model has never seen the pair (reading, glasses), or has seen it so rarely that it must lean on a lower-order estimate. With classic Backoff or Absolute discounting, that lower-order estimate is the Unigram probability P(w), and in a corpus full of news about "Hong Kong" the word Kong is very frequent.
Make it concrete with the toy corpus used throughout this part: C(Hong Kong) = 30 and one each of reading glasses, sun glasses, safety glasses and my glasses. Count the second word of each of the 34 bigram tokens. Kong appears 30 times and glasses 4 times, so the maximum-likelihood unigram gives P(Kong) = 30 / 34 ≈ 0.882 and P(glasses) = 4 / 34 ≈ 0.118. Absolute discounting with d = 0.75 keeps 1 − 0.75 = 0.25 for the one observed bigram after "reading" and spreads the freed 0.75 by the unigram:
That gives P_AD(Kong | reading) = 0.75 × 0.882 ≈ 0.662 and P_AD(glasses | reading) = 0.25 + 0.75 × 0.118 ≈ 0.338. Kong, a word that has only ever followed Hong, gets about twice the probability of the word that actually fits. (The slides state the problem in words; these numbers are ours, computed from the slide 88 corpus.)
The failure is in the question, not the arithmetic. When the model backs off, it is in a situation it has not seen: this history has not been followed by this word before. The useful question there is not "how likely is w?" but "how likely is w to show up as a novel continuation, after a context it has never followed?" Jurafsky and Martin phrase it exactly that way, and the answer to the second question is the Continuation probability, the lower-order distribution of Kneser-Ney smoothing. Raw frequency is a poor proxy for it because frequency can be concentrated: thirty tokens of Kong all come from one fixed phrase, which tells us nothing about Kong turning up somewhere new. This is the same Sparsity problem as before, now hitting the fallback instead of the main estimate.
| Lower-order distribution | Question it answers | Kong | glasses |
|---|---|---|---|
| Raw unigram P(w) | How likely is w? | 30 / 34 ≈ 0.882 | 4 / 34 ≈ 0.118 |
| Continuation P_cont(w) | How likely is w as a novel continuation? | 1 / 5 = 0.2 | 4 / 5 = 0.8 |
Recall
Why does an absolute-discounting model that backs off to the raw unigram rank Kong above glasses after "reading"?
Look at the words immediately to the left of each target. Kong has exactly one left neighbour, Hong. glasses has four: reading, sun, safety and my. Kong appears 30 times, but every one of those tokens is the same context repeated, so it earns credit for only one. That number of distinct left neighbours is the Continuation count:
To turn it into a probability, divide by the total number of bigram types, |{(u, w′) : C(uw′) > 0}|. The reasoning is that every bigram type was a novel continuation the first time it appeared, so the set of bigram types is the full record of "a word showed up in a new context", and each word's share of that record is its continuation probability. Jurafsky and Martin also write the denominator as the sum of every word's continuation count (Eq. C.6). The two are the same number, because each bigram type contributes one to exactly one word's count; in the toy corpus both equal 5.
| Word | Tokens as second word | Distinct preceding words | Continuation count | P_cont |
|---|---|---|---|---|
| Kong | 30 | Hong | 1 | 1 / 5 = 0.2 |
| glasses | 4 | reading, sun, safety, my | 4 | 4 / 5 = 0.8 |
| Hong | 0 | none | 0 | 0 / 5 = 0 |
This is a different kind of Relative frequency: the unit being counted is a context, not a token. As Jurafsky and Martin put it, a frequent word occurring in only one context will have a low continuation probability, and SRILM's manual describes its lower-order Kneser-Ney estimate as proportional to the number of unique words that precede it in the training data. The same machinery handles any vocabulary: the words "Francisco" (almost always after San) and "York" (after New) look just like Kong.
Recall
Define the continuation count and P_cont, and compute both for Kong and glasses in the toy corpus.
Quick check
Why does Kneser-Ney give glasses a higher continuation probability than Kong?
Take P_KN(glasses | reading) in Kneser-Ney smoothing. The bigram "reading glasses" was seen once. The first term keeps what that count earned after paying the discount, (1 − 0.75) / 1 = 0.25. The second term adds a share of the freed mass, chosen by how widely glasses is used as a continuation. Written in general:
The denominator is the count of as a history, , which Jurafsky and Martin write explicitly in Eq. C.1 and C.8. The second term scales the Continuation probability by the Backoff weight (lambda) λ of the history, the mass the discount freed. Everything else is inherited: the Discounting step is exactly absolute discounting, and the structure is that of Linear interpolation with one difference that matters a lot, the lower order is P_cont instead of the Maximum likelihood estimation unigram.
The model is called interpolated because the second term is added for every word, even for bigrams that were seen. A backoff version of Kneser-Ney uses the continuation term only when and rescales it accordingly. Both exist in practice: SRILM builds either, and its -interpolate flag selects the interpolated form. Kneser and Ney introduced the method in 1995, and Chen and Goodman's large comparison found that a Kneser-Ney variant consistently outperforms all the other smoothing algorithms they evaluated.
History "reading" has one token and one continuation type. Discounting that single bigram removes 0.75 / 1 = 0.75 of the probability mass, so the continuation term must hand out exactly 0.75: λ(reading) = 0.75. History "Hong" has 30 tokens and still one type, so discounting frees only 0.75 / 30 = 0.025. The Backoff weight (lambda) is not chosen; it is whatever the discount removed.
Read it as two factors. d / C(h) is how much one discount costs in probability units for this history, and the set size N1+(h •) is how many times the discount was paid, once per distinct word that follows h. The product is the total mass removed, and the proof that the distribution is still a proper one is three lines long.
Worked example
Proof that P_KN(· | h) sums to 1
Sum the discounted terms
Only the N1+(h •) seen words contribute, and each loses exactly d: Σ_w max(C(hw) − d, 0) / C(h) = (C(h) − d · N1+(h •)) / C(h).Recognize lambda
That equals 1 − d · N1+(h •) / C(h) = 1 − λ(h).Sum the continuation terms
P_cont is a distribution, so Σ_w λ(h) P_cont(w) = λ(h) · 1 = λ(h).Result
(1 − λ(h)) + λ(h) = 1. For "reading": 0.25 + 0.75 = 1. For "Hong": 0.975 + 0.025 = 1.
The d = 0.75 used throughout is the same discount Part 07 measured: the Church and Gale held-out experiment held-out table shows counts of 2 to 9 shrinking by about 0.75, and d = n1 / (n1 + 2 n2) estimates it from counts of counts. Kneser-Ney inherits that discount unchanged; what it changes is where the freed mass goes.
Recall
Show that P_KN(· | h) sums to 1.
Recall
If C(h) doubles but the number of continuation types after h stays the same, what happens to λ(h), and why does that make sense?
Quick check
In λ(h) = d / C(h) × |{w : C(hw) > 0}|, what does the set size count?
Here is the full computation on the slide corpus. Every Kneser-Ney smoothing question, by hand or in code, follows the same order: continuation probabilities first, then the weight of the history, then each word's discounted term and continuation term, and finally a check that the probabilities sum to 1.
The toy corpus (slide 88)
- C(Hong Kong)
- 30
- C(reading glasses)
- 1
- C(sun glasses)
- 1
- C(safety glasses)
- 1
- C(my glasses)
- 1
- Discount d
- 0.75
- Bigram types
- 5
Worked example
P_KN after the history reading, d = 0.75
Step 1: continuation probabilities
Kong has one left context (Hong), glasses has four (reading, sun, safety, my), and there are 5 bigram types. P_cont(Kong) = 1 / 5 = 0.2 and P_cont(glasses) = 4 / 5 = 0.8.Step 2: the backoff weight of reading
"reading" occurs once as a history and is followed by one type, so λ(reading) = 0.75 / 1 × 1 = 0.75.Step 3: glasses after reading
P_KN(glasses | reading) = max(1 − 0.75, 0) / 1 + 0.75 × 0.8 = 0.25 + 0.6 = 0.85.Step 4: Kong after reading
C(reading Kong) = 0, so P_KN(Kong | reading) = 0 + 0.75 × 0.2 = 0.15.Result
0.85 + 0.15 = 1. Every other word has P_cont = 0 in this corpus, so nothing else gets mass after "reading".
Notice where glasses gets its probability. The bigram "reading glasses" contributes only 0.25; the other 0.6 arrives through the continuation term, because glasses is a word that turns up after many different words. With a single observation of the history, the model trusts the history little (λ is large) and the continuation distribution does most of the work.
Quick check
In the slide 88 toy corpus with d = 0.75, what is P_KN(Kong | reading)?
Put the two models side by side. After "reading", frequency-unigram Backoff gave Kong about 0.662 and glasses about 0.338; Kneser-Ney smoothing gives Kong 0.15 and glasses 0.85. The ranking flips. But does Kneser-Ney now treat Kong unfairly where Kong belongs? Check the history Hong, which the slides start and leave unfinished.
C(Hong) = 30 with one continuation type, so λ(Hong) = 0.75 / 30 × 1 = 0.025. Then P_KN(Kong | Hong) = 29.25 / 30 + 0.025 × 0.2 = 0.975 + 0.005 = 0.98 and P_KN(glasses | Hong) = 0 + 0.025 × 0.8 = 0.02, which sum to 1. Kong keeps almost all the mass after Hong. Kneser-Ney did not punish Kong; it only removed Kong's unearned advantage in contexts it was never seen in.
| History | Word | Frequency backoff | Kneser-Ney |
|---|---|---|---|
| reading | Kong | 0.662 | 0.15 |
| reading | glasses | 0.338 | 0.85 |
| Hong | Kong | 0.997 | 0.98 |
| Hong | glasses | 0.003 | 0.02 |
The two histories show the full logic. Where the history is strong evidence, λ is tiny and the bigram dominates. Where the history is thin, λ is large and the continuation distribution decides, and there diversity beats frequency. The slogan to remember is that backoff should model continuation diversity, not token frequency.
From the toy to real toolkits
Real systems use Modified Kneser-Ney, which replaces the single d with three discounts, D1, D2 and D3+, for n-grams seen once, twice, and three or more times (JM C.2; Part 09 derives them). It is the default in KenLM, whose documentation says it estimates unpruned language models with modified Kneser-Ney smoothing, and in SRILM through -kndiscount (with -ukndiscount for the original single-discount version). Heafield and colleagues built an unpruned modified Kneser-Ney model on 126 billion tokens on one machine with 140 GB of RAM in 2.8 days, and it gained 0.8 BLEU in the WMT 2013 translation task.
Recall
Compute P_KN(glasses | Hong) and P_KN(Kong | Hong) with d = 0.75.
Recall
In the toy corpus, what is P_cont(Hong), and why is that a problem for a real model?
Quick check
For history Hong (C(Hong) = 30, one continuation type, d = 0.75), what is λ(Hong)?
Recap
If you remember nothing else
- Backing off to P(w) asks "how frequent is w?", so Kong, inflated by "Hong Kong", beats glasses after "reading".
- Kneser-Ney backs off to P_cont(w) = distinct left contexts of w / number of bigram types. In the toy corpus Kong gets 1/5 = 0.2 and glasses gets 4/5 = 0.8.
- P_KN(w | h) = max(C(hw) - d, 0) / C(h) + lambda(h) P_cont(w). It is interpolated: the backoff term is added even for seen bigrams.
- lambda(h) = d / C(h) times the number of distinct continuations of h, which is exactly the mass removed by discounting, so the distribution sums to 1.
- Toy corpus with d = 0.75: lambda(reading) = 0.75, P_KN(glasses | reading) = 0.25 + 0.6 = 0.85, P_KN(Kong | reading) = 0.15.
- History Hong: lambda(Hong) = 0.025, P_KN(Kong | Hong) = 0.98, P_KN(glasses | Hong) = 0.02. Strong histories barely back off.
- Slogan: backoff should model continuation diversity, not token frequency. Modified Kneser-Ney (D1, D2, D3+) is the version real toolkits use.
Sources
- Speech and Language Processing, Appendix C: Kneser-Ney SmoothingBookJurafsky and Martin, Stanford (draft of Aug 19 2026)Eq. C.1 to C.11: absolute discounting, the Hong Kong and reading glasses example, continuation probability, lambda, Church and Gale, d = n1 / (n1 + 2 n2), modified Kneser-Ney.(opens in a new tab)
- Speech and Language Processing, Chapter 3: N-gram Language ModelsBookJurafsky and Martin, StanfordHistory section: Modified Interpolated Kneser-Ney as the standard baseline, with SRILM and KenLM.(opens in a new tab)
- Improved backing-off for M-gram language modelingPaperKneser and Ney, ICASSP 1995, pp. 181 to 184(opens in a new tab)
- An Empirical Study of Smoothing Techniques for Language Modeling (TR-10-98)PaperChen and Goodman, Harvard University, 1998A Kneser-Ney variant consistently outperforms all other algorithms evaluated.(opens in a new tab)
- An Empirical Study of Smoothing Techniques for Language ModelingPaperChen and Goodman, Computer Speech and Language 13, 1999(opens in a new tab)
- Scalable Modified Kneser-Ney Language Model EstimationPaperHeafield, Pouzyrevsky, Clark and Koehn, ACL 2013126 billion tokens, 140 GB RAM, 2.8 days, +0.8 BLEU.(opens in a new tab)
- KenLM Language Model ToolkitDocsKenneth Heafield(opens in a new tab)
- SRILM ngram-discount manualDocsSRI InternationalLower-order Kneser-Ney proportional to the number of unique preceding words; -kndiscount, -ukndiscount and -interpolate.(opens in a new tab)