language modeling CS 685, Fall 2020 Introduction to Natural Language Processing http://people.cs.umass.edu/~miyyer/cs685/ Mohit Iyyer College of Information and Computer Sciences University of Massachusetts Amherst some slides from Dan Jurafsky and Richard Socher
46
Embed
New language modelingmiyyer/cs685/slides/01... · 2020. 8. 26. · some slides from Dan Jurafsky and Richard Socher. questions from last time ... This lecture: language modeling,
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
language modeling
CS 685, Fall 2020Introduction to Natural Language Processinghttp://people.cs.umass.edu/~miyyer/cs685/
Mohit IyyerCollege of Information and Computer Sciences
Some automatically generated sentences from a unigram model:
How can we generate text from a language model?
21
Dan*Jurafsky
Approximating'Shakespeare
10 CHAPTER 4 • N-GRAMS
Imagine all the words of English covering the probability space between 0 and 1,each word covering an interval proportional to its frequency. We choose a randomvalue between 0 and 1 and print the word whose interval includes this chosen value.We continue choosing random numbers and generating words until we randomlygenerate the sentence-final token </s>. We can use the same technique to generatebigrams by first generating a random bigram that starts with <s> (according to itsbigram probability), then choosing a random bigram to follow (again, according toits bigram probability), and so on.
To give an intuition for the increasing power of higher-order N-grams, Fig. 4.3shows random sentences generated from unigram, bigram, trigram, and 4-grammodels trained on Shakespeare’s works.
1–To him swallowed confess hear both. Which. Of save on trail for are ay device androte life have
gram –Hill he late speaks; or! a more to leg less first you enter
2–Why dost stand forth thy canopy, forsooth; he is this palpable hit the King Henry. Liveking. Follow.
gram –What means, sir. I confess she? then all sorts, he is trim, captain.
3–Fly, and will rid me these news of price. Therefore the sadness of parting, as they say,’tis done.
gram –This shall forbid it should be branded, if renown made it empty.
4–King Henry. What! I will go seek the traitor Gloucester. Exeunt some of the watch. Agreat banquet serv’d in;
gram –It cannot be but so.Figure 4.3 Eight sentences randomly generated from four N-grams computed from Shakespeare’s works. Allcharacters were mapped to lower-case and punctuation marks were treated as words. Output is hand-correctedfor capitalization to improve readability.
The longer the context on which we train the model, the more coherent the sen-tences. In the unigram sentences, there is no coherent relation between words or anysentence-final punctuation. The bigram sentences have some local word-to-wordcoherence (especially if we consider that punctuation counts as a word). The tri-gram and 4-gram sentences are beginning to look a lot like Shakespeare. Indeed, acareful investigation of the 4-gram sentences shows that they look a little too muchlike Shakespeare. The words It cannot be but so are directly from King John. Thisis because, not to put the knock on Shakespeare, his oeuvre is not very large ascorpora go (N = 884,647,V = 29,066), and our N-gram probability matrices areridiculously sparse. There are V 2 = 844,000,000 possible bigrams alone, and thenumber of possible 4-grams is V 4 = 7⇥1017. Thus, once the generator has chosenthe first 4-gram (It cannot be but), there are only five possible continuations (that, I,he, thou, and so); indeed, for many 4-grams, there is only one continuation.
To get an idea of the dependence of a grammar on its training set, let’s look at anN-gram grammar trained on a completely different corpus: the Wall Street Journal(WSJ) newspaper. Shakespeare and the Wall Street Journal are both English, sowe might expect some overlap between our N-grams for the two genres. Fig. 4.4shows sentences generated by unigram, bigram, and trigram grammars trained on40 million words from WSJ.
Compare these examples to the pseudo-Shakespeare in Fig. 4.3. While superfi-cially they both seem to model “English-like sentences”, there is obviously no over-
22
N-gram models
•We can extend to trigrams, 4-grams, 5-grams • In general this is an insufficient model of language • because language has long-distance dependencies:
“The computer which I had just put into the machine room on the fifth floor crashed.”
•But we can often get away with N-gram models
In the next video, we will look at some models that can theoretically handle
some of these longer-term dependencies
23
• The Maximum Likelihood Estimate (MLE)
- relative frequency based on the empirical counts on a training set
Estimating bigram probabilities
€
P(wi |wi−1) =count(wi−1,wi )count(wi−1)
€
P(wi |wi−1) =c(wi−1,wi )c(wi−1)
c — count
24
An example
<s> I am Sam </s> <s> Sam I am </s> <s> I do not like green eggs and ham </s>
€
P(wi |wi−1) =c(wi−1,wi )c(wi−1)
MLE
??????
25
An example
<s> I am Sam </s> <s> Sam I am </s> <s> I do not like green eggs and ham </s>
€
P(wi |wi−1) =c(wi−1,wi )c(wi−1)
MLE
26
An example
<s> I am Sam </s> <s> Sam I am </s> <s> I do not like green eggs and ham </s>
€
P(wi |wi−1) =c(wi−1,wi )c(wi−1)
MLE
Important terminology: a word type is a unique word in our vocabulary, while a
token is an occurrence of a word type in a dataset.
27
A bigger example: Berkeley Restaurant Project 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
28
Raw bigram counts
• Out of 9222 sentences
note: this is only a subset of the (much bigger) bigram
count table
29
Raw bigram probabilities• Normalize by unigrams:
• Result:
€
P(wi |wi−1) =c(wi−1,wi )c(wi−1)
MLE
30
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
these probabilities get super tiny when we have longer inputs w/ more infrequent words… how can we get around this?
logs to avoid underflow
31
log∏p(wi |wi−1) = ∑ log p(wi |wi−1)
Example with unigram model on a sentiment dataset:
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.
35
Evaluation: How good is our model?
• The goal isn’t to pound out fake sentences! • Obviously, generated sentences get “better” as we increase the model order •More precisely: using maximum likelihood estimators, higher order is always better likelihood on training set, but not test set