natural language processing - vub · • tokenization (counting) • lemmatization • stemming...
TRANSCRIPT
Natural Language Processing
Language modeling with N-grams
Pieter Wellens2012-2013
These slides are based on the course materials from the ANLP course given at the School of Informatics, Edinburgh and the online coursera Stanford NLP course by
Jurafski and Manning.
Saturday 23 February 13
Last week
Saturday 23 February 13
Last week
• Basics of text processing: words and morphology
Saturday 23 February 13
Last week
• Basics of text processing: words and morphology
• tokenization (counting)
• lemmatization
• stemming (porter algorithm)
• sentence segmentation
• cross-linguistic variation
• ...
Saturday 23 February 13
Today
• Language modeling with N-gram models
• Introduction to N-gram models
• Estimating N-gram probabilities
• Evaluation and Perplexity
• Unseen N-grams and smoothing
• Interpolation and scaling
Saturday 23 February 13
Exercise
• How likely are the following sentences or phrases in English?
Saturday 23 February 13
Exercise
• How likely are the following sentences or phrases in English?
• Це не англійська
Saturday 23 February 13
Exercise
• How likely are the following sentences or phrases in English?
• Це не англійська
• small the is house
Saturday 23 February 13
Exercise
• How likely are the following sentences or phrases in English?
• Це не англійська
• small the is house
• I am going house
Saturday 23 February 13
Exercise
• How likely are the following sentences or phrases in English?
• Це не англійська
• small the is house
• I am going house
• what if it as part of a larger sentence?
Saturday 23 February 13
Exercise
• How likely are the following sentences or phrases in English?
• Це не англійська
• small the is house
• I am going house
• what if it as part of a larger sentence? (712000 google hits)
Saturday 23 February 13
Exercise
• How likely are the following sentences or phrases in English?
• Це не англійська
• small the is house
• I am going house
• what if it as part of a larger sentence? (712000 google hits)
• I love you
Saturday 23 February 13
Exercise
• How likely are the following sentences or phrases in English?
• Це не англійська
• small the is house
• I am going house
• what if it as part of a larger sentence? (712000 google hits)
• I love you
• what about: I heart you
Saturday 23 February 13
Exercise
• How likely are the following sentences or phrases in English?
• colorless green ideas sleep furiously
Saturday 23 February 13
Exercise
• How likely are the following sentences or phrases in English?
• colorless green ideas sleep furiously
• She danced the night away
Saturday 23 February 13
Introduction
• Why would it be useful to have a probability function P(phrase) that returns the probability of a phrase?
Saturday 23 February 13
Introduction
• Why would it be useful to have a probability function P(phrase) that returns the probability of a phrase?
• Machine Translation
• P(tall person) > P(high person)
• Spell Correction
• The office is about fifteen minuets from my house
• P(about fifteen minutes from) > P(about fifteen minuets from)
• Speech Recognition
• P(I saw a van) >> P(eyes awe of an)
Saturday 23 February 13
Language Models
• Language models answer the question: “How likely is a string of words for a given language?”
• Given a string of English words W = w1, w2, w3, w4, ...., wn
• what is P(W)?
• Related to P(w5|w1,w2,w3,w4)
• A model that can do either of these is called a language model.
Saturday 23 February 13
Statistical language models
• Ignore everything except the raw data
• big data
• lots of computing power
• Count frequencies of sequences of words in large-scale (web-based) corpora
• N-gram models
Saturday 23 February 13
Today
• Language modeling with N-gram models
• Introduction to N-gram models
• Estimating N-gram probabilities
• Evaluation and Perplexity
• Unseen N-grams and smoothing
• Interpolation and scaling
Saturday 23 February 13
How to compute P(W)
• How do we compute this joint probability?
• P(its, water, is, so, transparent, that)
• Intuition: Let’s rely on the Chain Rule of Probability
Saturday 23 February 13
Reminder: The Chain Rule
• Rewriting a joint probability as a product of conditional probabilities
• P(w1,w2,w3,...wn) = P(w1)P(w2|w1)P(w3|w1,w2)...P(wn|w1,...,wn-1) = ∏i-=1>n P(wi|w1,...,wi-1)
• 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)
Saturday 23 February 13
How to estimate these probabilities
• Can we just count and divide?
• P(the|its water is so transparent that) = Count(its water is so transparent that the) / Count(its water if so transparent that)
Saturday 23 February 13
How to estimate these probabilities
• Can we just count and divide?
• P(the|its water is so transparent that) = Count(its water is so transparent that the) / Count(its water if so transparent that)
• No! Too many possible sentences!
• We will never see enough data for estimating these
Saturday 23 February 13
Markov assumption
• Simplifying assumption:
• 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)
Saturday 23 February 13
Markov assumption
• Simplifying assumption:
• 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)
• P(w1,w2,w3,...wn) = ∏i-=1>n P(wi|wi-k,...,wi-1)
• k-th order Markov model. What is conditioned on (e.g.wi-k,...,wi-1) is called the history.
• Models that use this assumption are called n-gram models
Saturday 23 February 13
The simplest case: Unigram model
• P(w1,w2,w3,...wn) = ∏i-=1>n P(wi)
• 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
Saturday 23 February 13
Bigram model
• P(w1,w2,w3,...wn) = ∏i-=1>n P(wi|wi-1) = P(w1)P(w2|w1) P(w3|w2)... P(wn|wn-1)
• Condition on the previous word
• Some automatically generated sentences from a unigram 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, yenoutside, new, car, parking, lot, of, the, agreement, reachedthis, would, be, a, record, november
Saturday 23 February 13
N-gram models
• We can extend to trigrams, 4-grams, 5-grams
Saturday 23 February 13
N-gram models
• We can extend to trigrams, 4-grams, 5-grams
• In general this is an insufficient model of language
• Why?
Saturday 23 February 13
N-gram models
• We can extend to trigrams, 4-grams, 5-grams
• In general this is an insufficient model of language
• Why? long distance dependencies
• “The computer which I had just put into the machine room on the fifth floor ...”
Saturday 23 February 13
N-gram models
• We can extend to trigrams, 4-grams, 5-grams
• In general this is an insufficient model of language
• Why? long distance dependencies
• “The computer which I had just put into the machine room on the fifth floor crashed”
• Often, however, we can get away with N-gram models
Saturday 23 February 13
Estimating bigram probabilities
• The maximum likelihood estimate
• P(wi | wi-1) = count(wi-1,wi) / count(wi-1)
Saturday 23 February 13
Estimating bigram probabilities
• The maximum likelihood estimate
• P(wi | wi-1) = count(wi-1,wi) / count(wi-1)
• An example: <s> I am Sam </s> <s> Sam I am </s> <s>I do not like green eggs and ham</s>
• P(I | <s>) = ? P(do | I) = ?
• P(</s> | Sam) = ?
Saturday 23 February 13
Estimating bigram probabilities
• The maximum likelihood estimate
• P(wi | wi-1) = count(wi-1,wi) / count(wi-1)
• An example: <s> I am Sam </s> <s> Sam I am </s> <s>I do not like green eggs and ham</s>
• P(I | <s>) = 2/3 = .67 P(do | I) = 1/3 = .33
• P(</s> | Sam) = 1/2 = .5
Saturday 23 February 13
Another example: trigram
• Europarl corpus:
• 225 trigrams in the Europarl corpus start with “the red”
• 123 of them end with “cross”
• maximum likelihood probability is 123/225 = 0.547
5
Example: 3-Gram
• Counts for trigrams and estimated word probabilities
the green (total: 1748)word c. prob.
paper 801 0.458group 640 0.367light 110 0.063party 27 0.015ecu 21 0.012
the red (total: 225)word c. prob.
cross 123 0.547tape 31 0.138army 9 0.040card 7 0.031, 5 0.022
the blue (total: 54)word c. prob.
box 16 0.296. 6 0.111
flag 6 0.111, 3 0.056
angel 3 0.056
– 225 trigrams in the Europarl corpus start with the red
– 123 of them end with cross
! maximum likelihood probability is 123
225
= 0.547.
Philipp Koehn ANLP Lecture 4 26 September 2012
Saturday 23 February 13
Practical issue
• We do everything in log space
• Avoid underflow
• (addition is faster than multiplication)
• log (p1 x p2 x p3 x p4) = log p1 + log p2 + log p3 + log p4
Saturday 23 February 13
Google n-grams
• http://storage.googleapis.com/books/ngrams/books/datasetsv2.html
• http://books.google.com/ngrams
• http://books.google.com/ngrams/info
Saturday 23 February 13
Today
• Language modeling with N-gram models
• Introduction to N-gram models
• Estimating N-gram probabilities
• Evaluation and Perplexity
• Unseen N-grams and smoothing
• Interpolation and scaling
Saturday 23 February 13
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.
Saturday 23 February 13
Perplexity
• Claude Shannon came up with a game:
• How well can we predict the next word?
• Would unigrams be good at this game?
• A better model is one which assigns a higher probability to the word that actually occurs
mushrooms 0.1
pepperoni 0.1
anchovies 0.01
….
fried rice 0.0001
….
and 1e-100
I"always"order"pizza"with"cheese"and"____"
The"33rd"President"of"the"US"was"____"
I"saw"a"____"
Saturday 23 February 13
Perplexity
• The best model is one that gives the highest probability to a given sentence.
• Perplexity:
• Chain rule:
• bigrams:
PP(W ) = P(w1w2...wN )−
1N
=1
P(w1w2...wN )N
Saturday 23 February 13
Perplexity
• Lower perplexity = better model
• Training 38 million words, test 1.5 million words on wall street journal corpus
N"gram'Order'
Unigram' Bigram' Trigram'
Perplexity* 962* 170* 109*
Saturday 23 February 13
Today
• Language modeling with N-gram models
• Introduction to N-gram models
• Estimating N-gram probabilities
• Evaluation and Perplexity
• Unseen N-grams and smoothing
• Interpolation and scaling
Saturday 23 February 13
Unseen N-grams (generalization)
• We have seen “i like to” in our corpus
• We have never seen “i like to smooth” in our corpus
• P(smooth|i like to) = 0
• Any sentence that includes “i like to smooth” will be assigned probability 0
Saturday 23 February 13
An example: Shakespeare corpus
• N = 884647 tokens and V = 29,066 types
• Shakespeare produced 300000 bigram types out of V2 = 844 million “possible” bigrams.
• 99,96 of the “possible” bigrams were never seen.
• Gets worse for larger n (e.g. 5-grams)
Saturday 23 February 13
Add-one Smoothing
• Also called Laplace smoothing
• Pretend we saw each word one more time than we did by adding one to all the counts.
• MLE estimate:
• Add-one estimate:
•
PMLE (wi |wi−1) =c(wi−1,wi )c(wi−1)
PAdd−1(wi |wi−1) =c(wi−1,wi )+1c(wi−1)+V
Saturday 23 February 13
Example: Berkeley restaurant corpus
• 9222 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
Saturday 23 February 13
Example: Berkeley restaurant corpus
• unigrams:
• bigrams:
Saturday 23 February 13
Example: Berkeley restaurant corpus
• unigrams:
• bigrams (MLE):
Saturday 23 February 13
Example: Berkeley restaurant corpus
• unigrams:
• bigrams (add-one):
Saturday 23 February 13
Example: Berkeley restaurant corpus
• smoothed probability with:
• bigrams (add-one):
Saturday 23 February 13
Add-one Smoothing
• Add-one smoothing is a blunt tool
• isn’t appropriate for n-grams
• it is used in other NLP tools where the amount of zeros isn’t so huge.
Saturday 23 February 13
Improvements to smoothing
• Add-k smoothing or Add-α smoothing:
• k or α < 1 and can be optimised with a held-out set.
PAdd−k (wi |wi−1) =c(wi−1,wi )+ kc(wi−1)+ kV
PAdd−k (wi |wi−1) =c(wi−1,wi )+m(
1V)
c(wi−1)+m
Saturday 23 February 13
Improvements to smoothing
• Unigram prior smoothing
PAdd−k (wi |wi−1) =c(wi−1,wi )+m(
1V)
c(wi−1)+m
PUnigramPrior (wi |wi−1) =c(wi−1,wi )+mP(wi )
c(wi−1)+m
Saturday 23 February 13
Improvements to smoothing
• Unigram prior smoothing
• Still not good enough...
PAdd−k (wi |wi−1) =c(wi−1,wi )+m(
1V)
c(wi−1)+m
PUnigramPrior (wi |wi−1) =c(wi−1,wi )+mP(wi )
c(wi−1)+m
Saturday 23 February 13
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.
Saturday 23 February 13
Notation: Nc = Frequency of frequency c
• Nc= the count of things we’ve seen c times
Saturday 23 February 13
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 3Sam 2am 2do 1not 1eat 1
• N1 = , N2 = , N3 =
Saturday 23 February 13
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 3Sam 2am 2do 1not 1eat 1
• N1 = 3, N2 = 2 , N3 = 1
• N0 = number of tokens (or n-grams) = 10
Saturday 23 February 13
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?
Saturday 23 February 13
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.
Saturday 23 February 13
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 but how to estimate?
Saturday 23 February 13
Good Turing formula
c*= (c+1)Nc+1
Nc
• Calculate it for cases seen once (.e.g. trout):
• MLE: 1/18
• c*(trout) = (1+1) * N2/N1 = 2 * 1/3 = 2/3
• P*GT (trout) = 2/3 / 18 = 1/27
Saturday 23 February 13
Good-Turing numbers
• Numbers from Church and Gale (1991)
• 22 million words of AP NewswireCount&c& Good&Turing&c*&
0& .0000270&1& 0.446&2& 1.26&3& 2.24&4& 3.24&5& 4.22&6& 5.19&7& 6.21&8& 7.24&9& 8.25&
c*= (c+1)Nc+1
Nc
Saturday 23 February 13
Today
• Language modeling with N-gram models
• Introduction to N-gram models
• Estimating N-gram probabilities
• Evaluation and Perplexity
• Unseen N-grams and smoothing
• Interpolation and scaling
Saturday 23 February 13
Backoff and interpolation
• Sometimes it helps to use less context
• For example because you have only seen the large context a few times (not reliable)
• Backoff:
• use trigram if you have good evidence, otherwise bigram, otherwise unigram
• Interpolation
• mix unigram, bigram, trigram
• Interpolation works better than backoffSaturday 23 February 13
Linear Interpolation
• Simple interpolation
• Lambdas conditional on context
Saturday 23 February 13
How to find out good lambdas?
• Use a held-out corpus
• Choose lambdas to maximize the probability of held-out data
Held%Out)Data)
Test))Data)Training)Data)
Saturday 23 February 13
Unknown words: Open versus closed vocabulary tasks
• Often we don’t encounter all words in the training set
• Out Of Vocabulary words = OOV words
• Open vocabulary task
• Solution: create an unknown word token <UNK>
• At normalization phase change some rare or unimportant words by <UNK>
• Train on this data-set
• At testing time use these <UNK> probabilities for “real” unseen words
Saturday 23 February 13
Large-scale (web) data
• For example: Google N-gram corpus
• Pruning
• Only store N-grams with count > threshold (google > 40, for unigrams)
• Entropy-based pruning
Saturday 23 February 13
Smoothing for Web-scale N-grams
• “Stupid Backoff” (Brants et al. 2007)
• No discounting, just use relative frequencies
S(wi |wi−k+1i−1 ) =
count(wi−k+1i )
count(wi−k+1i−1 )
if count(wi−k+1i )> 0
0.4S(wi |wi−k+2i−1 ) otherwise
"
#$$
%$$
S(wi ) =count(wi )
N
Saturday 23 February 13
Exercise
• (a) Implement, in a language of your own choice, a function that can measure a similarity between two strings. (b) Clearly document in your source code how your algorithm works.
• Taking two strings as input it should return a real number between 0 and 1. 1 means very similar (probably equal) and 0 means highly different (probably nothing in common at all).
• Do not consult existing algorithms (we will learn about them next week). Build it using your own ideas, knowledge and intuitions.
• Goal is not to write the best (fastest, most accurate, or robust) algorithm but to make you think about this problem. Don’t spend more than two hours on it.
• deadline: Thursday 28 Feb 20.00h. Saturday 23 February 13
Assignment
• Implement a tokenizer for English in a programming language of your choosing
• It should take as input an ascii (.txt) file which contains raw text (e.g. Shakespeare corpus)
• Output is a token on each line
• Make sure it is consistent wrt exceptions
• Deadline: Wednesday 6 March
• Send documented source code to [email protected]
Saturday 23 February 13