+ All Categories
Home > Documents > CS224d Deep Learning for Natural Language Processing...

CS224d Deep Learning for Natural Language Processing...

Date post: 06-Mar-2018
Category:
Upload: dinhngoc
View: 230 times
Download: 0 times
Share this document with a friend
40
CS224d Deep Learning for Natural Language Processing Lecture 2: Word Vectors Richard Socher
Transcript
Page 1: CS224d Deep Learning for Natural Language Processing ...cs224d.stanford.edu/lectures/CS224d-Lecture2.pdf · CS224d Deep Learning for Natural Language Processing Lecture 2: Word Vectors

CS224dDeepLearning

forNaturalLanguageProcessing

Lecture2:WordVectorsRichardSocher

Page 2: CS224d Deep Learning for Natural Language Processing ...cs224d.stanford.edu/lectures/CS224d-Lecture2.pdf · CS224d Deep Learning for Natural Language Processing Lecture 2: Word Vectors

Howdowerepresentthemeaningofaword?

3/31/16RichardSocher2

Defini4on:Meaning(Websterdic4onary)

•  theideathatisrepresentedbyaword,phrase,etc.

•  theideathatapersonwantstoexpressbyusingwords,signs,etc.

•  theideathatisexpressedinaworkofwri4ng,art,etc.

Page 3: CS224d Deep Learning for Natural Language Processing ...cs224d.stanford.edu/lectures/CS224d-Lecture2.pdf · CS224d Deep Learning for Natural Language Processing Lecture 2: Word Vectors

Howtorepresentmeaninginacomputer?

3/31/16RichardSocher3

Commonanswer:UseataxonomylikeWordNetthathashypernyms(is-a)rela4onshipsand

synonymsets(good):

[Synset('procyonid.n.01'),Synset('carnivore.n.01'),Synset('placental.n.01'),Synset('mammal.n.01'),Synset('vertebrate.n.01'),Synset('chordate.n.01'),Synset('animal.n.01'),Synset('organism.n.01'),Synset('living_thing.n.01'),Synset('whole.n.02'),Synset('object.n.01'),Synset('physical_en4ty.n.01'),Synset('en4ty.n.01')]

S:(adj)full,goodS:(adj)es4mable,good,honorable,respectableS:(adj)beneficial,goodS:(adj)good,just,uprightS:(adj)adept,expert,good,prac4ced,proficient,skillfulS:(adj)dear,good,nearS:(adj)good,right,ripe…S:(adv)well,goodS:(adv)thoroughly,soundly,goodS:(n)good,goodnessS:(n)commodity,tradegood,good

Page 4: CS224d Deep Learning for Natural Language Processing ...cs224d.stanford.edu/lectures/CS224d-Lecture2.pdf · CS224d Deep Learning for Natural Language Processing Lecture 2: Word Vectors

ProblemswiththisdiscreterepresentaDon

3/31/16RichardSocher4

•  Greatasresourcebutmissingnuances,e.g.synonyms:adept,expert,good,prac4ced,proficient,skillful?

•  Missingnewwords(impossibletokeepuptodate):wicked,badass,niXy,crack,ace,wizard,genius,ninjia

•  Subjec4ve

•  Requireshumanlabortocreateandadapt

•  Hardtocomputeaccuratewordsimilarityà

Page 5: CS224d Deep Learning for Natural Language Processing ...cs224d.stanford.edu/lectures/CS224d-Lecture2.pdf · CS224d Deep Learning for Natural Language Processing Lecture 2: Word Vectors

ProblemswiththisdiscreterepresentaDon

Thevastmajorityofrule-basedandsta4s4calNLPworkregardswordsasatomicsymbols:hotel, conference, walk

Invectorspaceterms,thisisavectorwithone1andalotofzeroes

[0 0 0 0 0 0 0 0 0 0 1 0 0 0 0] Dimensionality:20K(speech)–50K(PTB)–500K(bigvocab)–13M(Google1T)

Wecallthisa“one-hot”representa4on.Itsproblem:

motel [0 0 0 0 0 0 0 0 0 0 1 0 0 0 0] AND hotel [0 0 0 0 0 0 0 1 0 0 0 0 0 0 0] = 0

5

Page 6: CS224d Deep Learning for Natural Language Processing ...cs224d.stanford.edu/lectures/CS224d-Lecture2.pdf · CS224d Deep Learning for Natural Language Processing Lecture 2: Word Vectors

DistribuDonalsimilaritybasedrepresentaDons

Youcangetalotofvaluebyrepresen4ngawordbymeansofitsneighbors

“Youshallknowawordbythecompanyitkeeps”(J.R.Firth1957:11)

Oneofthemostsuccessfulideasofmodernsta4s4calNLP

government debt problems turning into banking crises as has happened in

saying that Europe needs unified banking regulation to replace the hodgepodge

ëThesewordswillrepresentbankingì

6

Page 7: CS224d Deep Learning for Natural Language Processing ...cs224d.stanford.edu/lectures/CS224d-Lecture2.pdf · CS224d Deep Learning for Natural Language Processing Lecture 2: Word Vectors

Howtomakeneighborsrepresentwords?

3/31/16RichardSocher7

Answer:WithacooccurrencematrixX

•  2op4ons:fulldocumentvswindows

•  Word-documentcooccurrencematrixwillgivegeneraltopics(allsportstermswillhavesimilarentries)leadingto“LatentSeman4cAnalysis”

