Presentation is loading. Please wait.

Presentation is loading. Please wait.

Page 1 Language Modeling. Page 2 Next Word Prediction From a NY Times story... Stocks... Stocks plunged this …. Stocks plunged this morning, despite a.

Similar presentations


Presentation on theme: "Page 1 Language Modeling. Page 2 Next Word Prediction From a NY Times story... Stocks... Stocks plunged this …. Stocks plunged this morning, despite a."— Presentation transcript:

1 Page 1 Language Modeling

2 Page 2 Next Word Prediction From a NY Times story... Stocks... Stocks plunged this …. Stocks plunged this morning, despite a cut in interest rates Stocks plunged this morning, despite a cut in interest rates by the Federal Reserve, as Wall... Stocks plunged this morning, despite a cut in interest rates by the Federal Reserve, as Wall Street began

3 Page 3 Stocks plunged this morning, despite a cut in interest rates by the Federal Reserve, as Wall Street began trading for the first time since last … Stocks plunged this morning, despite a cut in interest rates by the Federal Reserve, as Wall Street began trading for the first time since last Tuesday's terrorist attacks.

4 Page 4 Human Word Prediction Clearly, at least some of us have the ability to predict future words in an utterance. How? Domain knowledge Syntactic knowledge Lexical knowledge

5 Page 5 Claim A useful part of the knowledge needed to allow Word Prediction can be captured using simple statistical techniques In particular, we'll rely on the notion of the probability of a sequence (a phrase, a sentence)

6 Page 6 N-grams N-grams  ‘N-words’ ? It’s ‘N’ consecutive words that one can find in a given corpus or set of documents ? ‘N-gram’ model is a probabilistic model that computes probability of Nth word occurring after seeing ‘N-1’ words – also known as language model in speech recognition

7 Page 7 Some Interesting Facts The most frequent 250 words takes account of approximately 50% of all tokens in any random text ‘the’ is usually the most frequent word ‘the’ occurs 69,971 times in 1 million word Brown corpus (7%) The top 20 words in 1 year of Wall Street Journal is The, of, to, in, and, for, that, is, on, it, by, with, as, at, said, mister, from, its, are, he, million

8 Page 8 N-Gram Models of Language Use the previous N-1 words in a sequence to predict the next word Language Model (LM) unigrams, bigrams, trigrams,… How do we train these models? Very large corpora

9 Page 9 Applications Why do we want to predict a word, given some preceding words? Rank the likelihood of sequences containing various alternative hypotheses, e.g. for ASR Theatre owners say popcorn/unicorn sales have doubled... Assess the likelihood/goodness of a sentence, e.g. for text generation or machine translation The doctor recommended a cat scan.

