+ All Categories
Home > Documents > Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf ·...

Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf ·...

Date post: 22-May-2020
Category:
Upload: others
View: 14 times
Download: 0 times
Share this document with a friend
73
Language Modeling Many slides from Dan Jurafsky
Transcript
Page 1: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

LanguageModeling

ManyslidesfromDanJurafsky

Page 2: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

ProbabilisticLanguageModels

•Today’sgoal:assignaprobability toasentence•MachineTranslation:• P(highwindstonite)>P(largewindstonite)

• SpellCorrection• Theofficeisaboutfifteenminuets frommyhouse• P(aboutfifteenminutes from)>P(aboutfifteenminuets from)

• SpeechRecognition• P(Isawavan)>>P(eyesaweofan)

• +Summarization,question-answering,etc.,etc.!!

Why?

Page 3: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

ProbabilisticLanguageModeling

•Goal:computetheprobabilityofasentenceorsequenceofwords:

P(W)=P(w1,w2,w3,w4,w5…wn)

•Relatedtask:probability ofanupcomingword:P(w5|w1,w2,w3,w4)

•Amodelthatcomputeseitherofthese:P(W)orP(wn|w1,w2…wn-1) iscalledalanguagemodel.

• Better:thegrammarButlanguagemodelorLMisstandard

Page 4: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

HowtocomputeP(W)

• Howtocomputethisjointprobability:

•P(its,water,is,so,transparent, that)

• Intuition:let’srelyontheChainRuleofProbability

Page 5: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

Reminder:TheChainRule

•Recallthedefinitionofconditionalprobabilitiesp(B|A)=P(A,B)/P(A) Rewriting:P(A,B)=P(A)P(B|A)

•Morevariables:P(A,B,C,D)=P(A)P(B|A)P(C|A,B)P(D|A,B,C)

•TheChainRuleinGeneralP(x1,x2,x3,…,xn)=P(x1)P(x2|x1)P(x3|x1,x2)…P(xn|x1,…,xn-1)

Page 6: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

TheChainRuleappliedtocomputejointprobabilityofwordsinsentence

P(“itswaterissotransparent”) =P(its)× P(water|its)× P(is|its water)

× P(so|its wateris)× P(transparent|its waterisso)

P(w1w2…wn ) = P(wi |w1w2…wi−1)i∏

Page 7: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

Howtoestimatetheseprobabilities

• Couldwejustcountanddivide?

• No!Toomanypossiblesentences!•We’llneverseeenoughdataforestimatingthese

P(the | its water is so transparent that) =

Count(its water is so transparent that the)Count(its water is so transparent that)

Page 8: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

MarkovAssumption

•Simplifyingassumption:

•Ormaybe

P(the | its water is so transparent that) ≈ P(the | that)

P(the | its water is so transparent that) ≈ P(the | transparent that)

AndreiMarkov

Page 9: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

MarkovAssumption

•Inotherwords,weapproximateeachcomponentintheproduct

P(w1w2…wn ) ≈ P(wi |wi−k…wi−1)i∏

P(wi |w1w2…wi−1) ≈ P(wi |wi−k…wi−1)

Page 10: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

Simplestcase:Unigrammodel

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

Someautomaticallygeneratedsentencesfromaunigrammodel

P(w1w2…wn ) ≈ P(wi)i∏

Page 11: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

Conditiononthepreviousword:

Bigrammodel

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, yen

outside, new, car, parking, lot, of, the, agreement, reached

this, would, be, a, record, november

P(wi |w1w2…wi−1) ≈ P(wi |wi−1)

Page 12: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

N-grammodels

•Wecanextendtotrigrams,4-grams,5-grams• Ingeneralthisisaninsufficientmodeloflanguage• becauselanguagehaslong-distancedependencies:

“ThecomputerwhichIhadjustputintothemachineroomonthefifthfloorcrashed.”

•ButwecanoftengetawaywithN-grammodels

Page 13: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

IntroductiontoN-grams

LanguageModeling

Page 14: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

EstimatingN-gramProbabilities

LanguageModeling

Page 15: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

Estimatingbigramprobabilities

• TheMaximumLikelihoodEstimate

P(wi |wi−1) =count(wi−1,wi)count(wi−1)

P(wi |wi−1) =c(wi−1,wi)c(wi−1)

Page 16: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

Anexample

<s>IamSam</s><s>SamIam</s><s>Idonotlikegreeneggsandham</s>

P(wi |wi−1) =c(wi−1,wi)c(wi−1)

