Fall 2001 EE669: Natural Language Processing 1 Lecture 6: N-gram Models and Sparse Data (Chapter 6 of Manning and Schutze, Chapter 6 of Jurafsky and Martin,

Presentation on theme: "Fall 2001 EE669: Natural Language Processing 1 Lecture 6: N-gram Models and Sparse Data (Chapter 6 of Manning and Schutze, Chapter 6 of Jurafsky and Martin,"— Presentation transcript:

Fall 2001 EE669: Natural Language Processing 1 Lecture 6: N-gram Models and Sparse Data (Chapter 6 of Manning and Schutze, Chapter 6 of Jurafsky and Martin, and Chen and Goodman 1998) Wen-Hsiang Lu ( 盧文祥 ) Department of Computer Science and Information Engineering, National Cheng Kung University 2014/03/24 (Slides from Dr. Mary P. Harper, http://min.ecn.purdue.edu/~ee669/)

Fall 2001 EE669: Natural Language Processing 2 Overview Statistical inference consists of taking some data (generated in accordance with some unknown probability distribution) and then making some inferences about its distribution. We will study the classic task of language modeling as an example of statistical estimation.

Fall 2001 EE669: Natural Language Processing 3 “Shannon Game” and Language Models Claude E. Shannon. “Prediction and Entropy of Printed English”, Bell System Technical Journal 30:50-64. 1951. Predict the next word, given the previous words Determine probability of different sequences by examining training corpus Applications: OCR / Speech recognition – resolve ambiguity Spelling correction Machine translation Author identification

Fall 2001 EE669: Natural Language Processing 4 Speech and Noisy Channel Model In speech we can only decode the output to give the most likely input. Noisy Channel p(A|W) W A ^W^W Decode

Fall 2001 EE669: Natural Language Processing 5 Statistical Estimators Example: Corpus: five Jane Austen novels N = 617,091 words, V = 14,585 unique words Task: predict the next word of the trigram “inferior to ___” from test data, Persuasion: “[In person, she was] inferior to both [sisters.]” Given the observed training data … How do you develop a model (probability distribution) to predict future events?

Fall 2001 EE669: Natural Language Processing 6 The Perfect Language Model Sequence of word forms Notation: W = (w 1,w 2,w 3,...,w n ) The big (modeling) question is what is p(W)? Well, we know (chain rule): p(W) = p(w 1,w 2,w 3,...,w n ) = p(w 1 )p(w 2 |w 1 )p(w 3 |w 1,w 2 )   p(w n |w 1,w 2,...,w n-1 ) Not practical (even short for W  too many parameters)

Fall 2001 EE669: Natural Language Processing 7 Markov Chain Unlimited memory: –for w i, we know all its predecessors w 1,w 2,w 3,...,w i-1 Limited memory: –we disregard predecessors that are “too old” –remember only k previous words: w i-k,w i-k+1,...,w i-1 –called “k th order Markov approximation” Stationary character (no change over time): p(W)   i=1..n p(w i |w i-k,w i-k+1,...,w i-1 ), n = |W|

Fall 2001 EE669: Natural Language Processing 8 N-gram Language Models (n-1) th order Markov approximation  n-gram LM: p(W)   i=1..n p(w i |w i-n+1,w i-n+2,...,w i-1 ) In particular (assume vocabulary |V| = 20k): 0-gram LM: uniform model p(w) = 1/|V| 1 parameter 1-gram LM: unigram model p(w)2  10 4 parameters 2-gram LM: bigram model p(w i |w i-1 ) 4  10 8 parameters 3-gram LM: trigram modep(w i |w i-2,w i-1 ) 8  10 12 parameters 4-gram LM: tetragram modelp(w i | w i-3,w i-2,w i-1 )1.6  10 17 parameters

Fall 2001 EE669: Natural Language Processing 9 Reliability vs. Discrimination “large green ___________” tree? mountain? frog? car? “swallowed the large green ________” pill? tidbit? larger n: more information about the context of the specific instance (greater discrimination) smaller n: more instances in training data, better statistical estimates (more reliability)

Fall 2001 EE669: Natural Language Processing 10 LM Observations How large n? –zero is enough (theoretically) –but anyway: as much as possible (as close to “perfect” model as possible) –empirically: 3 parameter estimation? (reliability, data availability, storage space,...) 4 is too much: |V|=60k  1.296  10 19 parameters but: 6-7 would be almost ideal Reliability decreases with increase in detail (need compromise) For now, word forms only (no “linguistic” processing)

Fall 2001 EE669: Natural Language Processing 11 Parameter Estimation Parameter: numerical value needed to compute p(w|h) Data preparation: get rid of formatting etc. (“text cleaning”) define words (include punctuation) define sentence boundaries (insert “words” and ) letter case: keep, discard, or be smart: –name recognition –number type identification numbers: keep, replace by

Fall 2001 EE669: Natural Language Processing 12 Maximum Likelihood Estimate MLE: Relative Frequency... –...best predicts the data at hand (the “training data”) –See (Ney et al. 1997) for a proof that the relative frequency really is the maximum likelihood estimate. (p225) Trigrams from Training Data T: –count sequences of three words in T: C 3 (w i-2,w i-1,w i ) –count sequences of two words in T: C 2 (w i-2,w i-1 ): Can use C 2 (y,z) =  w C 3 (y,z,w) P MLE (w i-2,w i-1,w i ) = C 3 (w i-2,w i-1,w i ) / N P MLE (w i |w i-2,w i-1 ) = C 3 (w i-2,w i-1,w i ) / C 2 (w i-2,w i-1 )

Fall 2001 EE669: Natural Language Processing 13 Character Language Model Use individual characters instead of words: Might consider 4-grams, 5-grams or even more Good for cross-language comparisons Transform cross-entropy between letter- and word-based models: H S (p c ) = H S (p w ) / avg. # of characters/word in S p(W)  df  i=1..n p(c i |c i-n+1,c i-n+2,...,c i-1 )

Fall 2001 EE669: Natural Language Processing 14 LM: an Example Training data: He can buy you the can of soda –Unigram: (8 words in vocabulary) p 1 (He) = p 1 (buy) = p 1 (you) = p 1 (the) = p 1 (of) = p 1 (soda) =.125 p 1 ( can ) =.25 –Bigram: p 2 ( He| ) = 1, p 2 ( can|He ) = 1, p 2 ( buy|can ) =.5, p 2 ( of|can ) =.5, p 2 ( you |buy ) = 1,... –Trigram: p 3 ( He|, ) = 1, p 3 ( can|,He ) = 1, p 3 ( buy|He,can ) = 1, p 3 ( of|the,can ) = 1,..., p 3 ( |of,soda ) = 1. –Entropy: H(p 1 ) = 2.75, H(p 2 ) = 1, H(p 3 ) = 0

Fall 2001 EE669: Natural Language Processing 15 LM: an Example (The Problem) Cross-entropy: S = It was the greatest buy of all Even H S (p 1 ) fails (= H S (p 2 ) = H S (p 3 ) =  ), because: –all unigrams but p 1 (the), p 1 (buy), and p 1 (of) are 0. –all bigram probabilities are 0. –all trigram probabilities are 0. Need to make all “theoretically possible” probabilities non-zero.

Fall 2001 EE669: Natural Language Processing 17 Training Corpus Instances: “inferior to___”

Fall 2001 EE669: Natural Language Processing 18 Actual Probability Distribution

Fall 2001 EE669: Natural Language Processing 19 Maximum Likelihood Estimate

Fall 2001 EE669: Natural Language Processing 20 Comparison

Fall 2001 EE669: Natural Language Processing 21 The Zero Problem “Raw” n-gram language model estimate: –necessarily, there will be some zeros Often trigram model  2.16  10 14 parameters, data ~ 10 9 words –which are true zeros? optimal situation: even the least frequent trigram would be seen several times, in order to distinguish its probability vs. other trigrams optimal situation cannot happen, unfortunately –question: how much data would we need? Different kinds of zeros: p(w|h) = 0, p(w) = 0

Fall 2001 EE669: Natural Language Processing 22 Why do we need non-zero probabilities? Avoid infinite Cross Entropy: –happens when an event is found in the test data which has not been seen in training data Make the system more robust –low count estimates: they typically happen for “ detailed ” but relatively rare appearances –high count estimates: reliable but less “ detailed ”

Fall 2001 EE669: Natural Language Processing 23 Eliminating the Zero Probabilities: Smoothing Get new p’(w) (same  ): almost p(w) except for eliminating zeros Discount w for (some) p(w) > 0: new p’(w) < p(w)  w  discounted (p(w) - p’(w)) = D Distribute D to all w; p(w) = 0: new p’(w) > p(w) –possibly also to other w with low p(w) For some w (possibly): p’(w) = p(w) Make sure  w  p’(w) = 1 There are many ways of smoothing

Fall 2001 EE669: Natural Language Processing 24 Smoothing: an Example

Fall 2001 EE669: Natural Language Processing 25 Laplace’s Law: Smoothing by Adding 1 Laplace’s Law: –P LAP (w 1,..,w n )= (C(w 1,..,w n )+1) / (N+B) C(w 1,..,w n ) is the frequency of n-gram w 1,..,w n, N is the number of training instances, B is the number of bins training instances are divided into (vocabulary size) –Problem if B > C(W) (can be the case; even >> C(W)) –P LAP (w | h) = (C(h,w) + 1) / (C(h) + B) The idea is to give a little bit of the probability space to unseen events.

Fall 2001 EE669: Natural Language Processing 27 Laplace’s Law (original)

Fall 2001 EE669: Natural Language Processing 28 Laplace’s Law (adding one)

Fall 2001 EE669: Natural Language Processing 29 Laplace’s Law

Fall 2001 EE669: Natural Language Processing 30 Objections to Laplace’s Law For NLP applications that are very sparse, Laplace’s Law actually gives far too much of the probability space to unseen events. Worse at predicting the actual probabilities of bigrams with zero counts than other methods. Count variances are actually greater than the MLE.

Fall 2001 EE669: Natural Language Processing 31 Lidstone’s Law P = probability of specific n-gram C = count of that n-gram in training data N = total n-grams in training data B = number of “bins” (possible n-grams) = small positive number M.L.E: = 0 LaPlace’s Law: = 1 Jeffreys-Perks Law: = ½ P Lid (w | h) = (C(h,w) + ) / (C(h) + B )

Fall 2001 EE669: Natural Language Processing 32 Jeffreys-Perks Law

Fall 2001 EE669: Natural Language Processing 33 Objections to Lidstone’s Law Need an a priori way to determine. Predicts all unseen events to be equally likely. Gives probability estimates linear in the M.L.E. frequency.

Fall 2001 EE669: Natural Language Processing 35 Held-Out Estimator How much of the probability distribution should be reserved to allow for previously unseen events? Can validate choice by holding out part of the training data. How often do events seen (or not seen) in training data occur in validation data? Held out estimator by Jelinek and Mercer (1985)

Fall 2001 EE669: Natural Language Processing 36 Held Out Estimator For each n-gram, w 1,..,w n, compute C 1 (w 1,..,w n ) and C 2 (w 1,..,w n ), the frequencies of w 1,..,w n in training and held out data, respectively. –Let N r be the number of n-grams with frequency r in the training text. –Let T r be the total number of times that all n-grams that appeared r times in the training text appeared in the held out data, i.e., Then the average frequency of the frequency r n-grams is T r /N r An estimate for the probability of one of these n-gram is: P ho (w 1,..,w n )= (T r /N r )/N –where C(w 1,..,w n ) = r

Fall 2001 EE669: Natural Language Processing 37 Testing Models Divide data into training and testing sets. Training data: divide into normal training plus validation (smoothing) sets: around 10% for validation (fewer parameters typically) Testing data: distinguish between the “real” test set and a development set. –Use a development set prevent successive tweaking of the model to fit the test data –5 – 10% for testing –useful to test on multiple sets of test data in order to obtain the variance of results. –Are results (good or bad) just the result of chance? Use t-test

Fall 2001 EE669: Natural Language Processing 38 Cross-Validation Held out estimation is useful if there is a lot of data available. If not, it may be better to use each part of the data both as training data and held out data. –Deleted Estimation [Jelinek & Mercer, 1985] –Leave-One-Out [Ney et al., 1997]

Fall 2001 EE669: Natural Language Processing 39 Deleted Estimation Use data for both training and validation Divide training data into 2 parts (1)Train on A, validate on B (2)Train on B, validate on A Combine two models AB trainvalidate train Model 1 Model 2 Model 1Model 2 + Final Model

Fall 2001 EE669: Natural Language Processing 40 Cross-Validation Two estimates: Combined estimate: N r a = number of n-grams occurring r times in a-th part of training set T r ab = total number of those found in b-th part (arithmetic mean)

Fall 2001 EE669: Natural Language Processing 41 Good-Turing Estimation Intuition: re-estimate the amount of mass assigned to n-grams with low (or zero) counts using the number of n-grams with higher counts. For any n-gram that occurs r times, we should assume that it occurs r* times, where N r is the number of n-grams occurring precisely r times in the training data. To convert the count to a probability, we normalize the n-gram Wr with r counts as:

Fall 2001 EE669: Natural Language Processing 42 Good-Turing Estimation Note that N is equal to the original number of counts in the distribution. Makes the assumption of a binomial distribution, which works well for large amounts of data and a large vocabulary despite the fact that words and n-grams do not have that distribution.

Fall 2001 EE669: Natural Language Processing 43 Good-Turing Estimation Note that the estimate cannot be used if N r = 0; hence, it is necessary to smooth the N r values. The estimate can be written as: –If C(w 1,..,w n ) = r > 0, P GT (w 1,..,w n ) = r*/N where r*=((r+1)S(r+1))/S(r) and S(r) is a smoothed estimate of the expectation of N r. –If C(w 1,..,w n ) = 0, P GT (w 1,..,w n )  (N 1 /N 0 ) /N In practice, counts with a frequency greater than five are assumed reliable, as suggested by Katz. In practice, this method is not used by itself because it does not use lower order information to estimate probabilities of higher order n-grams.

Fall 2001 EE669: Natural Language Processing 44 Good-Turing Estimation N-grams with low counts are often treated as if they had a count of 0. In practice r* is used only for small counts; counts greater than k = 5 are assumed to be reliable: r* = r if r> k; otherwise:

Fall 2001 EE669: Natural Language Processing 45 Discounting Methods Absolute discounting: Decrease probability of each observed n-gram by subtracting a small constant when C(w 1, w 2, …, w n ) = r: Linear discounting: Decrease probability of each observed n-gram by multiplying by the same proportion when C(w 1, w 2, …, w n ) = r:

Fall 2001 EE669: Natural Language Processing 46 Combining Estimators: Overview If we have several models of how the history predicts what comes next, then we might wish to combine them in the hope of producing an even better model. Some combination methods: –Katz’s Back Off –Simple Linear Interpolation –General Linear Interpolation

Fall 2001 EE669: Natural Language Processing 47 Backoff Back off to lower order n-gram if we have no evidence for the higher order form. Trigram backoff:

Fall 2001 EE669: Natural Language Processing 48 Katz’s Back Off Model If the n-gram of concern has appeared more than k times, then an n-gram estimate is used but an amount of the MLE estimate gets discounted (it is reserved for unseen n-grams). If the n-gram occurred k times or less, then we will use an estimate from a shorter n-gram (back-off probability), normalized by the amount of probability remaining and the amount of data covered by this estimate. The process continues recursively.

Fall 2001 EE669: Natural Language Processing 49 Katz’s Back Off Model Katz used Good-Turing estimates when an n-gram appeared k or fewer times.

Fall 2001 EE669: Natural Language Processing 50 Problems with Backing-Off If bigram w 1 w 2 is common, but trigram w 1 w 2 w 3 is unseen, it may be a meaningful gap, rather than a gap due to chance and scarce data. –i.e., a “grammatical null” In that case, it may be inappropriate to back-off to lower-order probability.

Fall 2001 EE669: Natural Language Processing 51 Linear Interpolation One way of solving the sparseness in a trigram model is to mix that model with bigram and unigram models that suffer less from data sparseness. This can be done by linear interpolation (also called finite mixture models).

Fall 2001 EE669: Natural Language Processing 52 Simple Interpolated Smoothing Add information from less detailed distributions using =( 0, , ,  ): p’ (w i | w i-2,w i-1 ) =  p 3 (w i | w i-2,w i-1 ) +  p 2 (w i | w i-1 ) +  p 1 (w i ) + 0  /|V| Normalize: i > 0,  i=0..n i = 1 is sufficient ( 0 = 1 -  i=1..n i ) (n=3) Estimation using MLE: –fix the p 3, p 2, p 1 and |V| parameters as estimated from the training data –then find { i }that minimizes the cross entropy (maximizes probability of data): -(1/|D|)  i=1..|D| log 2 (p’ (w i |h i ))

Fall 2001 EE669: Natural Language Processing 53 CMU Language Model Toolkit Smoothing (Katz Back-off Plus Discount Method) Using discounting strategy: r* = r d r Multiple discounting methods:  Good-Turing discounting:  Linear discounting: d r = 1 – n 1 /R  Absolute discounting: d r = (r-b)/r  Witten-Bell discounting: d r (t) = R/(R+t)

Fall 2001 EE669: Natural Language Processing 54 Homework 5 Please collect 100 web news, and then build a bigram and a trigram language models, respectively. Please use at least three sentences to test your language models with two smoothing techniques, i.e., adding one and Good-Turing Estimation. Also, make some performance analysis to explain which model and smoothing method is better. Due day: March 31, 2014 Reference: slides 6, 7, 16, 25, 41, 44

Similar presentations