Kneser–Ney¶
KneserNey, ModifiedKneserNey · n-gram smoothing · bigram input
Kneser–Ney smoothing (Kneser & Ney, 1995) is the standard classical method for n-gram language models. It discounts every observed bigram by a fixed amount, and gives the freed mass to a lower-order model in which a word's weight is the number of distinct contexts it follows, not its frequency. A word that is frequent but appears in only one context ("Francisco", nearly always after "San") is then unlikely in a new context. Modified Kneser–Ney (Chen & Goodman, 1999) uses three discounts instead of one.
Definition¶
For a bigram \((v, w)\) with count \(c(v, w)\), context total \(c(v)\) and discount \(d\),
where \(N_{1+}(v)\) is the number of distinct words seen after \(v\), and the continuation probability is
An unseen context backs off to \(P_{\text{cont}}(w)\) alone. ModifiedKneserNey
uses discounts \(d_1\), \(d_2\) and \(d_{3+}\) for bigrams seen once, twice and three
or more times, estimated from the counts of counts.
Example¶
With the example bigrams:
>>> import freqprob
>>> bigrams = {
... ("the", "cat"): 5,
... ("the", "dog"): 3,
... ("a", "cat"): 2,
... ("a", "dog"): 1,
... ("big", "cat"): 1,
... ("small", "dog"): 1,
... }
>>> kn = freqprob.KneserNey(bigrams, discount=0.75, logprob=False)
>>> [round(kn(b), 3) for b in [("the", "cat"), ("the", "dog"), ("big", "dog"), ("new", "cat")]]
[0.625, 0.375, 0.375, 0.5]
>>> mkn = freqprob.ModifiedKneserNey(bigrams, logprob=False)
>>> [round(mkn(b), 3) for b in [("the", "cat"), ("a", "cat"), ("big", "dog")]]
[0.625, 0.558, 0.3]
"cat" and "dog" each follow three distinct contexts, so their continuation probabilities are equal (0.5), and a new context gives them the same probability although "cat" is more frequent. "big" was followed only by "cat", so "dog" after "big" gets the context's whole back-off weight, \(0.75 \times 0.5 = 0.375\).
Properties¶
- The strongest classical n-gram smoother, especially in its modified form (Chen & Goodman, 1999).
- Normalized: the probabilities of the words after each context sum to one.
- Bigrams only. Keys must be
(context, word)pairs; other keys are ignored. A word never seen in any bigram scores 0 (the floor, in log space). - The discount is typically between 0.5 and 0.8; 0.75 is a robust default.
ModifiedKneserNeyestimates its discounts and needs enough variety in the counts to do so.
API¶
freqprob.KneserNey ¶
Bases: BigramBackoff
Kneser-Ney smoothing probability distribution.
One of the most effective smoothing methods for language modeling. It
combines absolute discounting with interpolation and, crucially, weights a
word by the diversity of contexts it appears in (its continuation
probability) rather than its raw frequency. Each observed bigram scores
max(c - d, 0) / c(context) + backoff(context) * P_cont(word), and an
unseen bigram backs off to backoff(context) * P_cont(word).
This is absolute discounting with the unigram model replaced by the
continuation distribution, and it shares the back-off machinery with
freqprob.AbsoluteDiscounting accordingly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
freqdist
|
FrequencyDistribution
|
Frequency distribution mapping bigrams to their observed
counts, in the form |
required |
discount
|
float
|
Absolute discounting parameter |
0.75
|
logprob
|
bool
|
Return log-probabilities if |
True
|
Examples:
Observed bigrams are discounted and interpolated with the continuation model, while unseen bigrams back off to it through the context's back-off weight:
>>> bigram_counts = {
... ("the", "cat"): 5, ("the", "dog"): 3, ("a", "cat"): 2,
... ("a", "dog"): 1, ("big", "cat"): 1, ("small", "dog"): 1,
... }
>>> kn = KneserNey(bigram_counts, discount=0.75, logprob=False)
>>> round(kn(("the", "cat")), 3)
0.625
An unseen bigram whose word is known scores through the context's
back-off weight rather than a flat average -- here 0.75 * P_cont(dog),
since big was only ever followed by one word:
An unseen context falls back to the continuation probability alone:
Note
This implementation assumes bigram input. The discount d is
typically between 0.5 and 0.8; 0.75 is a robust default.
Reliable continuation probabilities require sufficient bigram data.
A word that never appears in the data has no continuation probability,
so it scores at the floor rather than borrowing an average.
Initialize Kneser-Ney smoothing.
freqprob.ModifiedKneserNey ¶
Bases: BigramBackoff
Modified Kneser-Ney smoothing probability distribution.
An enhanced Kneser-Ney variant that applies a different discount depending
on a bigram's count: d1 for singletons, d2 for doubletons, and
d3 for counts of three or more. Those discounts are estimated
automatically from the data's count-of-counts statistics, which usually
makes it more accurate than the fixed-discount version.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
freqdist
|
FrequencyDistribution
|
Frequency distribution mapping bigrams to their observed
counts, in the form |
required |
logprob
|
bool
|
Return log-probabilities if |
True
|
Examples:
The discount adapts to each bigram's count, and the score interpolates the discounted estimate with the continuation model:
>>> bigram_counts = {
... ("the", "cat"): 5, ("the", "dog"): 3, ("a", "cat"): 2,
... ("a", "dog"): 1, ("big", "cat"): 1, ("small", "dog"): 1,
... }
>>> mkn = ModifiedKneserNey(bigram_counts, logprob=False)
>>> round(mkn(("the", "cat")), 3)
0.625
>>> round(mkn(("a", "cat")), 3)
0.558
Note
Modified Kneser-Ney is a standard, state-of-the-art classical smoothing method for n-gram language models. Because it estimates its discounts from the data, it is more robust than fixed-discount methods but needs enough varied counts to estimate them reliably.
Initialize Modified Kneser-Ney smoothing.
References¶
- Kneser & Ney (1995); Chen & Goodman (1999). See References.