Download as pdf or txt
Download as pdf or txt
You are on page 1of 93

Unit-4: N-Grams & Language

Modeling
IS 7118: Natural Language Processing t

1st Year, 2nd Semester, M.Sc(IS)


(Slides are adapted from Text Book by Jurafsky & Martin )
Unit-4 Contents
• Word Counting in Corpora
• Simple (unsmoothed) N-Grams
• Training and Test Sets
• Evaluating N-Grams: Perplexity
• Smoothing
– Laplace Smoothing
– Good-Turing Smoothing
– Written-Bell Discounting
– Kenser-Ney Smoothing
• Interpolation
• Backoff ( Katz Backoff _
• Advanced Issues in Language Modeling
IS 7118: NLP Unit-4: N-Grams, 2
Prof. R.K.Rao Bandaru
Probabilistic Language Models

• Goal: assign a probability to a sentence


▪ Machine Translation:
▪ P(high winds tonite) > P(large winds tonite)
▪ Spell Correction
▪ The office is about fifteen minuets from my house
Why? ▪ P(about fifteen minutes from) > P(about fifteen minuets from)
▪ Speech Recognition
▪ P(I saw a van) >> P(eyes awe of an)
▪ + Summarization, question-answering, etc., etc.!!

IS 7118: NLP Unit-4: N-Grams, 3


Prof. R.K.Rao Bandaru
Counting
• Simple counting lies at the core of any
probabilistic approach. So let’s first take a look
at what we’re counting.
– He stepped out into the hall, was delighted to
encounter a water brother.
• 13 tokens, 15 if we include “,” and “.” as separate
tokens.
• Assuming we include the comma and period as tokens,
how many bigrams are there?

IS 7118: NLP Unit-4: N-Grams, 4


Prof. R.K.Rao Bandaru
Counting
• Not always that simple
– I do uh main- mainly business data processing
• Spoken language poses various challenges:disfluencies
– Should we count “uh” and other fillers as tokens?
– What about the repetition of fragments“mainly”? Should such
do-overs count twice or just once?
– The answers depend on the application.
• If we’re focusing on something like ASR to support indexing for
search, then “uh” isn’t helpful (it’s not likely to occur as a query).
• But filled pauses are very useful in dialog management, so we might
want them there
• Tokenization of text raises the same kinds of issues
IS 7118: NLP Unit-4: N-Grams, 5
Prof. R.K.Rao Bandaru
Counting: Types and Tokens
• How about
– They picnicked by the pool, then lay back on the
grass and looked at the stars.
• 18 tokens (again counting punctuation)
• But we might also note that “the” is used 3 times, so
there are only 16 unique types (as opposed to tokens).
• Wordform is the full inflected or derived form of the
word.
• In going forward, we’ll have occasion to focus on
counting both types and tokens of both words and N-
grams. IS 7118: NLP Unit-4: N-Grams, 6
Prof. R.K.Rao Bandaru
Counting: Corpora
• What happens when we look at large bodies of
text instead of single utterances

• Google Web Crawl


– Crawl of 1,024,908,267,229 English tokens in Web text
– 13,588,391 wordform types
• That seems like a lot of types... After all, even large dictionaries of English
have only around 500k types. Why so many here?

•Numbers
•Misspellings
•Names
•Acronyms
•etc

IS 7118: NLP Unit-4: N-Grams, Prof. 7


R.K.Rao Bandaru
Probabilistic Language Modeling

• Goal: compute the probability of a sentence or


sequence of words:
P(W) = P(w1,w2,w3,w4,w5…wn)
• Related task: probability of an upcoming word:
P(w5|w1,w2,w3,w4)
• A model that computes either of these:
P(W) or P(wn|w1,w2…wn-1) is called a language model.
• Better: the grammar But language model or LM is standard

IS 7118: NLP Unit-4: N-Grams, 8


Prof. R.K.Rao Bandaru
How to compute P(W)
• How to compute this joint probability:

– P(its, water, is, so, transparent, that)

• Intuition: let’s rely on the Chain Rule of Probability

IS 7118: NLP Unit-4: N-Grams, 9


Prof. R.K.Rao Bandaru
Reminder: The Chain Rule
• Recall the definition of conditional probabilities
P( A, B)
P( A | B) =
P( B)
Re writing : P( A | B) P( B) = P( A, B)
(or) P( B | A) P( A) = P( A, B)

• More variables:
P(A,B,C,D) = P(A)P(B|A)P(C|A,B)P(D|A,B,C)
• The Chain Rule in General
P(x1,x2,x3,…,xn) = P(x1)P(x2|x1)P(x3|x1,x2)…P(xn|x1,…,xn-1)

IS 7118: NLP Unit-4: N-Grams, 10


Prof. R.K.Rao Bandaru
The Chain Rule applied to compute joint
probability of words in sentence

P( w1w2  wn ) =  P( wi | w1w2  wi −1 )
i

P(“its water is so transparent”) =


P(its) × P(water|its) × P(is|its water)
× P(so|its water is) × P(transparent|its water is
so)

IS 7118: NLP Unit-4: N-Grams, 11


Prof. R.K.Rao Bandaru
How to estimate these probabilities
• Could we just count and divide?
P(the |its water is so transparent that) =
Count(its water is so transparent that the)
Count(its water is so transparent that)

• No! Too many possible sentences!


• We’ll never see enough data for estimating these

IS 7118: NLP Unit-4: N-Grams, 12


Prof. R.K.Rao Bandaru
Markov Assumption

• Simplifying assumption:
Andrei Markov

P(the | its water is so transparent that) » P(the | that)

• Or maybe
P(the |its water is so transparent that) » P(the |transparent that)

IS 7118: NLP Unit-4: N-Grams, 13


Prof. R.K.Rao Bandaru
Markov Assumption

P( w1w2  wn )   P( wi | wi −k  wi −1 )
i
• In other words, we approximate each
component in the product

P(wi | w1w2  wi −1 )  P(wi | wi −k  wi −1 )

IS 7118: NLP Unit-4: N-Grams, 14


Prof. R.K.Rao Bandaru
Simplest case: Unigram model
P( w1w2  wn )   P( wi )
i
Some automatically generated sentences from a unigram model
fifth, an, of, futures, the, an,
incorporated, a, a, the, inflation,
most, dollars, quarter, in, is, mass

thrift, did, eighty, said, hard, 'm,


july, bullish

that, or, limited, the


IS 7118: NLP Unit-4: N-Grams, 15
Prof. R.K.Rao Bandaru
Bigram model

Condition on the previous word:

P(wi | w1w2  wi −1 )  P(wi | wi −1 )


texaco, rose, one, in, this, issue, is,
pursuing, growth, in, a, boiler, house, said,
mr., gurria, mexico, 's, motion, control,
proposal, without, permission, from, five,
hundred, fifty, five, yen
outside, new, car, parking, lot, of, the,
agreement, reached

this, would, be, a,IS 7118:


record, november
NLP Unit-4: N-Grams,
Prof. R.K.Rao Bandaru
16
N-gram models

• We can extend to trigrams, quadrigrams, 5-grams


• In general this is an insufficient model of language
– because language has long-distance dependencies:
“The computer which I had just put into the machine room on
the fifth floor crashed.”

• But we can often get away with N-gram models

IS 7118: NLP Unit-4: N-Grams, 17


Prof. R.K.Rao Bandaru
Language Modeling

Introduction to N-grams
Language Modeling

Estimating N-gram Probabilities


Estimating bigram probabilities
• The Maximum Likelihood Estimate

count(wi-1,wi )
P(wi | wi-1) =
count(wi-1)
c(wi-1,wi )
P(wi | wi-1) =
c(wi-1)
• This estimate is called Maximum Likelihood Estimate (MLE) because it is the choice of parameters that gives the highest probability to the training
corpus

IS 7118: NLP Unit-4: N-Grams, 20


Prof. R.K.Rao Bandaru
An example
c( wi −1 , wi )
P( wi | wi −1 ) = MLE
c( wi −1 )
<s> I am Sam </s>
<s> Sam I am </s>
<s> I do not like green eggs and ham </s>

C(<s>,I)/C(<s>) =2/3

IS 7118: NLP Unit-4: N-Grams, 21


Prof. R.K.Rao Bandaru
More examples:
Berkeley Restaurant Project sentences
• can you tell me about any good cantonese restaurants
close by
• mid priced thai food is what i’m looking for
• tell me about chez panisse
• can you give me a listing of the kinds of food that are
available
• i’m looking for a good place to eat breakfast
• when is caffe venezia open during the day

IS 7118: NLP Unit-4: N-Grams, 22


Prof. R.K.Rao Bandaru
Raw bigram counts
• Bigram counts for eight of words(out of V=1446) in the Berkeley
Restaurant Project corpus of 9332 sentences.

IS 7118: NLP Unit-4: N-Grams, 23


Prof. R.K.Rao Bandaru
Raw bigram probabilities
c(wi-1,wi )
• Normalize by unigrams: P(wi | wi-1) =
c(wi-1)

• Result:

IS 7118: NLP Unit-4: N-Grams, 24


Prof. R.K.Rao Bandaru
Bigram estimates of sentence
probabilities

P(<s> I want english food </s>) =


P(I|<s>)
× P(want|I)
× P(english|want)
× P(food|english)
× P(</s>|food)
= 0.25 × 0.33 × 0.0011 × 0.5 × 0.68 = .000031
IS 7118: NLP Unit-4: N-Grams, 25
Prof. R.K.Rao Bandaru
What kinds of knowledge?
• P(english|want) = .0011
World knowledge
• P(chinese|want) = .0065
• P(to|want) = .66 Grammatical need
• P(eat | to) = .28
to food occurrence is very low,.
• P(food | to) = 0
• P(want | spend) = 0 Two verbs, grammar disallows
• P (i | <s>) = .25 discourse

IS 7118: NLP Unit-4: N-Grams, 26


Prof. R.K.Rao Bandaru
Practical Issues
• We do everything in log space
– Avoid underflow
– (also adding is faster than multiplying)

log(p1 ´ p2 ´ p3 ´ p4 ) = log p1 +log p2 +log p3 +log p4

IS 7118: NLP Unit-4: N-Grams, 27


Prof. R.K.Rao Bandaru
Language Modeling Toolkits
• SRILM
– http://www.speech.sri.com/projects/srilm/

IS 7118: NLP Unit-4: N-Grams, 28


Prof. R.K.Rao Bandaru
Google N-Gram Release, August 2006

IS 7118: NLP Unit-4: N-Grams, 29


Prof. R.K.Rao Bandaru
Google N-Gram Release
• serve as the incoming 92
• serve as the incubator 99
• serve as the independent 794
• serve as the index 223
• serve as the indication 72
• serve as the indicator 120
• serve as the indicators 45
• serve as the indispensable 111
• serve as the indispensible 40
• serve as the individual 234
http://googleresearch.blogspot.com/2006/08/all-our-n-gram-are-belong-to-you.html

IS 7118: NLP Unit-4: N-Grams, 30


Prof. R.K.Rao Bandaru
Google Book N-grams
• http://ngrams.googlelabs.com/

IS 7118: NLP Unit-4: N-Grams, 31


Prof. R.K.Rao Bandaru
Language Modeling

Estimating N-gram Probabilities


Language Modeling

Evaluation and Perplexity


Evaluation: How good is our model?
• Does our language model prefer good sentences to bad
ones?
– Assign higher probability to “real” or “frequently observed”
sentences
• Than “ungrammatical” or “rarely observed” sentences?
• We train parameters of our model on a training set.
• We test the model’s performance on data we haven’t
seen.
– A test set is an unseen dataset that is different from
our training set, totally unused.
– An evaluation metric tells us how well our model
does on the test set.
IS 7118: NLP Unit-4: N-Grams, 34
Prof. R.K.Rao Bandaru
Extrinsic evaluation of N-gram models
• Best evaluation for comparing models A and B
– Put each model in a task
• spelling corrector, speech recognizer, MT system
– Run the task, get an accuracy for A and for B
• How many misspelled words corrected properly
• How many words translated correctly
– Compare accuracy for A and B
• It’s called extrinsic evaluation because it is
external to the n-gram model itself.
IS 7118: NLP Unit-4: N-Grams, 35
Prof. R.K.Rao Bandaru
Difficulty of extrinsic (in-vivo)
evaluation of N-gram models
• Extrinsic evaluation
– Time-consuming; can take days or weeks
• So
– Sometimes use intrinsic evaluation, i.e., something
about intrinsically about language model themselves
rather than any particular application.
– the most common intrinsic evaluation is Perplexity.
– Bad approximation to extrinsic evaluation
• unless the test data looks just like the training data
• So generally only useful in pilot experiments
– But is helpful to think about.
IS 7118: NLP Unit-4: N-Grams, 36
Prof. R.K.Rao Bandaru
Intuition of Perplexity
• The Claude Shannon Game:
– How well can we predict the next word? mushrooms 0.1
pepperoni 0.1

I always order pizza with cheese and ____ anchovies 0.01


….
The 33rd President of the US was ____
fried rice 0.0001
I saw a ____ ….
and 1e-100

– Unigrams are terrible at this game. (Why?)


– Because a good language model will attempt to look at the
context to narrow down the body of probable words.
• A better model of a text: is one which assigns a higher
probability to the word that actually occurs
IS 7118: NLP Unit-4: N-Grams, 37
Prof. R.K.Rao Bandaru
Perplexity

❑The best language model is one that best predicts an unseen test set
• Gives the highest P(sentence)
1
-
Perplexity (PP) of a language model on PP(W ) = P(w1w2 ...wN ) N
a test set is the is a function of the
probability that the language model
assigns to that test set. For a test set 1
= N
W=w1w2….wN, the perplexity is the P(w1w2...wN )
probability of the test set normalized
by the number of words.
Chain rule:

For bigrams:
Because of inverse , minimizing perplexity is the same as 38
maximizing probability IS 7118: NLP Unit-4: N-Grams,
Prof. R.K.Rao Bandaru
IS 7118: NLP Unit-4: N-Grams,
Prof. R.K.Rao Bandaru
The Shannon Game intuition for perplexity
• From Josh Goodman:
• How hard is the task of recognizing digits ‘0,1,2,3,4,5,6,7,8,9’
– Perplexity 10 perplexity is related to the average branching factor
• How hard is recognizing (30,000) names? Speech recognition for
automated (switch board phone)operator to connect to an
employee.
– Perplexity = 30,000
• If a system has to recognize
– Operator (1 in 4)
– Sales (1 in 4)
– Technical Support (1 in 4)
– 30,000 names (1 in 120,000 each)
– Perplexity is ≈53
39
• Perplexity is weighted equivalent branching factor
Perplexity as branching factor
• Let’s suppose a sentence consisting of random
digits
• What is the perplexity of this sentence according
to a model that assign P=1/10 to each digit?

IS 7118: NLP Unit-4: N-Grams, 40


Prof. R.K.Rao Bandaru
Lower perplexity = better model

• Training 38 million words, 19,979 words vocabulary,


test 1.5 million words, Wall Street Journal
N-gram Unigram Bigram Trigram
Order

Perplexity 962 170 109

Lower the perplexity, the better is the model.


IS 7118: NLP Unit-4: N-Grams, 41
Prof. R.K.Rao Bandaru
Language Modeling

Evaluation and Perplexity


Language Modeling

Generalization and zeros


The Shannon Visualization Method

• Choose a random bigram


(<s>, w) according to its <s> I
probability I want
• Now choose a random want to
bigram (w, x) according to eat
to its probability eat Chinese
• And so on until we choose Chinese
</s> food
• Then string the words
together food </s>
I want to eat Chinese food

IS 7118: NLP Unit-4: N-Grams, 44


Prof. R.K.Rao Bandaru
Approximating Shakespeare
Figure: Sentences randomly generated from four N-gram models computed from
Shakespeare’s works. All characters are mapped to lower case and punctuation marks were
treated as words. Output was hand-corrected for capitalization to improve readability

IS 7118: NLP Unit-4: N-Grams, 45


Prof. R.K.Rao Bandaru
Shakespeare as corpus

• N=884,647 tokens, V=29,066


• Shakespeare produced 300,000 bigram types
out of V2= 844 million possible bigrams.
– So 99.96% of the possible bigrams were never seen
(have zero entries in the table)
• Quadrigrams worse: What's coming out looks
like Shakespeare because it is Shakespeare

IS 7118: NLP Unit-4: N-Grams, 46


Prof. R.K.Rao Bandaru
The wall street journal is not Shakespeare (no
offense)
• Figure: Sentences randomly generated from three N-gram models computed from 40
million words of the Wall Street Journal, lower casing all characters, and treating
punctuations as words. Output was then hand-corrected for capitalization to improve
readability.

IS 7118: NLP Unit-4: N-Grams, 47


Prof. R.K.Rao Bandaru
The perils of overfitting
• N-grams only work well for word prediction if the test
corpus looks like the training corpus
– In real life, it often doesn’t
– We need to train robust models that generalize!
– One kind of generalization: Zeros!
• Things that don’t ever occur in the training set
– But occur in the test set

IS 7118: NLP Unit-4: N-Grams, 48


Prof. R.K.Rao Bandaru
Zeros

• Training set: • Test set


… denied the allegations … denied the offer
… denied the reports … denied the loan
… denied the claims
… denied the request Probability will be 0

P(“offer” | denied the) = 0

IS 7118: NLP Unit-4: N-Grams, 49


Prof. R.K.Rao Bandaru
Zero probability bigrams
• Bigrams with zero probability
– mean that we will assign 0 probability to the test
set!
• And hence we cannot compute perplexity
(can’t divide by 0)!

IS 7118: NLP Unit-4: N-Grams, 50


Prof. R.K.Rao Bandaru
Language Modeling

Generalization and zeros


Language Modeling

Smoothing: Add-one (Laplace)


smoothing
The intuition of smoothing (from Dan Klein)

• When we have sparse statistics:


P(w | denied the)
3 allegations

allegations
2 reports
1 claims

reports

outcome
1 request

attack
claims

request

man
7 total
• Steal probability mass to generalize better

P(w | denied the)


2.5 allegations
1.5 reports
0.5 claims
allegations
allegations

0.5 request

outcome
2 other

reports

attack
7 total

man
claims

IS 7118: NLP Unit-4: N-Grams, request 53


Prof. R.K.Rao Bandaru
Add-one estimation
• Also called Laplace smoothing
• Pretend we saw each word one more time than we did
• Just add one to all the counts!

c(wi-1,wi )
• MLE estimate: PMLE (wi | wi-1 ) =
c(wi-1 )
• Add-1 estimate:
c(wi-1,wi )+1
PAdd-1 (wi | wi-1 ) =
c(wi-1 )+V
IS 7118: NLP Unit-4: N-Grams, 54
Prof. R.K.Rao Bandaru
Maximum Likelihood Estimates
• The maximum likelihood estimate
– of some parameter of a model M from a training set T
– maximizes the likelihood of the training set T given the model M
• Suppose the word “bagel” occurs 400 times in a corpus of a
million words
• What is the probability that a random word from some other
text will be “bagel”?
• MLE estimate is 400/1,000,000 = .0004
• This may be a bad estimate for some other corpus
– But it is the estimate that makes it most likely that “bagel” will
occur 400 times in a million word corpus.
– Smoothing is a non-maximum likelihood estimator
IS 7118: NLP Unit-4: N-Grams, 55
Prof. R.K.Rao Bandaru
Berkeley Restaurant Corpus:
Laplace smoothed bigram counts
• Add-one smoothed bigram counts for eight of words(out of
V=1446) in the Berkeley Restaurant Project corpus of 9332
sentences.

IS 7118: NLP Unit-4: N-Grams, 56


Prof. R.K.Rao Bandaru
Laplace-smoothed bigrams
• Add-one smoothed bigram probabilities for eight of words(out of V=1446) in the
Berkeley Restaurant Project corpus of 9332 sentences.

IS 7118: NLP Unit-4: N-Grams, 57


Prof. R.K.Rao Bandaru
IS 7118: NLP Unit-4: N-Grams,
Prof. R.K.Rao Bandaru
Reconstituted counts
• Figure: Add-one reconstituted bigram counts for eight of words(out of V=1446) in
the Berkeley Restaurant Project corpus of 9332 sentences.

Take those probabilities and reestimate the original counts, to know how much of this
add -one smoothing changed these probabilities 58
Compare with raw bigram counts

IS 7118: NLP Unit-4: N-Grams, 59


Prof. R.K.Rao Bandaru
Big Change to the Counts!
• C(want to) went from 608 to 238!
• P(food|Chinese) from 82 to 8.2!
• Discount d= c*/c
– d for “chinese food” =.10!!! A 10x reduction
– So in general, Laplace is a blunt instrument
– Could use more fine-grained method (add-k)
• But Laplace smoothing not generally used for N-grams, as we have much
better methods
• Despite its flaws Laplace (add-k) is however still used to smooth other
probabilistic models in NLP, especially
– For pilot studies
– In document classification
– Information retrieval
– In domains where the number of zeros isn’t so huge.

IS 7118: NLP Unit-4: N-Grams, 60


Prof. R.K.Rao Bandaru
Language Modeling

Smoothing: Add-one (Laplace)


smoothing
Language Modeling

Interpolation, Backoff, and Web-


Scale LMs
Backoff and Interpolation
• Sometimes it helps to use less context
– Condition on less context for contexts you haven’t
learned much about
• Backoff:
– use trigram if you have good evidence,
– otherwise bigram, otherwise unigram
• Interpolation:
– mix unigram, bigram, trigram
• Interpolation works better
IS 7118: NLP Unit-4: N-Grams, 63
Prof. R.K.Rao Bandaru
Linear Interpolation

• Simple interpolation

• Lambdas conditional on context:

IS 7118: NLP Unit-4: N-Grams, 64


Prof. R.K.Rao Bandaru
How to set the lambdas?

• Use a held-out corpus devset


Held-Out Test
Training Data Data Data

• Choose λ’s to maximize the probability of held-


out data:
– Fix the N-gram probabilities (on the training data)
– Then search for λs that give largest probability to held-out set:

logP(w1...wn | M(l1...lk )) = ålogPM ( l1... lk ) (wi | wi-1 )


i
IS 7118: NLP Unit-4: N-Grams, 65
Prof. R.K.Rao Bandaru
Unknown words: Open versus closed
vocabulary tasks
• If we know all the words in advanced
– Vocabulary V is fixed
– Closed vocabulary task
• Often we don’t know this
– Out Of Vocabulary = OOV words
– Open vocabulary task
• Instead: create an unknown word token <UNK>
– Training of <UNK> probabilities
• Create a fixed lexicon L of size V
• At text normalization phase, any training word not in L changed to <UNK>
• Now we train its probabilities like a normal word
– At decoding time
• If text input: Use UNK probabilities for any word not in training

IS 7118: NLP Unit-4: N-Grams, 66


Prof. R.K.Rao Bandaru
Huge web-scale n-grams

• How to deal with, e.g., Google N-gram corpus


• Pruning
– Only store N-grams with count > threshold.
• Remove singletons of higher-order n-grams
– Entropy-based pruning
• Efficiency
– Efficient data structures like tries
– Bloom filters: approximate language models
– Store words as indexes, not strings
• Use Huffman coding to fit large numbers of words into two bytes
– Quantize probabilities (4-8 bits instead of 8-byte float)
IS 7118: NLP Unit-4: N-Grams, 67
Prof. R.K.Rao Bandaru
Smoothing for Web-scale N-grams
• “Stupid backoff” (Brants et al. 2007)
• No discounting, just use relative frequencies

ì i
ïï count(wi-k+1
i-1
)
if count(wi-k+1 ) > 0
i

S(wi | wi-k+1 ) = í count(wi-k+1 )


i-1

ï i-1
ïî 0.4S(w i | w i-k+2 ) otherwise

count(wi )
S(wi ) =
N IS 7118: NLP Unit-4: N-Grams,
Prof. R.K.Rao Bandaru
68
N-gram Smoothing Summary
• Add-1 smoothing:
– OK for text categorization, not for language modeling
• The most commonly used method:
– Extended Interpolated Kneser-Ney
• For very large N-grams like the Web:
– Stupid backoff

IS 7118: NLP Unit-4: N-Grams, 69


Prof. R.K.Rao Bandaru
Advanced Language Modeling
• Discriminative models:
– choose n-gram weights to improve a task, not to fit the
training set
• Parsing-based models
• Caching Models
– Recently used words are more likely to appear
c(w Î history)
PCACHE (w | history) = l P(wi | wi-2 wi-1 )+(1- l )
| history |

– These perform very poorly for speech recognition


(why?)
IS 7118: NLP Unit-4: N-Grams, 70
Prof. R.K.Rao Bandaru
Language Modeling

Interpolation, Backoff, and Web-


Scale LMs
Language Modeling

Advanced: Good Turing


Smoothing
Reminder: Add-1 (Laplace) Smoothing

c(wi-1,wi )+1
PAdd-1 (wi | wi-1 ) =
c(wi-1 )+V

IS 7118: NLP Unit-4: N-Grams, 73


Prof. R.K.Rao Bandaru
More general formulations: Add-k

c(wi-1,wi )+ k
PAdd-k (wi | wi-1 ) =
c(wi-1 )+ kV
Now, let m=kV
1
c(wi-1,wi )+ m( )
PAdd-k (wi | wi-1 ) = V
c(wi-1 )+ m
IS 7118: NLP Unit-4: N-Grams, 74
Prof. R.K.Rao Bandaru
Unigram prior smoothing

1
c(wi-1,wi )+ m( )
PAdd-k (wi | wi-1 ) = V
c(wi-1 )+ m

c(wi-1, wi )+ mP(wi )
PUnigramPrior (wi | wi-1 ) =
c(wi-1 )+ m
IS 7118: NLP Unit-4: N-Grams, 75
Prof. R.K.Rao Bandaru
Advanced smoothing algorithms
• Intuition used by many smoothing algorithms
– Good-Turing
– Kneser-Ney
– Witten-Bell

Use the count of things we’ve seen once to help


estimate the count of things we’ve never seen

IS 7118: NLP Unit-4: N-Grams, 76


Prof. R.K.Rao Bandaru
Advanced smoothing algorithms
• Notation: Nc = Frequency of frequency c
• Nc = the count of things we’ve seen c times
• Sam I am I am Sam I do not eat
I 3
sam 2
am 2 N1 = 3

do 1 N2 = 2
not 1
N3 = 1
eat 1
IS 7118: NLP Unit-4: N-Grams, 77
Prof. R.K.Rao Bandaru
Good-Turing smoothing intuition
• You are fishing (a scenario from Josh Goodman), and caught:
– 10 carp, 3 perch, 2 whitefish, 1 trout, 1 salmon, 1 eel = 18 fish
• How likely is it that next species is trout?
– 1/18
• How likely is it that next species is new (i.e. catfish or bass)
– Let’s use our estimate of things-we-saw-once to estimate the
new things.
– 3/18 (because N1=3)
• Assuming so, how likely is it that next species is trout?
– Must be less than 1/18 We need to discount the probability
to accommodate new things.
– How to estimate?
IS 7118: NLP Unit-4: N-Grams, 78
Prof. R.K.Rao Bandaru
Good Turing calculations

N1 (c +1)Nc+1
P (things with zero frequency) =
*
GT c* =
N Nc
• Unseen (bass or catfish) • Seen once (trout)
oc=1
o c = 0:
o MLE p = 1/18
o MLE p = 0/18 = 0
o C*(trout) = 2 * N2/N1
o P*GT (unseen) = 2 * 1/3
= 2/3
= N1/N
o P*GT(trout) =
= 3/18 =2/3 / 18 discount
IS 7118: NLP Unit-4: N-Grams, = 1/27 79
Prof. R.K.Rao Bandaru
Bigram Frequencies of Frequencies and
GT Re-estimates –Example 2

IS 7118: NLP Unit-4: N-Grams, 80


Prof. R.K.Rao Bandaru
Bigram Frequencies of Frequencies and
GT Re-estimates–Example 2

3*= 4 * (381/642)
= 4 * .593
= 2.37
81
IS 7118: NLP Unit-4: N-Grams,
Prof. R.K.Rao Bandaru
GT Smoothed Bigram Probabilities

IS 7118: NLP Unit-4: N-Grams, 82


Prof. R.K.Rao Bandaru
Ney et al.’s Good Turing Intuition
H. Ney, U. Essen, and R. Kneser, 1995. On the estimation of 'small' probabilities by leaving-one-
out. IEEE Trans. PAMI. 17:12,1202-1212

Held-out words:

IS 7118: NLP Unit-4: N-Grams, 83


Prof. R.K.Rao Bandaru
Training Held out
Ney et al. Good Turing Intuition
(slide from Dan Klein)
N1 N0
• Intuition from leave-one-out validation
– Take each of the c training words out in turn
– c training sets of size c–1, held-out of size 1
– What fraction of held-out words are unseen in
training? N2 N1
• N1/c
– What fraction of held-out words are seen k times
in training? N3 N2
• (k+1)Nk+1/c
– So in the future we expect (k+1)Nk+1/c of the
words to be those with training count k

....
....
– There are Nk words with training count k
– Each should occur with probability:
• (k+1)Nk+1/c/Nk
(k +1)N k+1
– …or expected count: k* =
Nk N3511 N3510
IS 7118: NLP Unit-4: N-Grams, 84
Prof. R.K.Rao Bandaru N4417 N4416
Good-Turing complications
(slide from Dan Klein)

• Problem: what about “the”? (say c=4417)


– For small k, Nk > Nk+1
N1
– For large k, too jumpy, zeros N2
N3
wreck estimates

– Simple Good-Turing [Gale


and Sampson]: replace
N1
empirical Nk with a best-fit
N2
power law once counts get
IS 7118: NLP Unit-4: N-Grams, 85
unreliable Prof. R.K.Rao Bandaru
Resulting Good-Turing numbers

• Numbers from Church and Gale (1991) Count Good Turing


• 22 million words of AP Newswire c c*
0 .0000270
(c +1)Nc+1 1 0.446
c* = 2 1.26
Nc
3 2.24
• It sure looks like c* = (c - .75) 4 3.24
5 4.22
6 5.19
7 6.21
8 7.24
IS 7118: NLP Unit-4: N-Grams, 9 8.25 86
Prof. R.K.Rao Bandaru
Absolute Discounting Interpolation

• Save ourselves some time and just subtract 0.75


(or some d)! discounted bigram Interpolation weight

c(wi-1,wi )- d
PAbsoluteDiscounting (wi | wi-1 ) = + l (wi-1 )P(w)
c(wi-1 ) unigram

– (Maybe keeping a couple extra values of d for counts 1 and 2)


• But should we really just use the regular unigram P(w)?

IS 7118: NLP Unit-4: N-Grams, 87


Prof. R.K.Rao Bandaru
Kneser-Ney Smoothing I
• Better estimate for probabilities of lower-order unigrams!
– Shannon game: I can’t see without my reading___________?
Francisco
glasses

– “Francisco” is more common than “glasses”


– … but “Francisco” always follows “San”
• The unigram is useful exactly when we haven’t seen this bigram!
• Instead of P(w): “How likely is w”
• Pcontinuation(w): “How likely is w to appear as a novel continuation?
– For each word, count the number of bigram types it completes
– Every bigram type was a novel continuation the first time it was seen

PCONTINUATION (w)µ {wi-1 :c(wi-1,w) > 0}


IS 7118: NLP Unit-4: N-Grams, 88
Prof. R.K.Rao Bandaru
Kneser-Ney Smoothing II

• How many times does w appear as a novel continuation:

PCONTINUATION (w)µ {wi-1 :c(wi-1,w) > 0}

• Normalized by the total number of word bigram types

{(w j-1,w j ):c(w j-1,w j ) > 0}


{wi-1 :c(wi-1,w) > 0}
PCONTINUATION (w) =
{(w j-1,w j ) :c(w j-1,w j ) > 0}
IS 7118: NLP Unit-4: N-Grams, 89
Prof. R.K.Rao Bandaru
Kneser-Ney Smoothing III

• Alternative metaphor: The number of # of word types seen to


precede w
|{wi-1 :c(wi-1,w) > 0}|
• normalized by the # of words preceding all words:

{wi-1 : c(wi-1,w) > 0}


PCONTINUATION (w) =
å {w' i-1 : c(w'i-1,w') > 0}
w'
• A frequent word (Francisco) occurring in only one context (San)
will have a low continuation probability
IS 7118: NLP Unit-4: N-Grams, 90
Prof. R.K.Rao Bandaru
Kneser-Ney Smoothing IV

max(c(wi-1,wi )- d,0)
PKN (wi | wi-1 ) = + l (wi-1 )PCONTINUATION (wi )
c(wi-1 )
λ is a normalizing constant; the probability mass we’ve discounted

d
l (wi-1 ) = {w :c(wi-1,w) > 0}
c(wi-1 )
The number of word types that can follow wi-1
the normalized discount = # of word types we discounted
= # of times we applied normalized discount
IS 7118: NLP Unit-4: N-Grams, 91
Prof. R.K.Rao Bandaru
Kneser-Ney Smoothing:
Recursive formulation
i
i-1 max(cKN (wi-n+1 )- d,0) i-1 i-1
PKN (wi | wi-n+1 ) = i-1
+ l (wi-n+1 )PKN (wi | wi-n+2 )
cKN (wi-n+1 )

ìï count(·) for the highest order


cKN (·) = í
ïî continuationcount(·) for lower order

Continuation count = Number of unique single word contexts for 


IS 7118: NLP Unit-4: N-Grams, 92
Prof. R.K.Rao Bandaru
End of the Unit-4

???

You might also like