3 LM Jan 08 2021
3 LM Jan 08 2021
Modeling
Probabilistic Language Models
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)
The Chain Rule applied to compute joint
probability of words in sentence
Simplifying assumption:
Andrei Markov
Or maybe
P(the | its water is so transparent that) ≈ P(the | transparent that)
Markov Assumption
P(w1w 2 …w n ) ≈ ∏ P(w i )
i
Some automatically generated sentences from a unigram model
count(w i−1,w i )
P(w i | w i−1 ) =
count(w i−1 )
c(w i−1,w i )
P(w i | w i−1 ) =
c(w i−1 )
€
An example
<s> I am Sam </s>
c(w i−1,w i )
P(w i | w i−1 ) = <s> Sam I am </s>
c(w i−1 )
<s> I do not like green eggs and ham </s>
More examples:
Berkeley Restaurant Project sentences
Result:
Bigram estimates of sentence probabilities
SRILM
◦ http://www.speech.sri.com/projects/srilm/
KenLM
◦ https://kheafield.com/code/kenlm/
Google N-Gram Release, August 2006
…
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
Google Book N-grams
http://ngrams.googlelabs.com/
Language Estimating N-gram
Probabilities
Modeling
Language Evaluation and Perplexity
Modeling
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?
Chain rule:
For bigrams:
Let's imagine a call-routing phone system gets 120K calls and has to recognize
◦ "Operator" (let's say this occurs 1 in 4 calls)
◦ "Sales" (1in 4)
◦ "Technical Support" (1 in 4)
◦ 30,000 different names (each name occurring 1 time in the 120K calls)
◦ What is the perplexity? Next slide
The Shannon Game intuition for perplexity
Josh Goodman: imagine a call-routing phone system gets 120K calls and has
to recognize
◦ "Operator" (let's say this occurs 1 in 4 calls)
◦ "Sales" (1in 4)
◦ "Technical Support" (1 in 4)
◦ 30,000 different names (each name occurring 1 time in the 120K calls)
We get the perplexity of this sequence of length 120Kby first multiplying 120K
probabilities (90K of which are 1/4 and 30K of which are 1/120K), nd then
taking the inverse 120,000th root:
Perp = (¼ * ¼ * ¼* ¼ * ¼ * …. * 1/120K * 1/120K * ….)^(-1/120K)
But this can be arithmetically simplified to just N = 4: the operator (1/4), the
sales (1/4), the tech support (1/4), and the 30,000 names (1/120,000):
Perplexity= ((¼ * ¼ * ¼ * 1/120K)^(-1/4) = 52.6
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?
Lower perplexity = better model
Approximating Shakespeare
its bigram probability), and so on.
To give an intuition for the increasing power of higher-order N-grams, Fig. 4.3
shows random sentences generated from unigram, bigram, trigram, and 4-gram
models trained on Shakespeare’s works.
–To him swallowed confess hear both. Which. Of save on trail for are ay device and
1gram rote life have
–Hill he late speaks; or! a more to leg less first you enter
–Why dost stand forth thy canopy, forsooth; he is this palpable hit the King Henry. Live
2gram king. Follow.
–What means, sir. I confess she? then all sorts, he is trim, captain.
–Fly, and will rid me these news of price. Therefore the sadness of parting, as they say,
3gram ’tis done.
–This shall forbid it should be branded, if renown made it empty.
–King Henry. What! I will go seek the traitor Gloucester. Exeunt some of the watch. A
4gram great banquet serv’d in;
–It cannot be but so.
Figure 4.3 Eight sentences randomly generated from four N-grams computed from Shakespeare’s works. All
characters were mapped to lower-case and punctuation marks were treated as words. Output is hand-corrected
Shakespeare as corpus
1
gram
Months the my and issue of year foreign new exchange’s september
were recession exchange new endorsed a acquire to six executives
Last December through the way to preserve the Hudson corporation N.
2
gram
B. E. C. Taylor would seem to complete the major central planners one
point five percent of U. S. E. has already old M. X. corporation of living
on information such as more frequently fishing to keep her
They also point to ninety nine point six billion dollars from two hundred
3
gram
four oh six three percent of the rates of interest stores as Mexico and
Brazil on market conditions
Figure 4.4 Three sentences randomly generated from three N-gram models computed from
40 million words of the Wall Street Journal, lower-casing all characters and treating punctua-
Can you guess the training set author of the LM
that generated these random 3-gram sentences?
They also point to ninety nine point six billion dollars
from two hundred four oh six three percent of the rates
of interest stores as Mexico and gram Brazil on market
conditions
This shall forbid it should be branded, if renown made
it empty.
“You are uniformly charming!” cried he, with a smile of
associating and now and then I bowed and they
perceived a chaise and four to wish for.
44
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
Zeros
Training set: • Test set
… denied the allegations … denied the offer
… denied the reports … denied the loan
… denied the claims
… denied the request
allegations
3 allegations
outcome
2 reports
reports
attack
1 claims
…
request
claims
man
1 request
7 total
Steal probability mass to generalize better
P(w | denied the)
2.5 allegations
allegations
1.5 reports
allegations
outcome
0.5 claims
reports
attack
0.5 request
…
man
claims
request
2 other
7 total
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!
count(wi )
S(wi ) =
N
65
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
66
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) = λ P(wi | wi−2 wi−1 ) + (1− λ )
| history |
◦ These turned out to perform very poorly for speech recognition
(why?)
Language Interpolation, Backoff, and
Web-Scale LMs
Modeling
Advanced:
Language Kneser-Ney Smoothing
Modeling
Absolute discounting: just subtract a little
from each count
Suppose we wanted to subtract a little from a Bigram count Bigram count in
count of 4 to save probability mass for the zeros in training heldout set
0 .0000270
How much to subtract ? 1 0.448
2 1.25
Church and Gale (1991)’s clever idea 3 2.24
4 3.23
Divide up 22 million words of AP Newswire
5 4.21
◦ Training and held-out set 6 5.23
◦ for each bigram in the training set 7 6.21
◦ see the actual count in the held-out set! 8 7.21
It sure looks like c* = (c - .75) 9 8.26
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 ) = + λ (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)?
71
Kneser-Ney Smoothing I
Better estimate for probabilities of lower-order unigrams!
Kong
glasses
◦ Shannon game: I can’t see without my reading___________?
◦ “Kong” turns out to be more common than “glasses”
◦ … but “Kong” always follows “Hong”
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
max(c(wi−1, wi ) − d, 0)
PKN (wi | wi−1 ) = + λ (wi−1 )PCONTINUATION (wi )
c(wi−1 )
λ is a normalizing constant; the probability mass we’ve discounted
d
λ (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
75
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
+ λ (wi−n+1 )PKN (wi | wi−n+2 )
cKN (wi−n+1 )