Page 17: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

Moreexamples:BerkeleyRestaurantProjectsentences

• canyoutellmeaboutanygoodcantonese restaurantscloseby•midpricedthai foodiswhati’m lookingfor• tellmeaboutchezpanisse• canyougivemealistingofthekindsoffoodthatareavailable• i’m lookingforagoodplacetoeatbreakfast•wheniscaffe venezia openduringtheday

Page 18: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

Rawbigramcounts

• Outof9222sentences

Page 19: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

Rawbigramprobabilities

• Normalizebyunigrams:

• Result:

Page 20: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

Bigramestimatesofsentenceprobabilities

P(<s>Iwantenglish food</s>)=P(I|<s>) × P(want|I)× P(english|want) × P(food|english) × P(</s>|food)

=.000031

Page 21: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

Whatkindsofknowledge?

•P(english|want) =.0011•P(chinese|want) =.0065•P(to|want)=.66•P(eat|to)=.28•P(food|to)=0•P(want|spend)=0•P(i |<s>)=.25

Page 22: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

PracticalIssues

•Wedoeverythinginlogspace•Avoidunderflow•(alsoaddingisfasterthanmultiplying)

log(p1 × p2 × p3 × p4 ) = log p1 + log p2 + log p3 + log p4

Page 23: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

LanguageModelingToolkits

•SRILM•http://www.speech.sri.com/projects/srilm/•KenLM•https://kheafield.com/code/kenlm/

Page 24: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

GoogleN-GramRelease,August2006

Page 25: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

GoogleN-GramRelease

• 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

Page 26: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

GoogleBookN-grams

• http://ngrams.googlelabs.com/

Page 27: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

EstimatingN-gramProbabilities

LanguageModeling

Page 28: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

EvaluationandPerplexity

LanguageModeling

Page 29: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

Evaluation:Howgoodisourmodel?

• Doesourlanguagemodelprefergoodsentencestobadones?• Assignhigherprobabilityto“real”or“frequentlyobserved”sentences

• Than“ungrammatical”or“rarelyobserved”sentences?

• Wetrainparametersofourmodelonatrainingset.• Wetestthemodel’sperformanceondatawehaven’tseen.• Atestsetisanunseendatasetthatisdifferentfromourtrainingset,totallyunused.• Anevaluationmetrictellsushowwellourmodeldoesonthetestset.

Page 30: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

Trainingonthetestset

• Wecan’tallowtestsentencesintothetrainingset• Wewillassignitanartificiallyhighprobabilitywhenwesetitinthetestset• “Trainingonthetestset”• Badscience!• Andviolatesthehonorcode

30

Page 31: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

ExtrinsicevaluationofN-grammodels

•BestevaluationforcomparingmodelsAandB• Puteachmodelinatask• spellingcorrector,speechrecognizer,MTsystem

• Runthetask,getanaccuracyforAandforB• Howmanymisspelledwordscorrectedproperly• Howmanywordstranslatedcorrectly

• CompareaccuracyforAandB

Page 32: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

Difficultyofextrinsic(in-vivo)evaluationofN-grammodels•Extrinsicevaluation• Time-consuming;cantakedaysorweeks

•So• Sometimesuseintrinsic evaluation:perplexity• Badapproximation• unlessthetestdatalooksjust likethetrainingdata• Sogenerallyonlyusefulinpilotexperiments

• Butishelpfultothinkabout.

Page 33: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

IntuitionofPerplexity

• TheShannonGame:• Howwellcanwepredictthenextword?

• Unigramsareterribleatthisgame.(Why?)

• Abettermodelofatext• isonewhichassignsahigherprobabilitytothewordthatactuallyoccurs

Ialwaysorderpizzawithcheeseand____

The33rd PresidentoftheUSwas____

Isawa____

mushrooms 0.1

pepperoni 0.1

anchovies 0.01

….

fried rice 0.0001

….

and 1e-100

Page 34: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

Perplexity

Perplexityistheinverseprobabilityofthetestset,normalizedbythenumberofwords:

Chainrule:

Forbigrams:

Minimizingperplexityisthesameasmaximizingprobability

Thebestlanguagemodelisonethatbestpredictsanunseentestset• GivesthehighestP(sentence)

PP(W ) = P(w1w2...wN )−

1N

=1

P(w1w2...wN )N

Page 35: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

Perplexityasbranchingfactor

