Seman&cs with Dense Vectors Dorota Glowacka - - PowerPoint PPT Presentation

seman cs with dense vectors
SMART_READER_LITE
LIVE PREVIEW

Seman&cs with Dense Vectors Dorota Glowacka - - PowerPoint PPT Presentation

Seman&cs with Dense Vectors Dorota Glowacka dorota.glowacka@ed.ac.uk Previous lectures: - how to represent a word as a sparse vector with


slide-1
SLIDE 1

Seman&cs ¡with ¡Dense ¡Vectors ¡

Dorota ¡Glowacka ¡ dorota.glowacka@ed.ac.uk ¡

slide-2
SLIDE 2

Previous ¡lectures: ¡ ¡-­‑ ¡how ¡to ¡represent ¡a ¡word ¡as ¡a ¡sparse ¡vector ¡ with ¡dimensions ¡corresponding ¡to ¡the ¡words ¡in ¡ the ¡vocabulary ¡ ¡ ¡ ¡-­‑ ¡the ¡values ¡in ¡the ¡vector ¡were ¡a ¡func&on ¡of ¡ the ¡ count ¡ of ¡ the ¡ word ¡ co-­‑occurring ¡ with ¡ each ¡ neighbouring ¡word ¡ ¡ -­‑ ¡ each ¡ word ¡ is ¡ thus ¡ represented ¡ with ¡ a ¡ vector ¡that ¡is ¡long ¡(with ¡vocabularies ¡of ¡20,000 ¡ to ¡ 50,000) ¡ and ¡ sparse ¡ (with ¡ most ¡ elements ¡ of ¡ the ¡vector ¡for ¡each ¡word ¡equal ¡to ¡zero) ¡ ¡ ¡

slide-3
SLIDE 3

Today’s ¡Lecture ¡

  • How ¡to ¡represent ¡a ¡word ¡with ¡vectors ¡that ¡are ¡

short ¡(with ¡length ¡of ¡50 ¡– ¡1,000) ¡and ¡dense ¡(most ¡ values ¡are ¡non-­‑zero) ¡

  • Why ¡short ¡vectors? ¡

¡-­‑ ¡easier ¡to ¡include ¡as ¡features ¡in ¡machine ¡ learning ¡systems ¡ ¡-­‑ ¡because ¡they ¡contain ¡fewer ¡parameters, ¡they ¡ generalize ¡beRer ¡and ¡are ¡less ¡prone ¡to ¡overfiTng ¡ ¡-­‑ ¡sparse ¡vectors ¡are ¡beRer ¡at ¡capturing ¡ synonymy ¡ ¡

slide-4
SLIDE 4

Singular ¡Value ¡Decomposi&on ¡(SVD) ¡

  • SVD ¡is ¡a ¡method ¡for ¡finding ¡the ¡most ¡important ¡

dimensions ¡of ¡a ¡dataset ¡

  • It ¡can ¡be ¡applied ¡to ¡any ¡rectangular ¡matrix ¡
  • SVD ¡ belongs ¡ to ¡ a ¡ family ¡ of ¡ methods ¡ that ¡ can ¡

approximate ¡ an ¡ N-­‑dimensional ¡ dataset ¡ using ¡ fewer ¡ dimensions, ¡ such ¡ as ¡ Principle ¡ Component ¡ Analysis ¡(PCA) ¡or ¡Factor ¡Analysis ¡

  • First ¡applied ¡in ¡Latent ¡Seman>c ¡Analysis ¡(LSA) ¡to ¡

tasks ¡ genera&ng ¡ embeddings ¡ from ¡ term-­‑ document ¡matrices ¡

slide-5
SLIDE 5

Singular ¡Value ¡Decomposi&on ¡(SVD) ¡

  • Dimensionality ¡reduc&on ¡methods ¡first ¡rotate ¡the ¡

axes ¡of ¡the ¡original ¡dataset ¡into ¡a ¡new ¡space. ¡

  • The ¡new ¡space ¡is ¡chosen ¡so ¡that ¡the ¡highest ¡order ¡