•  Instead:Windowaroundeachwordàcapturesbothsyntac4c(POS)andseman4cinforma4on

Page 8: CS224d Deep Learning for Natural Language Processing ...cs224d.stanford.edu/lectures/CS224d-Lecture2.pdf · CS224d Deep Learning for Natural Language Processing Lecture 2: Word Vectors

Windowbasedcooccurencematrix

3/31/16RichardSocher8

•  Windowlength1(morecommon:5-10)

•  Symmetric(irrelevantwhetherleXorrightcontext)

•  Examplecorpus:•  Ilikedeeplearning.

•  IlikeNLP.

•  Ienjoyflying.

Page 9: CS224d Deep Learning for Natural Language Processing ...cs224d.stanford.edu/lectures/CS224d-Lecture2.pdf · CS224d Deep Learning for Natural Language Processing Lecture 2: Word Vectors

Windowbasedcooccurencematrix

3/31/16RichardSocher9

•  Examplecorpus:•  Ilikedeeplearning.

•  IlikeNLP.

•  Ienjoyflying.counts I like enjoy deep learning NLP flying .

I 0 2 1 0 0 0 0 0

like 2 0 0 1 0 1 0 0

enjoy 1 0 0 0 0 0 1 0

deep 0 1 0 0 1 0 0 0

learning 0 0 0 1 0 0 0 1

NLP 0 1 0 0 0 0 0 1

flying 0 0 1 0 0 0 0 1

. 0 0 0 0 1 1 1 0

Page 10: CS224d Deep Learning for Natural Language Processing ...cs224d.stanford.edu/lectures/CS224d-Lecture2.pdf · CS224d Deep Learning for Natural Language Processing Lecture 2: Word Vectors

Problemswithsimplecooccurrencevectors

3/31/16RichardSocher10

Increaseinsizewithvocabulary

Veryhighdimensional:requirealotofstorage

Subsequentclassifica4onmodelshavesparsityissues

àModelsarelessrobust

Page 11: CS224d Deep Learning for Natural Language Processing ...cs224d.stanford.edu/lectures/CS224d-Lecture2.pdf · CS224d Deep Learning for Natural Language Processing Lecture 2: Word Vectors

SoluDon:Lowdimensionalvectors

3/31/16RichardSocher11

•  Idea:store“most”oftheimportantinforma4oninafixed,smallnumberofdimensions:adensevector

•  Usuallyaround25–1000dimensions

•  Howtoreducethedimensionality?

Page 12: CS224d Deep Learning for Natural Language Processing ...cs224d.stanford.edu/lectures/CS224d-Lecture2.pdf · CS224d Deep Learning for Natural Language Processing Lecture 2: Word Vectors

Method1:DimensionalityReducDononX

3/31/16RichardSocher12

SingularValueDecomposi4onofcooccurrencematrixX.

Rohde, Gonnerman, Plaut Modeling Word Meaning Using Lexical Co-Occurrence

r

= nn

r

r

X U S

SSS

S.

23

1

r

UUU1 2 3

Vm m

VV1

2

3. . ...

. ..

=n

X U S

m

V T

V T

UUU1 2 3

Sk

0

0

0

0

Vm

VV1

2

3..

.

SSS2 3

1

. ..

kk

kn

r

k

Figure 1: The singular value decomposition of matrix X .X is the best rank k approximation to X , in terms of leastsquares.

tropy of the document distribution of row vector a. Wordsthat are evenly distributed over documents will have highentropy and thus a low weighting, reflecting the intuitionthat such words are less interesting.The critical step of the LSA algorithm is to compute

the singular value decomposition (SVD) of the normal-ized co-occurrencematrix. An SVD is similar to an eigen-value decomposition, but can be computed for rectangu-lar matrices. As shown in Figure 1, the SVD is a prod-uct of three matrices, the first, U , containing orthonormalcolumns known as the left singular vectors, and the last,VT containing orthonormal rows known as the right sin-gular vectors, while the middle, S, is a diagonal matrixcontaining the singular values. The left and right singu-lar vectors are akin to eigenvectors and the singular valuesare akin to eigenvalues and rate the importance of the vec-tors.1 The singular vectors reflect principal components,or axes of greatest variance in the data.If the matrices comprising the SVD are permuted such

that the singular values are in decreasing order, they canbe truncated to a much lower rank, k. It can be shown thatthe product of these reducedmatrices is the best rank k ap-proximation, in terms of sum squared error, to the originalmatrix X . The vector representing word a in the reduced-rank space is Ua, the ath row of U , while the vector repre-senting document b is Vb, the bth row of V . If a new word,c, or a new document, d, is added after the computationof the SVD, their reduced-dimensionality vectors can becomputed as follows:

Uc = XcV S−1

Vd = XTd US−1

The similarity of two words or two documents in LSAis usually computed using the cosine of their reduced-dimensionality vectors, the formula for which is given in

1In fact, if the matrix is symmetric and positive semidefinite, the leftand right singular vectors will be identical and equivalent to its eigen-vectors and the singular values will be its eigenvalues.

Table 3. It is unclear whether the vectors are first scaledby the singular values, S, before computing the cosine,as implied in Deerwester, Dumais, Furnas, Landauer, andHarshman (1990).Computing the SVD itself is not trivial. For a dense