• Let’ssupposeasentenceconsistingofrandomdigits• WhatistheperplexityofthissentenceaccordingtoamodelthatassignP=1/10toeachdigit?

Page 36: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

Lowerperplexity=bettermodel

•Training38millionwords,test1.5millionwords,WSJ

N-gramOrder

Unigram Bigram Trigram

Perplexity 962 170 109

Page 37: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

EvaluationandPerplexity

LanguageModeling

Page 38: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

Generalizationandzeros

LanguageModeling

Page 39: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

TheShannonVisualizationMethod

• Choosearandombigram(<s>,w)accordingtoitsprobability

• Nowchoosearandombigram(w,x)accordingtoitsprobability• Andsoonuntilwechoose</s>• Thenstringthewordstogether

<s> II wantwant to

to eateat Chinese

Chinese foodfood </s>

I want to eat Chinese food

Page 40: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

ApproximatingShakespeare

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-

Page 41: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

Shakespeareascorpus

•N=884,647tokens,V=29,066•Shakespeareproduced300,000bigramtypesoutofV2=844millionpossiblebigrams.•So99.96%ofthepossiblebigramswereneverseen(havezeroentries inthetable)

•Quadrigramsworse:What'scomingoutlookslikeShakespearebecauseitis Shakespeare

Page 42: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

Thewallstreetjournalisnotshakespeare(nooffense) 4.3 • GENERALIZATION AND ZEROS 11

1 Months the my and issue of year foreign new exchange’s septemberwere recession exchange new endorsed a acquire to six executives

gram

2Last December through the way to preserve the Hudson corporation N.B. E. C. Taylor would seem to complete the major central planners one

gram point five percent of U. S. E. has already old M. X. corporation of livingon information such as more frequently fishing to keep her

3They also point to ninety nine point six billion dollars from two hundredfour oh six three percent of the rates of interest stores as Mexico and

gram Brazil on market conditionsFigure 4.4 Three sentences randomly generated from three N-gram models computed from40 million words of the Wall Street Journal, lower-casing all characters and treating punctua-tion as words. Output was then hand-corrected for capitalization to improve readability.

lap whatsoever in possible sentences, and little if any overlap even in small phrases.This stark difference tells us that statistical models are likely to be pretty useless aspredictors if the training sets and the test sets are as different as Shakespeare andWSJ.

How should we deal with this problem when we build N-gram models? One wayis to be sure to use a training corpus that has a similar genre to whatever task we aretrying to accomplish. To build a language model for translating legal documents,we need a training corpus of legal documents. To build a language model for aquestion-answering system, we need a training corpus of questions.

Matching genres is still not sufficient. Our models may still be subject to theproblem of sparsity. For any N-gram that occurred a sufficient number of times,we might have a good estimate of its probability. But because any corpus is limited,some perfectly acceptable English word sequences are bound to be missing from it.That is, we’ll have a many cases of putative “zero probability N-grams” that shouldreally have some non-zero probability. Consider the words that follow the bigramdenied the in the WSJ Treebank3 corpus, together with their counts:

denied the allegations: 5denied the speculation: 2denied the rumors: 1denied the report: 1

But suppose our test set has phrases like:

denied the offerdenied the loan

Our model will incorrectly estimate that the P(offer|denied the) is 0!These zeros— things things that don’t ever occur in the training set but do occurzeros

in the test set—are a problem for two reasons. First, they means we are underes-timating the probability of all sorts of words that might occur, which will hurt theperformance of any application we want to run on this data.

Second, if the probability of any word in the testset is 0, the entire probability ofthe test set is 0. But the definition of perplexity is based on the inverse probabilityof the test set. If some words have zero probability, we can’t compute perplexity atall, since we can’t divide by 0!

Page 43: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

Zeros

•Trainingset:…deniedtheallegations…deniedthereports…deniedtheclaims…deniedtherequest

P(“offer”|deniedthe)=0

• Testset…deniedtheoffer…deniedtheloan

Page 44: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

Zeroprobabilitybigrams

• Bigramswithzeroprobability• meanthatwewillassign0probabilitytothetestset!

• Andhencewecannotcomputeperplexity(can’tdivideby0)!

Page 45: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

Generalizationandzeros

LanguageModeling

Page 46: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

Smoothing:Add-one(Laplace)smoothing

LanguageModeling

Page 47: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

The intuition of smoothing (from Dan Klein)

• Whenwehavesparsestatistics:

• Stealprobabilitymasstogeneralizebetter

P(w|denied the)3allegations2reports1claims1request7total