dimension ¡ captures ¡ the ¡ most ¡ variance ¡ in ¡ the ¡

  • riginal ¡dataset, ¡the ¡next ¡dimension ¡captures ¡the ¡

next ¡most ¡variance, ¡and ¡so ¡on. ¡

  • While ¡ some ¡ informa&on ¡ about ¡ the ¡ rela&onship ¡

between ¡the ¡original ¡points ¡is ¡necessarily ¡lost ¡in ¡ the ¡ new ¡ transforma&on, ¡ the ¡ remaining ¡ dimensions ¡preserve ¡as ¡much ¡as ¡possible ¡of ¡the ¡

  • riginal ¡seTng. ¡
slide-6
SLIDE 6
slide-7
SLIDE 7

Latent ¡Seman&c ¡Analysis ¡(LSA) ¡

  • LSA ¡is ¡a ¡par&cular ¡applica&on ¡of ¡SVD ¡to ¡a ¡|V| ¡× ¡c ¡term-­‑

document ¡matrix ¡X ¡represen&ng ¡|V| ¡words ¡and ¡their ¡ co-­‑occurrence ¡with ¡c ¡documents. ¡

  • SVD ¡ factorizes ¡ matrix ¡ X ¡ into ¡ the ¡ product ¡ of ¡ three ¡

matrices: ¡

  • 1. |V|× ¡ m ¡ matrix ¡ W, ¡ where ¡ each ¡ row ¡ w ¡ represents ¡ a ¡

word ¡and ¡each ¡column ¡represents ¡m ¡dimensions ¡in ¡a ¡ latent ¡space. ¡ ¡ m ¡column ¡vectors ¡are ¡orthogonal ¡to ¡each ¡other ¡and ¡are ¡

  • rdered ¡by ¡the ¡amount ¡of ¡variance ¡in ¡the ¡original ¡dataset ¡

m ¡= ¡rank ¡of ¡X ¡(number ¡of ¡linearly ¡independent ¡rows) ¡ ¡

slide-8
SLIDE 8

Latent ¡Seman&c ¡Analysis ¡(LSA) ¡

  • 2. ¡Σ ¡is ¡a ¡diagonal ¡m ¡× ¡m ¡matrix ¡with ¡singular ¡values ¡along ¡

the ¡ diagonal, ¡ expressing ¡ the ¡ importance ¡ of ¡ each ¡

  • dimension. ¡
  • 3. ¡The ¡m ¡× ¡c ¡matrix ¡C, ¡where ¡each ¡row ¡represents ¡one ¡of ¡

the ¡ latent ¡ dimensions ¡ and ¡ the ¡ m ¡ row ¡ vectors ¡ are ¡

  • rthogonal ¡to ¡each ¡other. ¡
  • By ¡using ¡only ¡the ¡first ¡k ¡dimensions ¡of ¡W, ¡Σ ¡and ¡C, ¡the ¡

product ¡ of ¡ these ¡ 3 ¡ matrices ¡ becomes ¡ a ¡ least-­‑squares ¡ approxima&on ¡to ¡the ¡original ¡X. ¡

  • Since ¡ the ¡ first ¡ dimensions ¡ encode ¡ the ¡ most ¡ variance, ¡

SVD ¡ models ¡ the ¡ most ¡ important ¡ informa&on ¡ in ¡ the ¡

  • riginal ¡X ¡
slide-9
SLIDE 9

X ! " # # # # # # # # # $ % & & & & & & & & & = W ! " # # # # # # # # # $ % & & & & & & & & & σ1 … σ 2 … σ 3 … ! ! ! " … ! σ m ! " # # # # # # # $ % & & & & & & & C ! " # # # # # # # $ % & & & & & & &

V ×c

V × m

m× m

m×c

slide-10
SLIDE 10

Taking ¡only ¡the ¡top ¡k ¡≤ ¡m ¡dimensions ¡a]er ¡SVD ¡is ¡applied ¡to ¡the ¡co-­‑occurrence ¡matrix ¡X: ¡ ¡