matrix with dimensions n < m, the SVD computationrequires time proportional to n2m. This is impracticalfor matrices with more than a few thousand dimensions.However, LSA co-occurrence matrices tend to be quitesparse and the SVD computation is much faster for sparsematrices, allowing the model to handle hundreds of thou-sands of words and documents. The LSA similarity rat-ings tested here were generated using the term-to-termpairwise comparison interface available on the LSA website (http://lsa.colorado.edu).2 The model was trained onthe TouchstoneApplied Science Associates (TASA) “gen-eral reading up to first year college” data set, with the top300 dimensions retained.

2.3 WordNet-based models

WordNet is a network consisting of synonym sets, repre-senting lexical concepts, linked together with various rela-tions, such as synonym, hypernym, and hyponym (Milleret al., 1990). There have been several efforts to base ameasure of semantic similarity on the WordNet database,some of which are reviewed in Budanitsky and Hirst(2001), Patwardhan, Banerjee, and Pedersen (2003), andJarmasz and Szpakowicz (2003). Here we briefly sum-marize each of these methods. The similarity ratings re-ported in Section 3 were generated using version 0.06 ofTed Pedersen’s WordNet::Similarity module, along withWordNet version 2.0.The WordNet methods have an advantage over HAL,

LSA, and COALS in that they distinguish between mul-tiple word senses. This raises the question, when judg-ing the similarity of a pair of polysemous words, ofwhich senses to use in the comparison. When given thepair thick–stout, most human subjects will judge them tobe quite similar because stout means strong and sturdy,which may imply that something is thick. But the pairlager–stout is also likely to be considered similar becausethey denote types of beer. In this case, the rater may noteven be consciously aware of the adjective sense of stout.Consider also hammer–saw versus smelled–saw. Whetheror not we are aware of it, we tend to rate the similarity ofa polysemous word pair on the basis of the senses that aremost similar to one another. Therefore, the same was donewith the WordNet models.

2The document-to-document LSAmode was also tested but the term-to-term method proved slightly better.

4

isthebestrankkapproxima4ontoX,intermsofleastsquares.

Rohde, Gonnerman, Plaut Modeling Word Meaning Using Lexical Co-Occurrence

r

= nn

r

r

X U S

SSS

S.

23

1

r

UUU1 2 3

Vm m

VV1

2

3. . ...

. ..

=n

X U S

m

V T

V T

UUU1 2 3

Sk

0

0

0

0

Vm

VV1

2

3..

.

SSS2 3

1

. ..

kk

kn

r

k

Figure 1: The singular value decomposition of matrix X .X is the best rank k approximation to X , in terms of leastsquares.

tropy of the document distribution of row vector a. Wordsthat are evenly distributed over documents will have highentropy and thus a low weighting, reflecting the intuitionthat such words are less interesting.The critical step of the LSA algorithm is to compute

the singular value decomposition (SVD) of the normal-ized co-occurrencematrix. An SVD is similar to an eigen-value decomposition, but can be computed for rectangu-lar matrices. As shown in Figure 1, the SVD is a prod-uct of three matrices, the first, U , containing orthonormalcolumns known as the left singular vectors, and the last,VT containing orthonormal rows known as the right sin-gular vectors, while the middle, S, is a diagonal matrixcontaining the singular values. The left and right singu-lar vectors are akin to eigenvectors and the singular valuesare akin to eigenvalues and rate the importance of the vec-tors.1 The singular vectors reflect principal components,or axes of greatest variance in the data.If the matrices comprising the SVD are permuted such

that the singular values are in decreasing order, they canbe truncated to a much lower rank, k. It can be shown thatthe product of these reducedmatrices is the best rank k ap-proximation, in terms of sum squared error, to the originalmatrix X . The vector representing word a in the reduced-rank space is Ua, the ath row of U , while the vector repre-senting document b is Vb, the bth row of V . If a new word,c, or a new document, d, is added after the computationof the SVD, their reduced-dimensionality vectors can becomputed as follows:

Uc = XcV S−1

Vd = XTd US−1

The similarity of two words or two documents in LSAis usually computed using the cosine of their reduced-dimensionality vectors, the formula for which is given in

1In fact, if the matrix is symmetric and positive semidefinite, the leftand right singular vectors will be identical and equivalent to its eigen-vectors and the singular values will be its eigenvalues.

Table 3. It is unclear whether the vectors are first scaledby the singular values, S, before computing the cosine,as implied in Deerwester, Dumais, Furnas, Landauer, andHarshman (1990).Computing the SVD itself is not trivial. For a dense

matrix with dimensions n < m, the SVD computationrequires time proportional to n2m. This is impracticalfor matrices with more than a few thousand dimensions.However, LSA co-occurrence matrices tend to be quitesparse and the SVD computation is much faster for sparsematrices, allowing the model to handle hundreds of thou-sands of words and documents. The LSA similarity rat-ings tested here were generated using the term-to-termpairwise comparison interface available on the LSA website (http://lsa.colorado.edu).2 The model was trained onthe TouchstoneApplied Science Associates (TASA) “gen-eral reading up to first year college” data set, with the top300 dimensions retained.

2.3 WordNet-based models

WordNet is a network consisting of synonym sets, repre-senting lexical concepts, linked together with various rela-tions, such as synonym, hypernym, and hyponym (Milleret al., 1990). There have been several efforts to base ameasure of semantic similarity on the WordNet database,some of which are reviewed in Budanitsky and Hirst(2001), Patwardhan, Banerjee, and Pedersen (2003), andJarmasz and Szpakowicz (2003). Here we briefly sum-marize each of these methods. The similarity ratings re-ported in Section 3 were generated using version 0.06 ofTed Pedersen’s WordNet::Similarity module, along withWordNet version 2.0.The WordNet methods have an advantage over HAL,

LSA, and COALS in that they distinguish between mul-tiple word senses. This raises the question, when judg-ing the similarity of a pair of polysemous words, ofwhich senses to use in the comparison. When given thepair thick–stout, most human subjects will judge them tobe quite similar because stout means strong and sturdy,which may imply that something is thick. But the pairlager–stout is also likely to be considered similar becausethey denote types of beer. In this case, the rater may noteven be consciously aware of the adjective sense of stout.Consider also hammer–saw versus smelled–saw. Whetheror not we are aware of it, we tend to rate the similarity ofa polysemous word pair on the basis of the senses that aremost similar to one another. Therefore, the same was donewith the WordNet models.

2The document-to-document LSAmode was also tested but the term-to-term method proved slightly better.

4

Page 13: CS224d Deep Learning for Natural Language Processing ...cs224d.stanford.edu/lectures/CS224d-Lecture2.pdf · CS224d Deep Learning for Natural Language Processing Lecture 2: Word Vectors

SimpleSVDwordvectorsinPython

3/31/16RichardSocher13

Corpus:Ilikedeeplearning.IlikeNLP.Ienjoyflying.

Page 14: CS224d Deep Learning for Natural Language Processing ...cs224d.stanford.edu/lectures/CS224d-Lecture2.pdf · CS224d Deep Learning for Natural Language Processing Lecture 2: Word Vectors

SimpleSVDwordvectorsinPython

3/31/16RichardSocher14

Corpus:Ilikedeeplearning.IlikeNLP.Ienjoyflying.Prin4ngfirsttwocolumnsofUcorrespondingtothe2biggestsingularvalues

Page 15: CS224d Deep Learning for Natural Language Processing ...cs224d.stanford.edu/lectures/CS224d-Lecture2.pdf · CS224d Deep Learning for Natural Language Processing Lecture 2: Word Vectors

Wordmeaningisdefinedintermsofvectors

•  Inallsubsequentmodels,includingdeeplearningmodels,awordisrepresentedasadensevector

linguis,cs=

15

0.2860.792−0.177−0.1070.109−0.5420.3490.271

Page 16: CS224d Deep Learning for Natural Language Processing ...cs224d.stanford.edu/lectures/CS224d-Lecture2.pdf · CS224d Deep Learning for Natural Language Processing Lecture 2: Word Vectors

HackstoX

3/31/16RichardSocher16

•  Problem:func4onwords(the,he,has)aretoofrequentàsyntaxhastoomuchimpact.Somefixes:

•  min(X,t),witht~100

•  Ignorethemall

•  Rampedwindowsthatcountcloserwordsmore

•  UsePearsoncorrela4onsinsteadofcounts,thensetnega4vevaluesto0

•  +++

Page 17: CS224d Deep Learning for Natural Language Processing ...cs224d.stanford.edu/lectures/CS224d-Lecture2.pdf · CS224d Deep Learning for Natural Language Processing Lecture 2: Word Vectors

InteresDngsemanDcpaPersemergeinthevectors

3/31/16RichardSocher17

Rohde, Gonnerman, Plaut Modeling Word Meaning Using Lexical Co-Occurrence

HEAD

HANDFACE

DOG

AMERICA

CAT

EYE

EUROPE

FOOT

CHINAFRANCE

CHICAGO

ARM

FINGER

NOSE

LEG

RUSSIA

MOUSE

AFRICA

ATLANTA

EAR

SHOULDER

ASIA

COW

BULL

PUPPY LION

HAWAII

MONTREAL

TOKYO

TOE

MOSCOW

TOOTH

NASHVILLE

BRAZIL

WRIST

KITTEN

ANKLE

TURTLE

OYSTER

Figure 8: Multidimensional scaling for three noun classes.

WRISTANKLE

SHOULDERARMLEGHAND

FOOTHEADNOSEFINGER

TOEFACEEAREYE

TOOTHDOGCAT

PUPPYKITTEN

COWMOUSE

TURTLEOYSTER

LIONBULLCHICAGOATLANTA

MONTREALNASHVILLE

TOKYOCHINARUSSIAAFRICAASIAEUROPEAMERICA

BRAZILMOSCOW

FRANCEHAWAII

Figure 9: Hierarchical clustering for three noun classes using distances based on vector correlations.

20

AnImprovedModelofSeman4cSimilarityBasedonLexicalCo-OccurrenceRohdeetal.2005

Page 18: CS224d Deep Learning for Natural Language Processing ...cs224d.stanford.edu/lectures/CS224d-Lecture2.pdf · CS224d Deep Learning for Natural Language Processing Lecture 2: Word Vectors

InteresDngsyntacDcpaPersemergeinthevectors

3/31/16RichardSocher18

AnImprovedModelofSeman4cSimilarityBasedonLexicalCo-OccurrenceRohdeetal.2005

Rohde, Gonnerman, Plaut Modeling Word Meaning Using Lexical Co-Occurrence

READ

CALLED

TOLD

HEARD

ASKED

CUT

FELT

NOTICED

EXPLAINED

KICKED

JUMPED

DETECTED

EMAILED

QUESTIONED

SHOUTED

TASTED

PUNCHED

SHOVED

STABBED

SMELLEDSENSED

BASHED

TACKLED

DISCERNED

Figure 10: Multidimensional scaling of three verb semantic classes.

TAKE

SHOW

TOOKTAKINGTAKEN

SPEAK

EAT

CHOOSE

SPEAKING

GROW

GROWING

THROW

SHOWN

SHOWING

SHOWED

EATING

CHOSEN

SPOKE

CHOSE

GROWN

GREW

SPOKEN

THROWNTHROWING

STEAL

ATE

THREW

STOLEN

STEALING

CHOOSING

STOLE

EATEN

Figure 11: Multidimensional scaling of present, past, progressive, and past participle forms for eight verb families.

22

Page 19: CS224d Deep Learning for Natural Language Processing ...cs224d.stanford.edu/lectures/CS224d-Lecture2.pdf · CS224d Deep Learning for Natural Language Processing Lecture 2: Word Vectors

InteresDngsemanDcpaPersemergeinthevectors

3/31/16RichardSocher19

AnImprovedModelofSeman4cSimilarityBasedonLexicalCo-OccurrenceRohdeetal.2005

Rohde, Gonnerman, Plaut Modeling Word Meaning Using Lexical Co-Occurrence

DRIVE

LEARN

DOCTOR

CLEAN

DRIVER

STUDENT

TEACH

TEACHER

TREAT PRAY

PRIEST

MARRY

SWIMBRIDE

JANITORSWIMMER

Figure 13: Multidimensional scaling for nouns and their associated verbs.

Table 10The 10 nearest neighbors and their percent correlation similarities for a set of nouns, under the COALS-14K model.

gun point mind monopoly cardboard lipstick leningrad feet1) 46.4 handgun 32.4 points 33.5 minds 39.9 monopolies 47.4 plastic 42.9 shimmery 24.0 moscow 59.5 inches2) 41.1 firearms 29.2 argument 24.9 consciousness 27.8 monopolistic 37.2 foam 40.8 eyeliner 22.7 sevastopol 57.7 foot3) 41.0 firearm 25.4 question 23.2 thoughts 26.5 corporations 36.7 plywood 38.8 clinique 22.7 petersburg 52.0 metres4) 35.3 handguns 22.3 arguments 22.4 senses 25.0 government 35.6 paper 38.4 mascara 20.7 novosibirsk 45.7 legs5) 35.0 guns 21.5 idea 22.2 subconscious 23.2 ownership 34.8 corrugated 37.2 revlon 20.3 russia 45.4 centimeters6) 32.7 pistol 20.1 assertion 20.8 thinking 22.2 property 32.3 boxes 35.4 lipsticks 19.6 oblast 44.4 meters7) 26.3 weapon 19.5 premise 20.6 perception 22.2 capitalism 31.3 wooden 35.3 gloss 19.5 minsk 40.2 inch8) 24.4 rifles 19.3 moot 20.4 emotions 21.8 capitalist 31.0 glass 34.1 shimmer 19.2 stalingrad 38.4 shoulders9) 24.2 shotgun 18.9 distinction 20.1 brain 21.6 authority 30.7 fabric 33.6 blush 19.1 ussr 37.8 knees10) 23.6 weapons 18.7 statement 19.9 psyche 21.3 subsidies 30.5 aluminum 33.5 nars 19.0 soviet 36.9 toes