P(w|denied the)2.5allegations1.5reports0.5claims0.5request2other7total

allegatio

ns

repo

rts

claims

attack

request

man

outcome

allegatio

ns

attack

man

outcome

…allegatio

ns

repo

rts

claims

requ

est

Page 48: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

Add-oneestimation

•AlsocalledLaplacesmoothing• Pretendwesaweachwordonemoretimethanwedid• Justaddonetoallthecounts!

•MLEestimate:

•Add-1estimate:

PMLE (wi |wi−1) =c(wi−1,wi )c(wi−1)

PAdd−1(wi |wi−1) =c(wi−1,wi )+1c(wi−1)+V

Page 49: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

Berkeley Restaurant Corpus: Laplace smoothed bigram counts

Page 50: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

Laplace-smoothed bigrams

Page 51: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

Reconstituted counts

Page 52: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

Compare with raw bigram counts

Page 53: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

Add-1estimationisabluntinstrument

• Soadd-1isn’tusedforN-grams:• We’llseebettermethods

• Butadd-1isusedtosmoothotherNLPmodels• Fortextclassification• Indomainswherethenumberofzerosisn’tsohuge.

Page 54: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

Smoothing:Add-one(Laplace)smoothing

LanguageModeling

Page 55: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

Interpolation,Backoff,andWeb-ScaleLMs

LanguageModeling

Page 56: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

Backoff and Interpolation• Sometimesithelpstouseless context• Conditiononlesscontextforcontextsyouhaven’tlearnedmuchabout

• Backoff:• usetrigramifyouhavegoodevidence,• otherwisebigram,otherwiseunigram

• Interpolation:• mixunigram,bigram,trigram

• Interpolationworksbetter

Page 57: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

LinearInterpolation

•Simpleinterpolation

• Lambdasconditionaloncontext:

4.4 • SMOOTHING 15

The sharp change in counts and probabilities occurs because too much probabil-ity mass is moved to all the zeros.

4.4.2 Add-k smoothingOne alternative to add-one smoothing is to move a bit less of the probability massfrom the seen to the unseen events. Instead of adding 1 to each count, we add a frac-tional count k (.5? .05? .01?). This algorithm is therefore called add-k smoothing.add-k

P⇤Add-k(wn|wn�1) =

C(wn�1wn)+ kC(wn�1)+ kV

(4.23)

Add-k smoothing requires that we have a method for choosing k; this can bedone, for example, by optimizing on a devset. Although add-k is is useful for sometasks (including text classification), it turns out that it still doesn’t work well forlanguage modeling, generating counts with poor variances and often inappropriatediscounts (Gale and Church, 1994).

4.4.3 Backoff and InterpolationThe discounting we have been discussing so far can help solve the problem of zerofrequency N-grams. But there is an additional source of knowledge we can drawon. If we are trying to compute P(wn|wn�2wn�1) but we have no examples of aparticular trigram wn�2wn�1wn, we can instead estimate its probability by usingthe bigram probability P(wn|wn�1). Similarly, if we don’t have counts to computeP(wn|wn�1), we can look to the unigram P(wn).

In other words, sometimes using less context is a good thing, helping to general-ize more for contexts that the model hasn’t learned much about. There are two waysto use this N-gram “hierarchy”. In backoff, we use the trigram if the evidence isbackoff

sufficient, otherwise we use the bigram, otherwise the unigram. In other words, weonly “back off” to a lower-order N-gram if we have zero evidence for a higher-orderN-gram. By contrast, in interpolation, we always mix the probability estimatesinterpolation

from all the N-gram estimators, weighing and combining the trigram, bigram, andunigram counts.

In simple linear interpolation, we combine different order N-grams by linearlyinterpolating all the models. Thus, we estimate the trigram probability P(wn|wn�2wn�1)by mixing together the unigram, bigram, and trigram probabilities, each weightedby a l :

P̂(wn|wn�2wn�1) = l1P(wn|wn�2wn�1)

+l2P(wn|wn�1)

+l3P(wn) (4.24)

such that the l s sum to 1: X

i

li = 1 (4.25)

In a slightly more sophisticated version of linear interpolation, each l weight iscomputed in a more sophisticated way, by conditioning on the context. This way,if we have particularly accurate counts for a particular bigram, we assume that thecounts of the trigrams based on this bigram will be more trustworthy, so we canmake the l s for those trigrams higher and thus give that trigram more weight in

4.4 • SMOOTHING 15