10 Page 10 Counting Words in Corpora What is a word? e.g., are cat and cats the same word? September and Sept? zero and oh? Is _ a word? * ? ‘(‘ ? How many words are there in don’t ? Gonna ?

11 Page 11 Terminology Sentence: unit of written language Utterance: unit of spoken language Types: number of distinct words in a corpus (vocabulary size) Tokens: total number of words

12 Page 12 Corpora Corpora are collections of text and speech Brown Corpus Wall Street Journal AP news DARPA/NIST text/speech corpora (Call Home, ATIS, switchboard, Broadcast News, TDT, Communicator) TRAINS, Radio News

13 Page 13 Simple N-Grams Assume a language has V word types in its lexicon, how likely is word x to follow word y? Simplest model of word probability: 1/V Alternative 1: estimate likelihood of x occurring in new text based on its general frequency of occurrence estimated from a corpus (unigram probability) popcorn is more likely to occur than unicorn Alternative 2: condition the likelihood of x occurring in the context of previous words (bigrams, trigrams,…) mythical unicorn is more likely than mythical popcorn

14 Page 14 Computing the Probability of a Word Sequence Conditional Probability P(A 1,A 2 ) = P(A 1 ) P(A 2 |A 1 ) The Chain Rule generalizes to multiple events P(A 1, …,A n ) = P(A 1 ) P(A 2 |A 1 ) P(A 3 |A 1,A 2 )…P(A n |A 1 …A n-1 ) Compute the product of component conditional probabilities? P(the mythical unicorn) = P(the) P(mythical|the) P(unicorn|the mythical) The longer the sequence, the less likely we are to find it in a training corpus P(Most biologists and folklore specialists believe that in fact the mythical unicorn horns derived from the narwhal) Solution: approximate using n-grams

15 Page 15 Bigram Model Approximate by P(unicorn|the mythical) by P(unicorn|mythical) Markov assumption: the probability of a word depends only on the probability of a limited history Generalization: the probability of a word depends only on the probability of the n previous words trigrams, 4-grams, … the higher n is, the more data needed to train backoff models

16 Page 16 Using N-Grams For N-gram models  P(w n-1,w n ) = P(w n | w n-1 ) P(w n-1 ) By the Chain Rule we can decompose a joint probability, e.g. P(w 1,w 2,w 3 ) P(w 1,w 2,...,w n ) = P(w 1 |w 2,w 3,...,w n ) P(w 2 |w 3,...,w n ) … P(w n-1 |w n ) P(w n ) For bigrams then, the probability of a sequence is just the product of the conditional probabilities of its bigrams P(the,mythical,unicorn) = P(unicorn|mythical) P(mythical|the) P(the| )

17 Page 17 Training and Testing N-Gram probabilities come from a training corpus overly narrow corpus: probabilities don't generalize overly general corpus: probabilities don't reflect task or domain A separate test corpus is used to evaluate the model, typically using standard metrics held out test set; development test set cross validation results tested for statistical significance

18 Page 18 A Simple Example P(I want to eat Chinese food) = P(I | ) P(want | I) P(to | want) P(eat | to) P(Chinese | eat) P(food | Chinese)

19 Page 19 A Bigram Grammar Fragment from BERP.001Eat British.03Eat today.007Eat dessert.04Eat Indian.01Eat tomorrow.04Eat a.02Eat Mexican.04Eat at.02Eat Chinese.05Eat dinner.02Eat in.06Eat lunch.03Eat breakfast.06Eat some.03Eat Thai.16Eat on

20 Page 20.01British lunch.05Want a.01British cuisine.65Want to.15British restaurant.04I have.60British food.08I don’t.02To be.29I would.09To spend.32I want.14To have.02 I’m.26To eat.04 Tell.01Want Thai.06 I’d.04Want some.25 I

21 Page 21 P(I want to eat British food) = P(I| ) P(want|I) P(to|want) P(eat|to) P(British|eat) P(food|British) =.25*.32*.65*.26*.001*.60 =.000080 vs. I want to eat Chinese food =.00015 Probabilities seem to capture ``syntactic'' facts, ``world knowledge'' eat is often followed by an NP British food is not too popular N-gram models can be trained by counting and normalization

22 Page 22 BERP Bigram Counts 0100004Lunch 000017019Food 112000002Chinese 522190200Eat 12038601003To 686078603Want 00013010878I lunchFoodChineseEatToWantI

23 Page 23 BERP Bigram Probabilities Normalization: divide each row's counts by appropriate unigram counts for w n-1 Computing the bigram probability of I I C(I,I)/C(all I) p (I|I) = 8 / 3437 =.0023 Maximum Likelihood Estimation (MLE): relative frequency of e.g. 4591506213938325612153437 LunchFoodChineseEatToWantI

24 Page 24 What do we learn about the language? What's being captured with... P(want | I) =.32 P(to | want) =.65 P(eat | to) =.26 P(food | Chinese) =.56 P(lunch | eat) =.055 What about... P(I | I) =.0023 P(I | want) =.0025 P(I | food) =.013

25 Page 25 P(I | I) =.0023 I I I I want P(I | want) =.0025 I want I want P(I | food) =.013 the kind of food I want is...

26 Page 26 Approximating Shakespeare As we increase the value of N, the accuracy of the n-gram model increases, since choice of next word becomes increasingly constrained Generating sentences with random unigrams... Every enter now severally so, let Hill he late speaks; or! a more to leg less first you enter With bigrams... What means, sir. I confess she? then all sorts, he is trim, captain. Why dost stand forth thy canopy, forsooth; he is this palpable hit the King Henry.

27 Page 27 Trigrams Sweet prince, Falstaff shall die. This shall forbid it should be branded, if renown made it empty. Quadrigrams What! I will go seek the traitor Gloucester. Will you not tell me who I am?

28 Page 28 There are 884,647 tokens, with 29,066 word form types, in about a one million word Shakespeare corpus Shakespeare produced 300,000 bigram types out of 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

29 Page 29 N-Gram Training Sensitivity If we repeated the Shakespeare experiment but trained our n- grams on a Wall Street Journal corpus, what would we get? This has major implications for corpus selection or design

30 Page 30 Some Useful Empirical Observations A small number of events occur with high frequency A large number of events occur with low frequency You can quickly collect statistics on the high frequency events You might have to wait an arbitrarily long time to get valid statistics on low frequency events Some of the zeroes in the table are really zeros But others are simply low frequency events you haven't seen yet. How to address?

31 Page 31 Smoothing Techniques Every n-gram training matrix is sparse, even for very large corpora Solution: estimate the likelihood of unseen n-grams Problems: how do you adjust the rest of the corpus to accommodate these ‘phantom’ n-grams?

32 Page 32 Add-one Smoothing For unigrams: Add 1 to every word (type) count Normalize by N (tokens) /(N (tokens) +V (types)) Smoothed count (adjusted for additions to N) is Normalize by N to get the new unigram probability: Add delta smoothing

33 Page 33 Basic Idea: Use Count of things you have seen once to estimate the things you haven’t seen i.e. Estimate the probability of seeing something for the first time View training corpus as series of events, one for each token (N) and one for each new type (T). Probability of seeing something new is This probability mass will assigned to unseen cases Witten-Bell Discounting

34 Page 34 Witten-Bell (cont…) A zero ngram is just an ngram you haven’t seen yet…but every ngram in the corpus was unseen once…so... How many times did we see an ngram for the first time? Once for each ngram type (T) Est. total probability of unseen bigrams as Each of Z unseen cases will be assigned an equal portion probability mass - T/N+T Seen cases will have probabilities discounted as

35 Page 35 Witten-Bell (cont…) But for bigrams we can condition on the first word: Instead of trying to find what is the probability of finding a new bigram we can ask what is the probability of finding a new bigram that starts with the given word w Then unseen portion will be assigned probability mass And for seen cases we discount as follows:

36 Page 36 Distributing Among the Zeros If a bigram “wx wi” has a zero count Number of bigrams starting with wx that were not seen Actual frequency of bigrams beginning with wx Number of bigram types starting with wx

37 Page 37 Re-estimate amount of probability mass for zero (or low count) ngrams by looking at ngrams with higher counts For any n-gram that occurs ‘c’ times assume it occurs c* times, where Nc is the n number n-grams occurring c times Estimate a smoothed count E.g. N 0 ’s adjusted count is a function of the count of ngrams that occur once, N 1 Probability mass assigned to unseen cases works out to be n1/N Counts for bigrams that never occurred (c0) will be just count of bigrams that occurred once by count of bigrams that never occurred. How do we know count of bigrams that never occurred? Good-Turing Discounting

38 Page 38 Backoff methods (e.g. Katz ‘87) If you don’t have a count for ‘n-gram’ backoff to weighted ‘n-1’ gram Alphas needed to make a proper probability distribution (If we backoff when probability is zero, we are adding extra probability mass) For e.g. a trigram model Compute unigram, bigram and trigram probabilities In use: – Where trigram unavailable back off to bigram if available, o.w. unigram probability

39 Page 39 Summary N-gram models are approximation to the correct model given by chain rule N-gram probabilities can be used to estimate the likelihood Of a word occurring in a context (N-1) N-gram models suffer from sparse data Smoothing techniques deal with problems of unseen words in corpus

40 Page 40 Statistical Techniques in NLP

41 Page 41 Example: Text Summarization Let’s say we are doing text summarization using sentence extraction Someone gave us a document where each sentence is scored between 1 to 20 and Extracting top 10% scoring sentence can potentially be a summary We have 100 such documents Problem: Use a machine learning technique to build a model that predicts the score of sentences in a new document

42 Page 42 Example: Text Summarization CORPUS 100 documents Each sentence scored (1 to 20) Machine Learning Corpus- Based Model

43 Page 43 Regression for our Text Summarization Problem For simplicity let’s assume all of documents are of equal length ‘N’ Our training dataset (100 documents) We have 100xN sentences in total 100xN scores, let’s call these scores y’s For each sentence we know it’s position in the document. Let’s call these sentence positions x’s so we get 100xN sentence positions

44 Page 44 Regression as Function Approximation Using our training data ‘X’ we must predict a score for each sentence in a new document We can do such prediction by finding a function y=f(x) that fits the training data well Need to find out what f(x) to use Need to compute how good the training data fits with the chosen f(x)

45 Page 45 Empirical Risk Minimization Need to compute how good the data fits the chosen function f(x) We can define a loss function L(y, f(x)) Find average loss: Simple Loss Function:

46 Page 46 Linear Regression We can choose f(x) to be linear, polynomial, exponential or any other class of functions If we use linear function we get linear regression, widely used modeling technique Where m is slope and c is intercept on y axis

47 Page 47 Text Summarization with Linear Regression We had Nx100 (x i, y i ) pairs where x was sentence position and y was score for the sentence Let’s look at a sample plot Sentence position Score (x i, y i ) (x i, f(x i )) Error (y i - f(x i )) The line we have found by minimizing average squared error is our model (summarizer) Given any new ‘x’ (i.e. sentence position of a new document) we can predict ‘y’ – score that represents it’s significance to summary

48 Page 48 Naïve Bayes Classifier Let’s assume instead of score between 1 to 20 for each sentence, all the sentence have been classified into two classes – IN SUMMARY (1), NOT IN SUMMARY (0) Now, given a document we want to predict the class (1) or (0) for each sentence in the document. All the sentence in class (1) should be included in the summary This is a binary classification problem

49 Page 49 Intuitive Example of Naïve Bayes Classifier Let us assume the objects can be classified into RED or GREEN We have a corpus with objected manually labeled as RED or GREEN We need to figure out if the test object is RED or GREEN Number of green objects is twice that of red. So, it is reasonable to assume that a new object (not observed yet) is twice likely to be green than red (prior probability) All figures in the given example are from electronic textbook StatSoft

50 Page 50 Example (RED or GREEN classification) There are total of 60 objects with 40 GREEN and 20 RED Prior probability of GREEN  # of GREEN objects Total # of objects Prior probability of RED  # of RED objects Total # of objects Prior probability of GREEN  40 60 Prior probability of RED  20 60

51 Page 51 Example (RED or GREEN classification) Looking at the picture it is reasonable to assume that likelihood of object being RED or GREEN can be computed by number of RED or GREEN objects in the vicinity Need to figure out if the test object is GREEN or RED Likelihood of X being GREEN  # of GREEN objects in vicinity of X Total # of GREEN Likelihood of X being RED  # of RED objects in vicinity of X Total # of RED

52 Page 52 Example (RED or GREEN classification) Looking at the picture it is reasonable to assume that likelihood of object being RED or GREEN can be computed by number of RED or GREEN objects in the vicinity Need to figure out if the test object is GREEN or RED Likelihood of X being GREEN  # of GREEN objects in vicinity of X Total # of GREEN Likelihood of X being RED  # of RED objects in vicinity of X Total # of RED  1/40  3/20

53 Page 53 Example (RED or GREEN classification) Posterior probability of X being GREEN = Prior probability of GREEN x Likelihood of X given GREEN = 40/60 x 1/40 = 1/60 Posterior probability of X being RED = Prior probability of RED x Likelihood of X given RED = 20/60 x 3/40 = 1/40 Hence, we classify our new object X as RED using our Bayesian classifier model.

54 Page 54 Document Vectors Documents can be represented in different types of vectors: binary vector, multinomial vector, feature vector Binary Vector: For each dimension, 1 if the word type is in the document and 0 otherwise Multinomial Vector: For each dimension, count # of times word type appears in the document Feature Vector: Extract various features from the document and represent them in a vector. Dimension equals the number of features

55 Page 55 Example of a multinomial document vector Screening of the critically acclaimed film NumaFung Reserved tickets can be picked up on the day of the show at the box office at Arledge Cinema. Tickets will not be reserved if not paid for in advance. 4 THE 2 TICKETS 2 RESERVED 2 OF 2 NOT 2 BE 2 AT 1 WILL 1 UP 1 SHOW 1 SCREENING 1 PICKED 1 PAID 1 ON 1 OFFICE 1 NUMAFUNG 1 IN 1 IF 1 FOR 1 FILM 1 DAY 1 CRITICALLY 1 CINEMA 1 CAN 1 BOX 1 ARLEDGE 1 ADVANCE 1 ACCLAIMED 42222221111111111111111111114222222111111111111111111111

56 Page 56 Example of a multinomial document vector 4 THE 2 SEATS 2 RESERVED 2 OF 2 NOT 2 BE 2 AT 1 WILL 1 UP 1 SHOW 1 SHOWING 1 PICKED 1 PAID 1 ON 1 OFFICE 1 VOLCANO 1 IN 1 IF 1 FOR 1 FILM 1 DAY 1 CRITICALLY 1 CINEMA 1 CAN 422222211111111111111111422222211111111111111111 But if you want to compare previous vector to this new vector we cannot do any computation like cosine measure with this vector. Why? ->Different dimensions Hence, if you have multiple documents you need to first find set of all words (dimensions) in the set of documents. Values for all the words that does not appear in the given document is 0

57 Page 57 Feature Vectors Instead of just using words to represent documents, we can also extract features and use them to represent the document For example, let’s classify History and Physics documents We are given ‘N’ documents which are labeled as ‘HISTORY’ or ‘PHYSICS’ We can extract features like document length (LN), number of nouns (NN), number of verbs (VB), number of person names (PN), number of place (CN) names, number of organization names (ON), number of sentences (NS), number of pronouns (PNN)

58 Page 58 Feature Vectors Extracting such features you get a feature vector of length ‘K’ where ‘K’ is the number of dimensions (features) for each document Length Noun Count Verb Count # Person Name # Place Name # Orgzn Name. 780 45 3 4 7 67 23. 420 12 56 0 7 3 4. … 940 36 3 8 6 55 21. HISTPHYSICSHIST

59 Page 59 Feature Vectors Length Noun Count Verb Count # Person Name # Place Name # Orgzn Name. 780 45 3 4 7 67 23. 420 12 56 0 7 3 4. … 940 36 3 8 6 55 21. HISTPHYSICSHIST After such feature vector representation we can use various learning algorithms including Naïve Bayes, Decision Trees, to classify the new document using the training document set 940 36 3 8 6 55 21. HIST


Download ppt "Page 1 Language Modeling. Page 2 Next Word Prediction From a NY Times story... Stocks... Stocks plunged this …. Stocks plunged this morning, despite a."

Similar presentations


Ads by Google