Table 11The 10 nearest neighbors for a set of verbs, according to the COALS-14K model.

need buy play change send understand explain create1) 50.4 want 53.5 buying 63.5 playing 56.9 changing 55.0 sending 56.3 comprehend 53.0 understand 58.2 creating2) 50.2 needed 52.5 sell 55.5 played 55.3 changes 42.0 email 53.0 explain 46.3 describe 50.6 creates3) 42.1 needing 49.1 bought 47.6 plays 48.9 changed 40.2 e-mail 49.5 understood 40.0 explaining 45.1 develop4) 41.2 needs 41.8 purchase 37.2 players 32.2 adjust 39.8 unsubscribe 44.8 realize 39.8 comprehend 43.3 created5) 41.1 can 40.3 purchased 35.4 player 30.2 affect 37.3 mail 40.9 grasp 39.7 explained 42.6 generate6) 39.5 able 39.7 selling 33.8 game 29.5 modify 35.7 please 39.1 know 39.0 prove 37.8 build7) 36.3 try 38.2 sells 32.3 games 28.3 different 33.3 subscribe 38.8 believe 38.2 clarify 36.4 maintain8) 35.4 should 36.3 buys 29.0 listen 27.1 alter 33.1 receive 38.5 recognize 37.1 argue 36.4 produce9) 35.3 do 34.0 sale 26.8 playable 25.6 shift 32.7 submit 38.0 misunderstand 37.0 refute 35.4 integrate10) 34.7 necessary 31.5 cheap 25.0 beat 25.1 altering 31.5 address 37.9 understands 35.9 tell 35.2 implement