The sharp change in counts and probabilities occurs because too much probabil-ity mass is moved to all the zeros.

4.4.2 Add-k smoothingOne alternative to add-one smoothing is to move a bit less of the probability massfrom the seen to the unseen events. Instead of adding 1 to each count, we add a frac-tional count k (.5? .05? .01?). This algorithm is therefore called add-k smoothing.add-k

P⇤Add-k(wn|wn�1) =

C(wn�1wn)+ kC(wn�1)+ kV

(4.23)

Add-k smoothing requires that we have a method for choosing k; this can bedone, for example, by optimizing on a devset. Although add-k is is useful for sometasks (including text classification), it turns out that it still doesn’t work well forlanguage modeling, generating counts with poor variances and often inappropriatediscounts (Gale and Church, 1994).

4.4.3 Backoff and InterpolationThe discounting we have been discussing so far can help solve the problem of zerofrequency N-grams. But there is an additional source of knowledge we can drawon. If we are trying to compute P(wn|wn�2wn�1) but we have no examples of aparticular trigram wn�2wn�1wn, we can instead estimate its probability by usingthe bigram probability P(wn|wn�1). Similarly, if we don’t have counts to computeP(wn|wn�1), we can look to the unigram P(wn).

In other words, sometimes using less context is a good thing, helping to general-ize more for contexts that the model hasn’t learned much about. There are two waysto use this N-gram “hierarchy”. In backoff, we use the trigram if the evidence isbackoff

sufficient, otherwise we use the bigram, otherwise the unigram. In other words, weonly “back off” to a lower-order N-gram if we have zero evidence for a higher-orderN-gram. By contrast, in interpolation, we always mix the probability estimatesinterpolation

from all the N-gram estimators, weighing and combining the trigram, bigram, andunigram counts.

In simple linear interpolation, we combine different order N-grams by linearlyinterpolating all the models. Thus, we estimate the trigram probability P(wn|wn�2wn�1)by mixing together the unigram, bigram, and trigram probabilities, each weightedby a l :

P̂(wn|wn�2wn�1) = l1P(wn|wn�2wn�1)

+l2P(wn|wn�1)

+l3P(wn) (4.24)

such that the l s sum to 1: X

i

li = 1 (4.25)

In a slightly more sophisticated version of linear interpolation, each l weight iscomputed in a more sophisticated way, by conditioning on the context. This way,if we have particularly accurate counts for a particular bigram, we assume that thecounts of the trigrams based on this bigram will be more trustworthy, so we canmake the l s for those trigrams higher and thus give that trigram more weight in

Page 58: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

Howtosetthelambdas?

• Useaheld-out corpus

• Chooseλs tomaximizetheprobabilityofheld-outdata:• FixtheN-gramprobabilities(onthetrainingdata)• Thensearchforλs thatgivelargestprobabilitytoheld-outset:

TrainingData Held-OutData

TestData

logP(w1...wn |M (λ1...λk )) = logPM (λ1...λk ) (wi |wi−1)i∑

Page 59: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

Unknownwords:Openversusclosedvocabularytasks

• Ifweknowallthewordsinadvanced• VocabularyVisfixed• Closedvocabularytask

• Oftenwedon’tknowthis• OutOfVocabulary =OOVwords• Openvocabularytask

• Instead:createanunknownwordtoken<UNK>• Trainingof<UNK>probabilities

• CreateafixedlexiconLofsizeV• Attextnormalizationphase,anytrainingwordnotinLchangedto<UNK>• Nowwetrainitsprobabilitieslikeanormalword

• Atdecodingtime• Iftextinput:UseUNKprobabilitiesforanywordnotintraining

Page 60: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

Hugeweb-scalen-grams• Howtodealwith,e.g.,GoogleN-gramcorpus• Pruning• OnlystoreN-gramswithcount>threshold.

• Removesingletonsofhigher-ordern-grams• Entropy-basedpruning

• Efficiency• Efficientdatastructuresliketries• Bloomfilters:approximatelanguagemodels• Storewordsasindexes,notstrings

• UseHuffmancodingtofitlargenumbersofwordsintotwobytes• Quantizeprobabilities(4-8bitsinsteadof8-bytefloat)

Page 61: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

SmoothingforWeb-scaleN-grams

• “Stupidbackoff”(Brants etal.2007)•Nodiscounting, justuserelativefrequencies

61

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

Page 62: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

Biglanguagemodelshelpmachinetranslationalot