X ! " # # # # # # # # # $ % & & & & & & & & & = Wk ! " # # # # # # # # # # $ % & & & & & & & & & & σ1 … σ 2 … σ 3 … ! ! ! " … ! σ k ! " # # # # # # # $ % & & & & & & & C ! " $ % V ×c

V × k

k × k

k ×c

slide-11
SLIDE 11

SVD ¡and ¡LSA ¡

  • Using ¡only ¡the ¡top ¡k ¡dimensions ¡leads ¡to ¡a ¡

reduced ¡W ¡matrix, ¡with ¡one ¡k-­‑dimensioned ¡ row ¡per ¡word ¡

  • This ¡row ¡acts ¡as ¡a ¡dense ¡k-­‑dimensional ¡vector ¡

(embedding) ¡represen&ng ¡that ¡word ¡

  • LSA ¡embeddings ¡generally ¡set ¡k ¡= ¡300 ¡
  • LSA ¡applies ¡a ¡par&cular ¡weigh&ng ¡for ¡each ¡co-­‑
  • ccurrence ¡cell ¡that ¡mul&plies ¡two ¡weights: ¡

local ¡and ¡global ¡

slide-12
SLIDE 12

LSA ¡term ¡weigh&ng ¡

  • The ¡local ¡weight ¡of ¡each ¡term ¡i ¡in ¡document ¡j ¡is ¡

its ¡log ¡frequency: ¡

  • The ¡global ¡weight ¡of ¡term ¡i ¡is ¡a ¡version ¡of ¡its ¡

entropy: ¡ where ¡D ¡is ¡the ¡number ¡of ¡documents. ¡

log f i, j

( )+1

1+ ∑ j p i, j

( )log p i, j ( )

logD

slide-13
SLIDE 13

SVD ¡and ¡word-­‑context ¡

  • In ¡LSA, ¡SVD ¡is ¡applied ¡to ¡the ¡term-­‑document ¡
  • matrix. ¡
  • An ¡ alterna&ve ¡ is ¡ to ¡ apply ¡ SVD ¡ to ¡ the ¡ word-­‑

word ¡ or ¡ word-­‑context ¡ matrix ¡ – ¡ the ¡ context ¡ dimensions ¡are ¡words ¡(rather ¡than ¡documents ¡ as ¡in ¡LSA) ¡

  • Relies ¡on ¡PPMI-­‑weighted ¡word-­‑word ¡matrix ¡
  • Only ¡top ¡dimensions ¡are ¡used ¡– ¡truncated ¡SVD ¡
slide-14
SLIDE 14
slide-15
SLIDE 15

Skip-­‑gram ¡and ¡CBOW ¡

  • Methods ¡ for ¡ genera&ng ¡ dense ¡ embeddings ¡

inspired ¡by ¡neural ¡network ¡models ¡

  • Neural ¡ network ¡ language ¡ models ¡ are ¡ given ¡ a ¡

word ¡and ¡predict ¡a ¡context ¡– ¡this ¡process ¡can ¡be ¡ used ¡to ¡learn ¡word ¡embeddings. ¡

  • The ¡intui&on ¡is ¡that ¡words ¡with ¡similar ¡meanings ¡

tend ¡to ¡occur ¡near ¡each ¡other ¡in ¡text. ¡

  • The ¡process ¡for ¡learning ¡these ¡embeddings ¡has ¡a ¡

strong ¡ rela&onship ¡ with ¡ SVD ¡ factoriza&on ¡ and ¡ dot-­‑product ¡similarity ¡metrics. ¡

slide-16
SLIDE 16

Skip-­‑gram ¡Model ¡

  • Learns ¡two ¡separate ¡embeddings ¡for ¡each ¡word ¡

w: ¡word ¡embedding ¡v ¡and ¡context ¡embedding ¡c. ¡

  • Embeddings ¡ encoded ¡ in ¡ two ¡ matrices: ¡ word ¡

matrix ¡W ¡and ¡context ¡matrix ¡C. ¡

  • Each ¡ row ¡ i ¡ of ¡ word ¡ matrix ¡ W ¡ is ¡ 1 ¡ x ¡ d ¡ vector ¡