Table 12The 10 nearest neighbors for a set of adjectives, according to the COALS-14K model.

high frightened red correct similar fast evil christian1) 57.5 low 45.6 scared 53.7 blue 59.0 incorrect 44.9 similiar 43.1 faster 24.3 sinful 48.5 catholic2) 51.9 higher 37.2 terrified 47.8 yellow 37.7 accurate 43.2 different 41.2 slow 23.4 wicked 48.1 protestant3) 43.4 lower 33.7 confused 45.1 purple 37.5 proper 40.8 same 37.8 slower 23.2 vile 47.9 christians4) 43.2 highest 33.3 frustrated 44.9 green 36.3 wrong 40.6 such 28.2 rapidly 22.5 demons 47.2 orthodox5) 35.9 lowest 32.6 worried 43.2 white 34.1 precise 37.7 specific 27.3 quicker 22.3 satan 47.1 religious6) 31.5 increases 32.4 embarrassed 42.8 black 32.9 exact 35.6 identical 26.8 quick 22.3 god 46.4 christianity7) 30.7 increase 32.3 angry 36.8 colored 30.7 erroneous 34.6 these 25.9 speeds 22.3 sinister 43.8 fundamentalist8) 29.2 increasing 31.6 afraid 35.6 orange 30.6 valid 34.4 unusual 25.8 quickly 22.0 immoral 43.5 jewish9) 28.7 increased 30.4 upset 33.5 grey 30.6 inaccurate 34.1 certain 25.5 speed 21.5 hateful 43.2 evangelical10) 28.3 lowering 30.3 annoyed 32.4 reddish 29.8 acceptable 32.7 various 24.3 easy 21.3 sadistic 41.2 mormon