Page 63: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

N-gramSmoothingSummary

•Add-1smoothing:• OKfortextcategorization,notforlanguagemodeling

•Themostcommonlyusedmethod:• ExtendedInterpolatedKneser-Ney

•ForverylargeN-gramsliketheWeb:• Stupidbackoff

63

Page 64: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

Advanced Language Modeling• Discriminativemodels:• choosen-gramweightstoimproveatask,nottofitthetrainingset

• Parsing-basedmodels• CachingModels• Recentlyusedwordsaremorelikelytoappear

• Theseperformverypoorlyforspeechrecognition(why?)

PCACHE (w | history) = λP(wi |wi−2wi−1)+ (1−λ)c(w ∈ history)| history |

Page 65: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

Interpolation,Backoff,andWeb-ScaleLMs

LanguageModeling

Page 66: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

Language Modeling

Advanced: Kneser-Ney Smoothing

Page 67: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

Absolute discounting: just subtract a little from each count

• Supposewewantedtosubtractalittlefromacountof4tosaveprobabilitymassforthezeros• Howmuchtosubtract?

• ChurchandGale(1991)’scleveridea• Divideup22millionwordsofAPNewswire

• Trainingandheld-outset• foreachbigraminthetrainingset• seetheactualcountintheheld-outset!

• Itsurelookslikec*=(c- .75)

Bigramcountintraining

Bigramcountinheldout set

0 .00002701 0.4482 1.253 2.244 3.235 4.216 5.237 6.218 7.219 8.26

Page 68: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

Absolute Discounting Interpolation

• Saveourselvessometimeandjustsubtract0.75(orsomed)!

(Maybekeepingacoupleextravaluesofdforcounts1and2)•ButshouldwereallyjustusetheregularunigramP(w)?

68

PAbsoluteDiscounting (wi |wi−1) =c(wi−1,wi )− d

c(wi−1)+λ(wi−1)P(w)

discountedbigram

unigram

Interpolationweight

Page 69: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

• Betterestimateforprobabilitiesoflower-orderunigrams!• Shannongame:Ican’tseewithoutmyreading___________?• “Francisco”ismorecommonthan“glasses”• …but“Francisco”alwaysfollows“San”

• Theunigramisusefulexactlywhenwehaven’tseenthisbigram!• InsteadofP(w):“Howlikelyisw”• Pcontinuation(w):“Howlikelyiswtoappearasanovelcontinuation?• Foreachword,countthenumberofbigramtypesitcompletes• Everybigramtypewasanovelcontinuationthefirsttimeitwasseen

Francisco

Kneser-Ney Smoothing I

glasses

PCONTINUATION (w)∝ {wi−1 : c(wi−1,w)> 0}

Page 70: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

Kneser-Ney Smoothing II

• Howmanytimesdoeswappearasanovelcontinuation:

• Normalizedbythetotalnumberofwordbigramtypes

PCONTINUATION (w) ={wi−1 : c(wi−1,w)> 0}

{(wj−1,wj ) : c(wj−1,wj )> 0}

PCONTINUATION (w)∝ {wi−1 : c(wi−1,w)> 0}

{(wj−1,wj ) : c(wj−1,wj )> 0}

Page 71: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

Kneser-Ney Smoothing IV

71

PKN (wi |wi−1) =max(c(wi−1,wi )− d, 0)

c(wi−1)+λ(wi−1)PCONTINUATION (wi )

λ(wi−1) =d

c(wi−1){w : c(wi−1,w)> 0}

λ isanormalizingconstant;theprobabilitymasswe’vediscounted

thenormalizeddiscountThenumberofwordtypesthatcanfollowwi-1=#ofwordtypeswediscounted=#oftimesweappliednormalizeddiscount

Page 72: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

Kneser-Ney Smoothing: Recursive formulation

72

PKN (wi |wi−n+1i−1 ) = max(cKN (wi−n+1

i )− d, 0)cKN (wi−n+1

i−1 )+λ(wi−n+1

i−1 )PKN (wi |wi−n+2i−1 )

cKN (•) =count(•) for the highest order

continuationcount(•) for lower order

!"#

$#

Continuationcount=Numberofuniquesinglewordcontextsfor�

Page 73: Language Modeling - GitHub Pagesaritter.github.io/courses/5525_slides/language models.pdf · Probabilistic Language Models •Today’s goal: assign a probability to a sentence •Machine

Language Modeling

Advanced: Kneser-Ney Smoothing


Recommended