�Language Modeling
Introduction to N-grams
Dan Jurafsky
Probabilistic Language Models
Why?
Dan Jurafsky
Probabilistic Language Modeling
P(W) = P(w1,w2,w3,w4,w5…wn)
P(w5|w1,w2,w3,w4)
P(W) or P(wn|w1,w2…wn-1) is called a language model.
Dan Jurafsky
How to compute P(W)
Dan Jurafsky
Reminder: The Chain Rule
p(B|A) = P(A,B)/P(A) Rewriting: P(A,B) = P(A)P(B|A)
P(A,B,C,D) = P(A)P(B|A)P(C|A,B)P(D|A,B,C)
P(x1,x2,x3,…,xn) = P(x1)P(x2|x1)P(x3|x1,x2)…P(xn|x1,…,xn-1)
Dan Jurafsky
The Chain Rule applied to compute joint probability of words in sentence
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)
Dan Jurafsky
How to estimate these probabilities
Dan Jurafsky
Markov Assumption
Andrei Markov
Dan Jurafsky
Markov Assumption
Dan Jurafsky
Simplest case: 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
Some automatically generated sentences from a unigram model
Dan Jurafsky
Bigram model
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, record, november
Dan Jurafsky
N-gram models
“The computer which I had just put into the machine room on the fifth floor crashed.”
Dan Jurafsky
�Language Modeling
Introduction to N-grams
Dan Jurafsky
�Language Modeling
Estimating N-gram Probabilities
Dan Jurafsky
Estimating bigram probabilities
Dan Jurafsky
An example
<s> I am Sam </s>
<s> Sam I am </s>
<s> I do not like green eggs and ham </s>
Dan Jurafsky
More examples: �Berkeley Restaurant Project sentences
Dan Jurafsky
Raw bigram counts
Dan Jurafsky
Raw bigram probabilities
Dan Jurafsky
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)
= .000031
Dan Jurafsky
What kinds of knowledge?
Dan Jurafsky
Practical Issues
Dan Jurafsky
Language Modeling Toolkits
Dan Jurafsky
Google N-Gram Release, August 2006
…
Dan Jurafsky
Google N-Gram Release
Dan Jurafsky
Google Book N-grams
Dan Jurafsky
�Language Modeling
Estimating N-gram Probabilities
Dan Jurafsky
�Language Modeling
Evaluation and Perplexity
Dan Jurafsky
Evaluation: How good is our model?
Dan Jurafsky
(Extra Slide not in video) �Training on the test set
30
Dan Jurafsky
Extrinsic evaluation of N-gram models
Dan Jurafsky
Difficulty of extrinsic (in-vivo) evaluation of N-gram models
Dan Jurafsky
Intuition of Perplexity
I always order pizza with cheese and ____
The 33rd President of the US was ____
I saw a ____
mushrooms 0.1
pepperoni 0.1
anchovies 0.01
….
fried rice 0.0001
….
and 1e-100
Dan Jurafsky
Perplexity
Perplexity is the inverse probability of the test set, normalized by the number of words:
Chain rule:
For bigrams:
Minimizing perplexity is the same as maximizing probability
The best language model is one that best predicts an unseen test set
Dan Jurafsky
The Shannon Game intuition for perplexity
Dan Jurafsky
The Shannon Game intuition for perplexity
A call-routing phone system gets 120K calls and has to recognize
To get the perplexity of this sequence of length 120K:
1) multiply 120K probabilities (90K of which are 1/4 and 30K of which are 1/120K)
2) take the inverse 120,000th root:
Perp = (¼ * ¼ * ¼* ¼ * ¼ * …. * 1/120K * 1/120K * ….)^(-1/120K)
Can be arithmetically simplified to just N = 4: operator (1/4), sales (1/4), tech support (1/4), and 30,000 names (1/120,000):
Perplexity= (¼ * ¼ * ¼ * 1/120K)^(-1/4) = 52.6
Perplexity as branching factor
Dan Jurafsky
Lower perplexity = better model
N-gram Order | Unigram | Bigram | Trigram |
Perplexity | 962 | 170 | 109 |
Dan Jurafsky
�Language Modeling
Evaluation and Perplexity
Dan Jurafsky
�Language Modeling
Generalization and zeros
Dan Jurafsky
The Shannon Visualization Method
(<s>, w) according to its probability
<s> I
I want
want to
to eat
eat Chinese
Chinese food
food </s>
I want to eat Chinese food
Dan Jurafsky
Approximating Shakespeare
Dan Jurafsky
Shakespeare as corpus
Dan Jurafsky
The wall street journal is not shakespeare (no offense)
Dan Jurafsky
Can you guess the author of these random 3-gram sentences?
45
Dan Jurafsky
The perils of overfitting
Dan Jurafsky
Zeros
… denied the allegations
… denied the reports
… denied the claims
… denied the request
P(“offer” | denied the) = 0
… denied the offer
… denied the loan
Dan Jurafsky
Zero probability bigrams
Dan Jurafsky
�Language Modeling
Generalization and zeros
Dan Jurafsky
�Language Modeling
Smoothing: Add-one (Laplace) smoothing
Dan Jurafsky
The intuition of smoothing (from Dan Klein)
P(w | denied the)
3 allegations
2 reports
1 claims
1 request
7 total
P(w | denied the)
2.5 allegations
1.5 reports
0.5 claims
0.5 request
2 other
7 total
allegations
reports
claims
attack
request
man
outcome
…
allegations
attack
man
outcome
…
allegations
reports
claims
request
Dan Jurafsky
Add-one estimation
Dan Jurafsky
Maximum Likelihood Estimates
Dan Jurafsky
Berkeley Restaurant Corpus: Laplace smoothed bigram counts
Dan Jurafsky
Laplace-smoothed bigrams
Dan Jurafsky
Reconstituted counts
Dan Jurafsky
Compare with raw bigram counts
Dan Jurafsky
Add-1 estimation is a blunt instrument
Dan Jurafsky
�Language Modeling
Smoothing: Add-one (Laplace) smoothing
Dan Jurafsky
�Language Modeling
Interpolation, Backoff, and Web-Scale LMs
Dan Jurafsky
Backoff and Interpolation
Dan Jurafsky
Linear Interpolation
Dan Jurafsky
How to set the lambdas?
Training Data
Held-Out Data
Test
Data
Dan Jurafsky
Unknown words: Open versus closed vocabulary tasks
Dan Jurafsky
Huge web-scale n-grams
Dan Jurafsky
Smoothing for Web-scale N-grams
66
Dan Jurafsky
N-gram Smoothing Summary
67
Dan Jurafsky
Advanced Language Modeling
Dan Jurafsky
�Language Modeling
Interpolation, Backoff, and Web-Scale LMs
Dan Jurafsky