24

Page 20: CS224d Deep Learning for Natural Language Processing ...cs224d.stanford.edu/lectures/CS224d-Lecture2.pdf · CS224d Deep Learning for Natural Language Processing Lecture 2: Word Vectors

ProblemswithSVD

3/31/16RichardSocher20

Computa4onalcostscalesquadra4callyfornxmmatrix:

O(mn2)flops(whenn<m)

àBadformillionsofwordsordocuments

Hardtoincorporatenewwordsordocuments

DifferentlearningregimethanotherDLmodels

Page 21: CS224d Deep Learning for Natural Language Processing ...cs224d.stanford.edu/lectures/CS224d-Lecture2.pdf · CS224d Deep Learning for Natural Language Processing Lecture 2: Word Vectors

Idea:Directlylearnlow-dimensionalwordvectors

3/31/16RichardSocher21

•  Oldidea.Relevantforthislecture&deeplearning:•  Learningrepresenta4onsbyback-propaga4ngerrors.

(Rumelhartetal.,1986)

•  Aneuralprobabilis4clanguagemodel(Bengioetal.,2003)

•  NLP(almost)fromScratch(Collobert&Weston,2008)

•  Arecent,evensimplerandfastermodel:word2vec(Mikolovetal.2013)àintronow

Page 22: CS224d Deep Learning for Natural Language Processing ...cs224d.stanford.edu/lectures/CS224d-Lecture2.pdf · CS224d Deep Learning for Natural Language Processing Lecture 2: Word Vectors

MainIdeaofword2vec

3/31/16RichardSocher22

•  Insteadofcapturingcooccurrencecountsdirectly,

•  Predictsurroundingwordsofeveryword

•  Botharequitesimilar,see“Glove:GlobalVectorsforWordRepresenta,on”byPenningtonetal.(2014)andLevyandGoldberg(2014)…morelater

•  Fasterandcaneasilyincorporateanewsentence/documentoraddawordtothevocabulary

Page 23: CS224d Deep Learning for Natural Language Processing ...cs224d.stanford.edu/lectures/CS224d-Lecture2.pdf · CS224d Deep Learning for Natural Language Processing Lecture 2: Word Vectors

DetailsofWord2Vec

3/31/16RichardSocher23

•  Predictsurroundingwordsinawindowoflengthmofeveryword.

•  Objec4vefunc4on:Maximizethelogprobabilityofanycontextwordgiventhecurrentcenterword:

• 

•  Whereµrepresentsallvariablesweop4mize

Page 24: CS224d Deep Learning for Natural Language Processing ...cs224d.stanford.edu/lectures/CS224d-Lecture2.pdf · CS224d Deep Learning for Natural Language Processing Lecture 2: Word Vectors

DetailsofWord2Vec

3/31/16RichardSocher24

•  Predictsurroundingwordsinawindowoflengthmofeveryword

•  Forthesimplestfirstformula4onis

•  whereoistheoutside(oroutput)wordid,cisthecenterwordid,uandvare“center”and“outside”vectorsofoandc

•  Everywordhastwovectors!

•  Thisisessen4ally“dynamic”logis4cregression

training time. The basic Skip-gram formulation defines p(wt+j |wt) using the softmax function:

p(wO|wI) =exp

!

v′wO

⊤vwI

"

#Ww=1 exp

!

v′w⊤vwI

" (2)

where vw and v′w are the “input” and “output” vector representations of w, and W is the num-ber of words in the vocabulary. This formulation is impractical because the cost of computing∇ log p(wO|wI) is proportional toW , which is often large (105–107 terms).

2.1 Hierarchical Softmax

A computationally efficient approximation of the full softmax is the hierarchical softmax. In thecontext of neural network language models, it was first introduced by Morin and Bengio [12]. Themain advantage is that instead of evaluating W output nodes in the neural network to obtain theprobability distribution, it is needed to evaluate only about log2(W ) nodes.

The hierarchical softmax uses a binary tree representation of the output layer with theW words asits leaves and, for each node, explicitly represents the relative probabilities of its child nodes. Thesedefine a random walk that assigns probabilities to words.

