Majid Al-RaimiRecursive and modified Kneser-Ney

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

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

  1. Write the recursive interpolated Kneser-Ney equation and the backoff weight that keeps every level normalized.
  2. Say which count c_KN uses at the highest order and at each lower order, and compute continuation counts from a corpus.
  3. Carry a trigram probability down to the uniform floor and give the probability of <UNK>.
  4. Compute the modified Kneser-Ney discounts D1, D2 and D3+ from counts of counts and explain why there are three.
  5. 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.

PKN(wi∣wi−n+1:i−1)=max⁡(cKN(wi−n+1:i)−d, 0)∑vcKN(wi−n+1:i−1 v)+λ(wi−n+1:i−1) PKN(wi∣wi−n+2:i−1)\begin{aligned} &P_{KN}(w_i \mid w_{i-n+1:i-1}) \\ &\quad = \frac{\max\bigl(c_{KN}(w_{i-n+1:i}) - d,\, 0\bigr)}{\sum_v c_{KN}(w_{i-n+1:i-1}\, v)} \\ &\quad + \lambda(w_{i-n+1:i-1})\, P_{KN}(w_i \mid w_{i-n+2:i-1}) \end{aligned}
Recursive interpolated Kneser-Ney (JM Appendix C, Eq. C.9). The last factor drops the oldest word of the history.

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:

λ(h)=d∑vcKN(h v)×∣{ w:cKN(h w)>0 }∣\begin{aligned} \lambda(h) &= \frac{d}{\sum_v c_{KN}(h\, v)} \\ &\quad \times \bigl|\{\, w : c_{KN}(h\, w) > 0 \,\}\bigr| \end{aligned}
The general backoff weight: the normalized discount times the number of distinct continuations of h (JM Eq. C.8 generalized; SRILM writes it bow(a_) = D n(a_*) / c(a_)).

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.

Each level keeps its discounted evidence and passes λ(h) of the mass to the next shorter history, down to 1/V
AspectInterpolated KNBackoff KN
When the lower order is usedAlways, for every word, seen or unseenOnly 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 probabilityDiscounted count plus a share of the lower orderDiscounted count only
SRILM flags-kndiscount -interpolate-kndiscount
Interpolated against backoff Kneser-Ney

Recall

Write the recursive interpolated KN equation and give λ(h).

P_KN(w | h) = max(c_KN(hw) − d, 0) / Σ_v c_KN(hv) + λ(h) P_KN(w | h′), where h′ drops the oldest word of h, and λ(h) = d / Σ_v c_KN(hv) × |{w : c_KN(hw) > 0}|, which is exactly the discounted mass.

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.

cKN(⋅)={count(⋅)for the highest ordercontinuationcount(⋅)for lower ordersc_{KN}(\cdot) = \begin{cases} \text{count}(\cdot) & \text{for the highest order} \\ \text{continuationcount}(\cdot) & \text{for lower orders} \end{cases}
The KN count (JM Appendix C, Eq. C.10)

The Continuation count is the number of unique single-word contexts, which for a bigram reads:

cKN(wi−1wi)=∣{ v:C(v wi−1wi)>0 }∣c_{KN}(w_{i-1} w_i) = \bigl|\{\, v : C(v\, w_{i-1} w_i) > 0 \,\}\bigr|
Continuation count of a bigram: how many distinct words have been seen right before it

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-gramLevelRaw countc_KNDistinct left neighbours
reading glassesbigram32her, his
glassesunigram42reading, sun
hongunigram32visited, loves
kongunigram31hong
sheunigram40none (always first)
Raw count against c_KN in the toy corpus, at the level where each n-gram is used as a lower order
Three tokens each, but reading glasses has 2 left contexts and kong only 1

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?

At a lower order you count distinct left contexts, here {her, his}. The two "her reading glasses" tokens count once.

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:

PKN(w)=max⁡(cKN(w)−d, 0)∑w′cKN(w′)+λ(ϵ) 1V\begin{aligned} P_{KN}(w) &= \frac{\max\bigl(c_{KN}(w) - d,\, 0\bigr)}{\sum_{w'} c_{KN}(w')} \\ &\quad + \lambda(\epsilon)\, \frac{1}{V} \end{aligned}
Unigram termination (JM Appendix C, Eq. C.11). ε is the empty history, and λ(ε) = d × (words with c_KN > 0) / Σ c_KN.

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

  1. 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.
  2. 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.
  3. 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.
  4. 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).
