Majid Al-RaimiKneser-Ney smoothing for bigrams

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
Understood
0/6 concepts

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

  1. Explain why backing off to raw unigram frequency ranks Kong above glasses after "reading".
  2. Define the continuation count and P_cont and compute them from a list of bigram types.
  3. Write the interpolated Kneser-Ney bigram formula and say what each of its two terms does.
  4. Derive lambda(h) from the discounted mass and prove that P_KN(. | h) sums to 1.
  5. 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:

PAD(w∣h)=max⁡(C(hw)−d, 0)C(h)+λ(h) P(w)\begin{aligned} P_{\text{AD}}(w \mid h) &= \frac{\max(C(hw) - d,\, 0)}{C(h)} \\ &\quad + \lambda(h)\, P(w) \end{aligned}
Absolute discounting interpolated with a raw unigram (JM Appendix C, Eq. C.1)

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.)

Frequency backoff asks how common w is; Kneser-Ney asks how many contexts lead to w

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 distributionQuestion it answersKongglasses
Raw unigram P(w)How likely is w?30 / 34 ≈ 0.8824 / 34 ≈ 0.118
Continuation P_cont(w)How likely is w as a novel continuation?1 / 5 = 0.24 / 5 = 0.8
Two lower-order distributions in the toy corpus

Recall

Why does an absolute-discounting model that backs off to the raw unigram rank Kong above glasses after "reading"?

The unigram counts tokens. Kong has 30 tokens, all from "Hong Kong", so its P(w) = 30 / 34 is large even though it has only ever followed one word. The backoff term then gives Kong about 0.662 and glasses about 0.338.

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:

N1+(∙ w)=∣{ v:C(v w)>0 }∣N_{1+}(\bullet\, w) = \bigl|\{\, v : C(v\,w) > 0 \,\}\bigr|
Continuation count: how many distinct words have been seen before w

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.

Pcont(w)=∣{ v:C(v w)>0 }∣∣{ (u,w′):C(u w′)>0 }∣=N1+(∙ w)∑w′N1+(∙ w′)\begin{aligned} P_{\text{cont}}(w) &= \frac{\bigl|\{\, v : C(v\,w) > 0 \,\}\bigr|}{\bigl|\{\, (u, w') : C(u\,w') > 0 \,\}\bigr|} \\ &= \frac{N_{1+}(\bullet\, w)}{\sum_{w'} N_{1+}(\bullet\, w')} \end{aligned}
Continuation probability (JM Appendix C, Eq. C.4 to C.6)
WordTokens as second wordDistinct preceding wordsContinuation countP_cont
Kong30Hong11 / 5 = 0.2
glasses4reading, sun, safety, my44 / 5 = 0.8
Hong0none00 / 5 = 0
Token counts against continuation counts in the toy corpus (5 bigram types)
Kong: 30 tokens but 1 context. glasses: 4 tokens and 4 contexts

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.

The continuation count is |{v : C(vw) > 0}|, the number of distinct left neighbours. P_cont divides it by the number of bigram types. Kong: 1, so 1 / 5 = 0.2. glasses: 4, so 4 / 5 = 0.8.

Quick check

Why does Kneser-Ney give glasses a higher continuation probability than Kong?

The interpolated Kneser-Ney bigram formula

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:

PKN(wi∣wi−1)=max⁡(C(wi−1wi)−d, 0)∑vC(wi−1v)⏟discounted bigram+λ(wi−1) Pcont(wi)⏟weighted continuation\begin{aligned} &P_{\text{KN}}(w_i \mid w_{i-1}) \\ &\quad = \underbrace{\frac{\max\bigl(C(w_{i-1} w_i) - d,\, 0\bigr)}{\sum_{v} C(w_{i-1} v)}}_{\text{discounted bigram}} \\ &\quad + \underbrace{\lambda(w_{i-1})\, P_{\text{cont}}(w_i)}_{\text{weighted continuation}} \end{aligned}
Interpolated Kneser-Ney for bigrams (JM Appendix C, Eq. C.7). First term: the bigram's own evidence minus a flat discount. Second term: the freed mass shared by continuation probability.

The denominator is the count of wi−1w_{i-1} as a history, ∑vC(wi−1v)\sum_{v} C(w_{i-1} v), 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 C(wi−1wi)=0C(w_{i-1} w_i) = 0 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.

λ(wi−1)=d∑vC(wi−1v)⋅∣{ w:C(wi−1w)>0 }∣\begin{aligned} \lambda(w_{i-1}) &= \frac{d}{\sum_{v} C(w_{i-1} v)} \\ &\quad \cdot \bigl|\{\, w : C(w_{i-1} w) > 0 \,\}\bigr| \end{aligned}
Normalized discount times the number of word types that follow the history, which is the number of types that were discounted (JM Eq. C.8)
N1+(h ∙)=∣{ w:C(h w)>0 }∣N_{1+}(h\,\bullet) = \bigl|\{\, w : C(h\,w) > 0 \,\}\bigr|
Number of distinct word types that follow h (the mirror image of N1+(• w))

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

  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).
  2. Recognize lambda

    That equals 1 − d · N1+(h •) / C(h) = 1 − λ(h).
  3. Sum the continuation terms

    P_cont is a distribution, so Σ_w λ(h) P_cont(w) = λ(h) · 1 = λ(h).
  4. 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.

The discounted terms sum to (C(h) − d · N1+(h •)) / C(h) = 1 − λ(h), because each of the N1+(h •) seen types loses exactly d. The continuation terms sum to λ(h) · Σ P_cont = λ(h). The total is 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?

λ(h) halves, since it is d · N1+(h •) / C(h). More evidence for the history means its own bigram counts are more trustworthy, so the model backs off less.

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

  1. 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.
  2. 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.
  3. 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.
  4. Step 4: Kong after reading

    C(reading Kong) = 0, so P_KN(Kong | reading) = 0 + 0.75 × 0.2 = 0.15.
  5. Result

    0.85 + 0.15 = 1. Every other word has P_cont = 0 in this corpus, so nothing else gets mass after "reading".
The 0.75 shaved from reading is poured out by P_cont: 0.6 to glasses, 0.15 to Kong

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.

HistoryWordFrequency backoffKneser-Ney
readingKong0.6620.15
readingglasses0.3380.85
HongKong0.9970.98
Hongglasses0.0030.02
Frequency-unigram backoff against Kneser-Ney, toy corpus, d = 0.75 (frequency backoff uses P(Kong) = 30/34, P(glasses) = 4/34)
λ shrinks as the evidence for a history grows: 0.025 for Hong, 0.75 for reading

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.

λ(Hong) = 0.75 / 30 = 0.025. Kong: 29.25 / 30 + 0.025 × 0.2 = 0.98. glasses: 0 + 0.025 × 0.8 = 0.02. They sum to 1.

Recall

In the toy corpus, what is P_cont(Hong), and why is that a problem for a real model?

It is 0, because Hong never appears as a second word. So P_KN(Hong | reading) = 0, a new zero. Full Kneser-Ney interpolates the lowest order with a uniform 1 / V (JM Eq. C.11), covered in Part 09.

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