More precisely, each word w can be reached by an appropriate path from the root of the tree. Letn(w, j) be the j-th node on the path from the root to w, and let L(w) be the length of this path, son(w, 1) = root and n(w,L(w)) = w. In addition, for any inner node n, let ch(n) be an arbitraryfixed child of n and let [[x]] be 1 if x is true and -1 otherwise. Then the hierarchical softmax definesp(wO|wI) as follows:

p(w|wI ) =

L(w)−1$

j=1

σ!

[[n(w, j + 1) = ch(n(w, j))]] · v′n(w,j)⊤vwI

"

(3)

where σ(x) = 1/(1 + exp(−x)). It can be verified that#W

w=1 p(w|wI) = 1. This implies that thecost of computing log p(wO|wI) and ∇ log p(wO|wI) is proportional to L(wO), which on averageis no greater than logW . Also, unlike the standard softmax formulation of the Skip-gram whichassigns two representations vw and v′w to each word w, the hierarchical softmax formulation hasone representation vw for each word w and one representation v′n for every inner node n of thebinary tree.

The structure of the tree used by the hierarchical softmax has a considerable effect on the perfor-mance. Mnih and Hinton explored a number of methods for constructing the tree structure and theeffect on both the training time and the resulting model accuracy [10]. In our work we use a binaryHuffman tree, as it assigns short codes to the frequent words which results in fast training. It hasbeen observed before that grouping words together by their frequency works well as a very simplespeedup technique for the neural network based language models [5, 8].

2.2 Negative Sampling

An alternative to the hierarchical softmax is Noise Contrastive Estimation (NCE), which was in-troduced by Gutmann and Hyvarinen [4] and applied to language modeling by Mnih and Teh [11].NCE posits that a good model should be able to differentiate data from noise by means of logisticregression. This is similar to hinge loss used by Collobert and Weston [2] who trained the modelsby ranking the data above noise.

While NCE can be shown to approximately maximize the log probability of the softmax, the Skip-gram model is only concerned with learning high-quality vector representations, so we are free tosimplify NCE as long as the vector representations retain their quality. We define Negative sampling(NEG) by the objective

log σ(v′wO

⊤vwI

) +k%

i=1

Ewi∼Pn(w)

&

log σ(−v′wi

⊤vwI

)'

(4)

3

Page 25: CS224d Deep Learning for Natural Language Processing ...cs224d.stanford.edu/lectures/CS224d-Lecture2.pdf · CS224d Deep Learning for Natural Language Processing Lecture 2: Word Vectors

Cost/ObjecDvefuncDons

3/31/16RichardSocher25

Wewillop4mize(maximizeorminimize)ourobjec4ve/costfunc4onsFornow:minimizeàgradientdescentRefresherwithtrivialexample:(fromWikipedia)Findalocalminimumofthefunc4onf(x)=x4−3x3+2,withderiva4vef'(x)=4x3−9x2.

Page 26: CS224d Deep Learning for Natural Language Processing ...cs224d.stanford.edu/lectures/CS224d-Lecture2.pdf · CS224d Deep Learning for Natural Language Processing Lecture 2: Word Vectors

DerivaDonsofgradient

3/31/16RichardSocher26

•  Whiteboard(seevideoifyou’renotinclass;)

•  ThebasicLegopiece

•  Usefulbasics:

•  Ifindoubt:writeoutwithindices

•  Chainrule!Ify=f(u)andu=g(x),i.e.y=f(g(x)),then:

Page 27: CS224d Deep Learning for Natural Language Processing ...cs224d.stanford.edu/lectures/CS224d-Lecture2.pdf · CS224d Deep Learning for Natural Language Processing Lecture 2: Word Vectors

ChainRule

3/31/16RichardSocher27

•  Chainrule!Ify=f(u)andu=g(x),i.e.y=f(g(x)),then:

•  Simpleexample:

Page 28: CS224d Deep Learning for Natural Language Processing ...cs224d.stanford.edu/lectures/CS224d-Lecture2.pdf · CS224d Deep Learning for Natural Language Processing Lecture 2: Word Vectors

InteracDveWhiteboardSession!

3/31/16RichardSocher28

Let’sderivegradienttogetherForoneexamplewindowandoneexampleoutsideword:

Page 29: CS224d Deep Learning for Natural Language Processing ...cs224d.stanford.edu/lectures/CS224d-Lecture2.pdf · CS224d Deep Learning for Natural Language Processing Lecture 2: Word Vectors

ApproximaDons:PSet1

3/31/16RichardSocher29

•  Withlargevocabulariesthisobjec4vefunc4onisnotscalableandwouldtraintooslowly!àWhy?

•  Idea:approximatethenormaliza4onor

•  Definenega4vepredic4onthatonlysamplesafewwordsthatdonotappearinthecontext

•  Similartofocusingonmostlyposi4vecorrela4ons

•  YouwillderiveandimplementthisinPset1!

Page 30: CS224d Deep Learning for Natural Language Processing ...cs224d.stanford.edu/lectures/CS224d-Lecture2.pdf · CS224d Deep Learning for Natural Language Processing Lecture 2: Word Vectors

LinearRelaDonshipsinword2vec

Theserepresenta4onsareverygoodatencodingdimensionsofsimilarity!•  Analogiestes4ngdimensionsofsimilaritycanbesolvedquite