embedding ¡vi ¡for ¡word ¡i ¡vocabulary ¡V. ¡

  • Each ¡column ¡i ¡of ¡the ¡context ¡matrix ¡C ¡is ¡a ¡d ¡x ¡1 ¡

vector ¡embedding ¡ci ¡for ¡word ¡i ¡in ¡vocabulary ¡V. ¡

slide-17
SLIDE 17

Predic&on ¡with ¡Skip-­‑grams ¡

  • Skip-­‑gram ¡model ¡predicts ¡each ¡neighbouring ¡

word ¡in ¡a ¡context ¡window ¡of ¡L ¡words, ¡e.g. ¡ context ¡window ¡L ¡= ¡2 ¡the ¡context ¡is ¡ ¡

  • We ¡want ¡to ¡predict ¡each ¡of ¡the ¡context ¡words ¡

from ¡word ¡wj. ¡

  • Skip-­‑gram ¡calculates ¡the ¡probability ¡p(wk|wj) ¡by ¡

compu&ng ¡dot ¡product ¡between ¡context ¡vector ¡ ck ¡of ¡word ¡k ¡and ¡target ¡vector ¡vj ¡for ¡word ¡wj. ¡

  • The ¡higher ¡the ¡dot ¡product ¡between ¡two ¡vectors, ¡

the ¡more ¡similar ¡they ¡are. ¡ ¡

wt−2,wt−1,wt+1,wt+2 " # $ %

slide-18
SLIDE 18

Predic&on ¡with ¡Skip-­‑grams ¡

slide-19
SLIDE 19

Predic&on ¡with ¡Skip-­‑grams ¡

  • Dot ¡product ¡ck ¡× ¡vj ¡is ¡a ¡number ¡ranging ¡from ¡-­‑inf. ¡

to ¡+inf. ¡ ¡

  • We ¡ use ¡ soSmax ¡ func&on ¡ to ¡ normalize ¡ the ¡ dot ¡

product ¡into ¡probabili&es: ¡

  • Compu&ng ¡the ¡denominator ¡requires ¡compu&ng ¡

dot ¡ product ¡ between ¡ each ¡ word ¡ in ¡ V ¡ and ¡ the ¡ target ¡word ¡wi, ¡which ¡may ¡take ¡a ¡long ¡&me. ¡

p wk | wj

( ) =

exp ck ⋅vj

( )

∑i∈V exp ci ⋅vj

( )

slide-20
SLIDE 20

Skip-­‑gram ¡with ¡nega&ve ¡sampling ¡

  • Faster ¡than ¡using ¡the ¡soSmax ¡func&on ¡
  • In ¡ the ¡ training ¡ phase, ¡ for ¡ each ¡ target ¡ word ¡ the ¡

algorithm ¡chooses ¡surrounding ¡context ¡words ¡as ¡ posi>ve ¡examples. ¡

  • For ¡each ¡posi&ve ¡example, ¡the ¡algorithm ¡samples ¡

k ¡ noise ¡ examples, ¡ or ¡ nega>ve ¡ examples, ¡ according ¡to ¡their ¡weighted ¡unigram ¡probability, ¡ from ¡non-­‑neighbour ¡words. ¡

  • The ¡goal ¡is ¡to ¡move ¡the ¡embeddings ¡towards ¡the ¡

neighbour ¡words ¡and ¡away ¡from ¡noise ¡words. ¡

slide-21
SLIDE 21

Skip-­‑gram ¡with ¡nega&ve ¡sampling ¡

lemon, a [tablespoon of apricot preserves or] jam ¡ ¡ ¡ ¡ ¡c1 ¡ ¡c2 ¡ ¡ ¡ ¡ ¡w ¡ ¡ ¡ ¡c3 ¡ ¡c4 ¡ goal ¡-­‑ ¡learn ¡an ¡embedding ¡whose ¡dot ¡product ¡with ¡each ¡ context ¡word ¡is ¡high ¡ ¡ We ¡select ¡2 ¡noise ¡words ¡for ¡each ¡of ¡the ¡context ¡words: ¡