SimulatorRecursive Kneser-Ney on the reading glasses corpus

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.

  1. trigramraw countmax(2 − d, 0) / 2 = 0.625; λ(her reading) = d × 1 / 2 = 0.375P(glasses) = 0.8770
  2. bigramcontinuation countmax(2 − d, 0) / 2 = 0.625; λ(reading) = d × 1 / 2 = 0.375P(glasses) = 0.6721
  3. unigramcontinuation countmax(2 − d, 0) / 15 = 0.083; λ(ε) = d × 11 / 15 = 0.55P(glasses) = 0.1256
  4. uniform1 / V = 1 / 130.0769
Top probability0.8770P_KN(glasses | her reading)
Uniform floor0.0423λ(ε) / V: every vocabulary word gets at least this at the unigram level. <UNK> gets it only if it is counted in V
Top-level weight0.375λ(her reading): mass the top level passes down

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?

λ(ε)/V, because the unigram level is interpolated with a uniform distribution and <UNK> has count 0, so only the uniform share reaches it.

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.

Church and Gale: count-1 bigrams shrink by 0.55, counts 2 to 4 by about 0.75

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:

Y=n1n1+2n2D1=1−2Yn2n1D2=2−3Yn3n2D3+=3−4Yn4n3\begin{gathered} Y = \frac{n_1}{n_1 + 2 n_2} \qquad D_1 = 1 - 2Y\frac{n_2}{n_1} \\ D_2 = 2 - 3Y\frac{n_3}{n_2} \qquad D_{3+} = 3 - 4Y\frac{n_4}{n_3} \end{gathered}
Modified KN discounts from counts of counts, computed separately for each order (SRILM ngram-discount)
Dn(k)=k−(k+1) tn,1 tn,k+1(tn,1+2tn,2) tn,kD_n(k) = k - \frac{(k+1)\, t_{n,1}\, t_{n,k+1}}{(t_{n,1} + 2 t_{n,2})\, t_{n,k}}
The same formula as Heafield et al. write it, with t(n, k) the number of order-n n-grams with adjusted count k

Worked example

Discounts from counts of counts

  1. Counts of counts

    Suppose an order has n1 = 1000, n2 = 300, n3 = 150 and n4 = 90.
  2. Y

    Y = 1000 / (1000 + 600) = 0.625.
  3. 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.
  4. 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:

γ(h)=D1 N1(h∙)+D2 N2(h∙)+D3+ N3+(h∙)∑wc(h w)\gamma(h) = \frac{\begin{gathered} D_1\, N_1(h\bullet) + D_2\, N_2(h\bullet) \\ + D_{3+}\, N_{3+}(h\bullet) \end{gathered}}{\sum_w c(h\, w)}
Backoff weight of modified KN (Heafield et al. call it b)

Worked example

Reserved mass for one history

  1. The history

    A history h is followed by four word types with counts 1, 1, 2 and 5, so Σ c(hw) = 9.
  2. 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.
  3. Discounted terms

    Count 5: 3.5 / 9 = 0.3889. Count 2: 0.9375 / 9 = 0.1042. Each count 1: 0.375 / 9 = 0.0417.
  4. 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.
AspectKneser-NeyModified Kneser-Ney
Discounts per order1 (d)3 (D1, D2, D3+)
How the discount is estimatedd = n1 / (n1 + 2 n2)Dk = k − (k+1) Y n(k+1) / n(k)
Reserved mass for history hd · N1+(h•) / Σ c(hw)(D1 N1 + D2 N2 + D3+ N3+) / Σ c(hw)
SRILM flag-ukndiscount-kndiscount
KenLMNot offeredlmplz (default)
Kneser-Ney against modified Kneser-Ney

Recall

Give the modified KN discount formulas.

Y = n1 / (n1 + 2 n2), D1 = 1 − 2Y n2/n1, D2 = 2 − 3Y n3/n2, D3+ = 3 − 4Y n4/n3, computed separately for each order.

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.arpa
KenLM and SRILM building the same kind of 5-gram model

The 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 count table needs an exact row; an embedding space puts cat near dog and bedroom near room

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.

Aspectn-gram KNNeural LM
ParametersUp to V^n counts, exponential in nGrow linearly with V and n
GeneralizationOnly through exact matches of the contextThrough similar vectors for similar words and contexts
Context lengthFixed and short (3 to 5 words in practice)Longer windows; later models use whole documents
ComputeCounting and lookup, very cheapTraining by gradient descent, far more expensive
Kneser-Ney n-gram models against neural language models

Recall

Why can no smoothing method, however good, let evidence about cat help predict dog?

Smoothing only moves probability mass among atomic symbols; it has no notion that two words are similar. Only a representation that places cat and dog near each other, as a neural LM's word vectors do, lets evidence transfer between them.

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