welljustbydoingvectorsubtrac4onintheembeddingspaceSyntac4cally•  xapple−xapples≈xcar−xcars≈xfamily−xfamilies

•  Similarlyforverbandadjec4vemorphologicalformsSeman4cally(Semeval2012task2)•  xshirt−xclothing≈xchair−xfurniture•  xking−xman≈xqueen−xwoman

30

Page 31: CS224d Deep Learning for Natural Language Processing ...cs224d.stanford.edu/lectures/CS224d-Lecture2.pdf · CS224d Deep Learning for Natural Language Processing Lecture 2: Word Vectors

CountbasedvsdirectpredicDon

3/31/16RichardSocher31

LSA, HAL (Lund & Burgess), COALS (Rohde et al), Hellinger-PCA (Lebret & Collobert)

• Fast training

• Efficient usage of statistics

• Primarily used to capture word similarity

• Disproportionate importance given to large counts

• NNLM, HLBL, RNN, Skip-gram/CBOW, (Bengio et al; Collobert & Weston; Huang et al; Mnih & Hinton; Mikolov et al; Mnih & Kavukcuoglu)

• Scales with corpus size

•  Inefficient usage of statistics

• Can capture complex patterns beyond word similarity

• Generate improved performance on other tasks

Page 32: CS224d Deep Learning for Natural Language Processing ...cs224d.stanford.edu/lectures/CS224d-Lecture2.pdf · CS224d Deep Learning for Natural Language Processing Lecture 2: Word Vectors

Combiningthebestofbothworlds:GloVe

3/31/16RichardSocher32

• Fasttraining

• Scalabletohugecorpora

• Goodperformanceevenwithsmallcorpus,andsmallvectors

Page 33: CS224d Deep Learning for Natural Language Processing ...cs224d.stanford.edu/lectures/CS224d-Lecture2.pdf · CS224d Deep Learning for Natural Language Processing Lecture 2: Word Vectors

Gloveresults

3/31/16RichardSocher33

1.frogs2.toad3.litoria4.leptodactylidae5.rana6.lizard7.eleutherodactylus

litoria leptodactylidae

rana eleutherodactylus

Nearestwordstofrog:

Page 34: CS224d Deep Learning for Natural Language Processing ...cs224d.stanford.edu/lectures/CS224d-Lecture2.pdf · CS224d Deep Learning for Natural Language Processing Lecture 2: Word Vectors

king

manwoman

WordAnalogies

Testforlinearrela4onships,examinedbyMikolovetal.(2014)

a:b::c:?

man

woman

[0.200.20]

[0.600.30]

king [0.300.70]

[0.700.80]

-

+

+

queen

queen

man:woman::king:?

a:b::c:?

Page 35: CS224d Deep Learning for Natural Language Processing ...cs224d.stanford.edu/lectures/CS224d-Lecture2.pdf · CS224d Deep Learning for Natural Language Processing Lecture 2: Word Vectors

GloveVisualizaDons

3/31/16RichardSocher35

Page 36: CS224d Deep Learning for Natural Language Processing ...cs224d.stanford.edu/lectures/CS224d-Lecture2.pdf · CS224d Deep Learning for Natural Language Processing Lecture 2: Word Vectors

GloveVisualizaDons:Company-CEO

3/31/16RichardSocher36

Page 37: CS224d Deep Learning for Natural Language Processing ...cs224d.stanford.edu/lectures/CS224d-Lecture2.pdf · CS224d Deep Learning for Natural Language Processing Lecture 2: Word Vectors

GloveVisualizaDons:SuperlaDves

3/31/16RichardSocher37

Page 38: CS224d Deep Learning for Natural Language Processing ...cs224d.stanford.edu/lectures/CS224d-Lecture2.pdf · CS224d Deep Learning for Natural Language Processing Lecture 2: Word Vectors

[]

Wordembeddingmatrix

•  Ini4alizemostwordvectorsoffuturemodelswithour“pre-trained”embeddingmatrix|V|

L= … n

aardvarkaat…

•  Alsocalledalook-uptable•  Conceptuallyyougetaword’svectorbyleXmul4plyingaone-hotvectore(oflength|V|)byL:x=Le

38

Page 39: CS224d Deep Learning for Natural Language Processing ...cs224d.stanford.edu/lectures/CS224d-Lecture2.pdf · CS224d Deep Learning for Natural Language Processing Lecture 2: Word Vectors

Advantagesoflowdimensionalwordvectors

3939

Whatisthemajorbenefitofdeeplearnedwordvectors?

Abilitytoalsopropagateanyinforma4onintothemvianeuralnetworks(nextlecture).

Sc1c2c3

x1 x2x3+1

a1 a2P(c | d,λ) = eλ

T f (c,d )

eλT f ( !c ,d )

!c∑

Page 40: CS224d Deep Learning for Natural Language Processing ...cs224d.stanford.edu/lectures/CS224d-Lecture2.pdf · CS224d Deep Learning for Natural Language Processing Lecture 2: Word Vectors

Advantagesoflowdimensionalwordvectors

40

•  Wordvectorswillformthebasisforallsubsequentlectures.

•  Allourseman4crepresenta4onswillbevectors!

•  Nextlecture:

•  Somemoredetailsaboutwordvectors

•  Predictlabelsforwordsincontextforsolvinglotsofdifferenttasks


Recommended