[cement physical dear coaxial apricot attendant hence forever puddle]

¡n1 ¡ ¡n2 ¡ ¡ ¡ ¡ ¡n3 ¡n4 ¡ ¡w ¡ ¡n5 ¡ ¡n6 ¡ ¡n7 ¡ ¡ ¡ ¡ ¡n8 ¡ ¡

We ¡want ¡noise ¡words ¡n ¡to ¡have ¡a ¡low ¡dot-­‑product ¡with ¡ target ¡embedding ¡w. ¡

slide-22
SLIDE 22

Skip-­‑gram ¡with ¡nega&ve ¡sampling ¡ ¡

More ¡formally, ¡the ¡learning ¡objec&ve ¡is: ¡ ¡ ¡

where ¡σ ¡is ¡a ¡sigmoid ¡func&on ¡of ¡the ¡dot ¡product. ¡

The ¡learning ¡starts ¡with ¡randomly ¡ini&alized ¡W ¡ and ¡ C ¡ matrices, ¡ and ¡ then ¡ walking ¡ through ¡ the ¡ training ¡ corpus ¡ to ¡ maximize ¡ the ¡ objec&ve ¡ func&on. ¡ ¡

logα c⋅w

( )+∑i=1

k Εni≈p w

( ) logσ ni ⋅w

( )

% & ' (

slide-23
SLIDE 23
slide-24
SLIDE 24

Skip-­‑gram ¡as ¡neural ¡network ¡

We ¡have ¡input ¡vector ¡x ¡of ¡word ¡wj ¡represented ¡as ¡one-­‑hot ¡ vector ¡(one ¡element ¡= ¡1, ¡and ¡all ¡the ¡others ¡equal ¡0). ¡ Predict ¡probability ¡of ¡each ¡of ¡the ¡output ¡words ¡in ¡3 ¡steps: ¡

  • 1. ¡Select ¡embedding ¡from ¡W: ¡x ¡is ¡mul&plied ¡by ¡W ¡to ¡give ¡

hidden ¡(projec>on) ¡layer. ¡

  • 2. ¡ Compute ¡ dot ¡ product ¡ ck ¡ x ¡ vj: ¡ for ¡ each ¡ of ¡ the ¡ context ¡

words, ¡mul&ply ¡projec&on ¡vector ¡by ¡context ¡matrix ¡C. ¡This ¡ produces ¡a ¡1 ¡x ¡|V| ¡dimensional ¡output ¡vector ¡with ¡a ¡score ¡ for ¡each ¡word ¡in ¡V. ¡

  • 3. ¡Normalize ¡dot ¡products ¡into ¡probabili&es: ¡

¡ ¡ ¡

p wk wj

( ) = yk =

exp ck ⋅vj

( )

∑i∈V exp ci ⋅vj

( )

slide-25
SLIDE 25

Proper&es ¡of ¡embeddings ¡

Redmond ¡ Havel ¡ ¡ ninjutsu ¡ graffi4 ¡ capitulate ¡ Redmond ¡ Wash ¡ ¡ Vaclav ¡Havel ¡ ninja ¡ spray ¡paint ¡ ¡ capitula&on ¡ Redmond ¡ Washington ¡ President ¡ Vaclav ¡Havel ¡ Mar&al ¡arts ¡ graffi& ¡ capitulated ¡ ¡ Microso] ¡ Velvet ¡ Revolu&on ¡ swordsmanship ¡ taggers ¡ capitula&ng ¡ Examples ¡of ¡the ¡closest ¡tokens ¡to ¡some ¡target ¡words ¡using ¡a ¡phrase-­‑based ¡extension ¡of ¡ the ¡skip-­‑gram ¡algorithm ¡(Mikolov ¡et ¡al. ¡2013) ¡

slide-26
SLIDE 26

Proper&es ¡of ¡Embeddings ¡

Offsets ¡ between ¡ embeddings ¡ can ¡ capture ¡ rela4ons ¡ between ¡ words, ¡ e.g. ¡ vector(king) ¡ – ¡ vector(man) ¡ + ¡ vector(woman) ¡is ¡close ¡to ¡vector(queen) ¡ ¡

¡

Offsets ¡can ¡capture ¡gramma4cal ¡ number ¡

slide-27
SLIDE 27

Brown ¡clustering ¡

  • Method ¡ of ¡ grouping ¡ words ¡ into ¡ clusters ¡ based ¡ on ¡ their ¡

rela&onship ¡with ¡preceding ¡and ¡following ¡words. ¡

  • Brown ¡clusters ¡can ¡be ¡used ¡to ¡create ¡bit ¡vectors ¡for ¡a ¡word ¡

that ¡can ¡func&on ¡as ¡a ¡syntac&c ¡representa&on. ¡

  • Algorithm ¡makes ¡use ¡of ¡class-­‑based ¡language ¡model, ¡where ¡

each ¡word ¡w ¡belongs ¡to ¡a ¡class ¡c ¡with ¡a ¡probability ¡p. ¡

  • Probabili&es ¡to ¡a ¡pair ¡of ¡words ¡are ¡assigned ¡by ¡modelling ¡

the ¡transi&on ¡between ¡classes ¡rather ¡than ¡between ¡words. ¡

  • Class-­‑based ¡LM ¡can ¡assign ¡probability ¡to ¡an ¡en&re ¡corpus ¡

given ¡a ¡par&cular ¡clustering ¡C: ¡

P corpus C

( ) = ∏i−1

n P ci ci−1

( )P wi ci ( )

slide-28
SLIDE 28

Brown ¡clustering ¡

Brown ¡clustering ¡is ¡a ¡hierarchical ¡algorithm: ¡

  • 1. Each ¡word ¡is ¡ini&ally ¡assigned ¡to ¡its ¡own ¡cluster. ¡
  • 2. We ¡consider ¡merging ¡each ¡pair ¡of ¡clusters. ¡The ¡

pair ¡ whose ¡ merger ¡ results ¡ in ¡ the ¡ smallest ¡ decrease ¡ in ¡ the ¡ likelihood ¡ of ¡ the ¡ corpus ¡ is ¡

  • merged. ¡
  • 3. Clustering ¡proceeds ¡un&l ¡all ¡words ¡are ¡in ¡one ¡big ¡
  • cluster. ¡

Two ¡ words ¡ are ¡ most ¡ likely ¡ to ¡ be ¡ clustered ¡ if ¡ they ¡ have ¡ similar ¡ probabili&es ¡ for ¡ preceding ¡ and ¡ following ¡words. ¡

slide-29
SLIDE 29

Brown ¡clustering ¡

  • By ¡tracing ¡the ¡order ¡in ¡which ¡clusters ¡are ¡merged, ¡the ¡model ¡

builds ¡a ¡binary ¡tree ¡with ¡leaves ¡are ¡words. ¡

  • A ¡word ¡can ¡be ¡represented ¡by ¡binary ¡string ¡that ¡corresponds ¡

to ¡its ¡path ¡from ¡the ¡root ¡with ¡0 ¡for ¡le] ¡and ¡1 ¡for ¡right ¡ ¡

slide-30
SLIDE 30

Brown ¡clustering ¡

We ¡ can ¡ extract ¡ useful ¡ features ¡ by ¡ taking ¡ binary ¡ prefixes ¡of ¡the ¡bit ¡string: ¡ ¡01 ¡ ¡-­‑ ¡cluster ¡of ¡month ¡names ¡{November, ¡October} ¡ ¡001 ¡ ¡-­‑ ¡names ¡of ¡common ¡nouns ¡for ¡corporate ¡ ¡ ¡ ¡ ¡execu&ves ¡{chairman, ¡president} ¡ ¡1 ¡– ¡verbs ¡{run, ¡sprint, ¡walk} ¡ ¡0 ¡– ¡is ¡nouns ¡ The ¡ shorter ¡ the ¡ prefix, ¡ the ¡ more ¡ abstract ¡ the ¡

  • cluster. ¡