word embeddings - - PowerPoint PPT Presentation

word embeddings
SMART_READER_LITE
LIVE PREVIEW

word embeddings - - PowerPoint PPT Presentation

word embeddings , ( ) 1 Word embeddings (


slide-1
SLIDE 1

1

Τι θα δούμε σήμερα

▪ Τα βασικά στοιχεία των word embeddings ▪ Ερωτήσεις, ασκήσεις ▪ Στατιστικά συλλογής (και ίσως συμπίεση)

slide-2
SLIDE 2

2

Word embeddings ΙΙ (basics)

slide-3
SLIDE 3

3

Διανυσματική αναπαράσταση (representation) λέξεων – κατανεμημένη (distributed) αναπαράσταση λέξη διάνυσμα

embedding Στόχος: όμοιες λέξεις -> όμοια διανύσματα

dense, low dimension

slide-4
SLIDE 4

4

Είσοδος κείμενα, προτάσεις Google news …

slide-5
SLIDE 5

5

Ομοιότητα/απόσταση

1 2 3 4 5 6 ||(1, 2, 3||

slide-6
SLIDE 6

6

slide-7
SLIDE 7

7

Πως θα βρούμε τις ποιο όμοιες λέξεις με το dog;

slide-8
SLIDE 8

8

TIP: Όπου μπορούμε χρησιμοποιούμε πράξεις πινάκων. Γιατί;

Το σωστό |V| x 1

slide-9
SLIDE 9

9

Λέξη ποιο όμοια σε πολλές άλλες;

slide-10
SLIDE 10

10

Σε προηγούμενα μαθήματα είδαμε Lemmatization Stemming Λέξεις σημασιολογικά κοντινές

slide-11
SLIDE 11

11

Πως θα πάρουμε αυτόν τον πίνακα;

slide-12
SLIDE 12

12

Βασική ιδέα

Μία λέξη προσδιορίζεται από τις συμφραζόμενες της λέξεις (context) Ο καθηγητής διδάσκει το μάθημα στους φοιτητές του στην αίθουσα. Παράθυρο (window) = 3 Center word Context word Κάθε λέξη δύο αναπαραστάσεις: (1) center (2) context Δηλαδή, έχουμε 2 |V| x d πίνακες ▪ Το center-διάνυσμα της center λέξης πρέπει να είναι όμοιο με τα context- διανύσματα (δηλαδή, το άθροισμα των context διανυσμάτων) των context λέξεων ▪ Και προφανώς το συμμετρικό Learning: παραδείγματα κειμένου και προσπαθούμε να «μάθουμε» αυτά τα διανύσματα (βάρη) Training examples – fix the matrices to work for them

slide-13
SLIDE 13

13

w: center representation – c: context representation

slide-14
SLIDE 14

14

w: center representation – c: context representation

Negative sampling (αρνητικά παραδείγματα)

slide-15
SLIDE 15

Word2Vec

15

Two algorithms

  • 1. Continuous Bag of Words (CBOW)

Predict center word from a bag-of-words context

  • 2. Skip-grams (SG)

Predict context words given the center word Position independent (do not account for distance from center) Two training methods

  • 1. Hierarchical softmax
  • 2. Negative sampling

Tomas Mikolov, Ilya Sutskever, Kai Chen, Gregory S. Corrado, Jeffrey Dean: Distributed Representations of Words and Phrases and their Compositionality. NIPS 2013: 3111-3119

slide-16
SLIDE 16

16

Το σκυλί __ την ουρά Η γάτα __ το ποντίκι Ο ήλιος __ το πρωί Το φεγγάρι __ κάθε νύχτα

Βασική ιδέα

CBOW __ __ κουνά __ __ __ __ κυνηγάει __ __ __ __ ανατέλλει __ __ __ __ δύει __ __ skipgram

slide-17
SLIDE 17

17

Ας δούμε πάλι και κάποιες λεπτομέρειες

slide-18
SLIDE 18

One-hot vectors

18

Έστω ότι υπάρχουν |V| διαφορετικές λέξεις (όροι) στο λεξικό μας ▪ Διατάσσουμε τις λέξεις αλφαβητικά ▪ Αναπαριστούμε κάθε λέξη με ένα R|𝑊|𝑦1 διάνυσμα που έχει παντού 0 και μόνο έναν 1 στη θέση που αντιστοιχεί στη θέση της λέξης στη διάταξη

𝟐 . . . 𝑥𝑏𝑏𝑠𝑒𝑤𝑏𝑠𝑙 = 𝟐 . . . 𝑥𝑏 = . . . 𝟐 𝑥𝑨𝑓𝑠𝑐𝑏 = 1 𝟏 . . . 𝑥𝑏𝑢 =

.. .

▪ Καμία πληροφορία για ομοιότητα ▪ Πολλές διαστάσεις

slide-19
SLIDE 19

Given matrix W, πως παίρνουμε το embedding της i-οστής λέξης;

19

W

𝑥𝑗

i

𝐹𝑂𝐷 𝑗 = 𝑋 𝐽𝑗

1

i

One-hot or indicator vector, all 0s but position i 𝑃𝑜𝑓 ℎ𝑝𝑢 𝑤𝑓𝑑𝑢𝑝𝑠 𝐽𝑗

Lookup/project

slide-20
SLIDE 20

CBOW

20

|V| number of words N size of embedding m size of the window (context)

Use a window of context words to predict the center word

Input: 2m context words Output: center word each represented as a one-hot vector

slide-21
SLIDE 21

CBOW

21

Use a window of context words to predict the center word Learns two matrices (two embeddings per word, one when context,

  • ne when center)

|V| Embedding of the i-th word when center word i N

W W’

N |V| i Embedding of the i-th word when context word

|V| x N context embeddings when input N x |V| center embeddings when output

slide-22
SLIDE 22

CBOW

22

Intuition The W’-embedding of the center word should be similar to the (sum of the) W-embeddings of its context words

We want similarity close to one for the center word and close to 0 for all other words

slide-23
SLIDE 23

CBOW

23

Given window size m 𝑦(𝑑) one hot vector for context words, y one hot vector for the center word

  • 1. INPUT: the one hot vectors for the 2m context words

𝑦(𝑑−𝑛), …, 𝑦(𝑑−1), 𝑦(𝑑+1), …, 𝑦(𝑑+𝑛)

  • 2. GET THE EMBEDDINGS of the context words

𝑤𝑑−𝑛 = 𝑋𝑦(𝑑−𝑛), …, 𝑤𝑑−1 = 𝑋𝑦(𝑑−1), 𝑤𝑑+1 = 𝑋𝑦(𝑑+1), …, 𝑤𝑑+𝑛= 𝑋𝑦(𝑑+𝑛)

  • 3. TAKE THE SUM these vectors

ො 𝑤 =

𝑤𝑑−𝑛+𝑤𝑑−𝑛+1+⋯𝑤𝑑+𝑛 2𝑛

, ො 𝑤 ∈ 𝑆𝑂

  • 4. COMPUTE SIMILARITY: dot produce W’ (all center vectors) and context ො

𝑤 z = W’ ො 𝑤

  • 5. Turn the score vector to probabilities

ො 𝑧 = softmax(z)

We want this to be close to 1 for the center word

slide-24
SLIDE 24

24

slide-25
SLIDE 25

25

1 … 1 …

cat

  • n

1 …

Input layer Hidden layer sat Output layer

  • ne-hot

vector

  • ne-hot

vector Index of cat in vocabulary

slide-26
SLIDE 26

26

1 … 1 …

cat

  • n

1 …

Input layer Hidden layer sat Output layer

𝑋

𝑊×𝑂

𝑋

𝑊×𝑂

V-dim V-dim N-dim

𝑋′𝑂×𝑊

V-dim N will be the size of word vector We must learn W and W’

slide-27
SLIDE 27

27

1 … 1 …

xcat xon

1 …

Input layer Hidden layer sat Output layer V-dim V-dim N-dim V-dim + ො 𝑤 = 𝑤𝑑𝑏𝑢 + 𝑤𝑝𝑜 2

0.1 2.4 1.6 1.8 0.5 0.9 … … … 3.2 0.5 2.6 1.4 2.9 1.5 3.6 … … … 6.1 … … … … … … … … … … … … … … … … … … … … 0.6 1.8 2.7 1.9 2.4 2.0 … … … 1.2

×

1 …

𝑋

𝑊×𝑂 𝑈

× 𝑦𝑑𝑏𝑢 = 𝑤𝑑𝑏𝑢

2.4 2.6 … … 1.8

=

slide-28
SLIDE 28

28

1 … 1 …

xcat xon

1 …

Input layer Hidden layer sat Output layer V-dim V-dim N-dim V-dim + ො 𝑤 = 𝑤𝑑𝑏𝑢 + 𝑤𝑝𝑜 2

0.1 2.4 1.6 1.8 0.5 0.9 … … … 3.2 0.5 2.6 1.4 2.9 1.5 3.6 … … … 6.1 … … … … … … … … … … … … … … … … … … … … 0.6 1.8 2.7 1.9 2.4 2.0 … … … 1.2

×

1 …

𝑋

𝑊×𝑂 𝑈

× 𝑦𝑝𝑜 = 𝑤𝑝𝑜

1.8 2.9 … … 1.9

=

slide-29
SLIDE 29

29

1 … 1 …

cat

  • n

1 …

Input layer Hidden layer ො 𝑧sat Output layer

𝑋

𝑊×𝑂

𝑋

𝑊×𝑂

V-dim V-dim N-dim

𝑋

𝑊×𝑂 ′

× ො 𝑤 = 𝑨

V-dim N will be the size of word vector ො 𝑤

ො 𝑧 = 𝑡𝑝𝑔𝑢𝑛𝑏𝑦(𝑨)

slide-30
SLIDE 30

30

1 … 1 …

cat

  • n

1 …

Input layer Hidden layer ො 𝑧sat Output layer

𝑋

𝑊×𝑂

𝑋

𝑊×𝑂

V-dim V-dim N-dim

𝑋

𝑊×𝑂 ′

× ො 𝑤 = 𝑨 ො 𝑧 = 𝑡𝑝𝑔𝑢𝑛𝑏𝑦(𝑨)

V-dim N will be the size of word vector ො 𝑤

0.01 0.02 0.00 0.02 0.01 0.02 0.01 0.7 … 0.00

ො 𝑧 We would prefer ො 𝑧 close to ො 𝑧𝑡𝑏𝑢

slide-31
SLIDE 31

31

1 … 1 …

xcat xon

1 …

Input layer Hidden layer sat Output layer V-dim V-dim N-dim V-dim

𝑋

𝑊×𝑂

𝑋

𝑊×𝑂

0.1 2.4 1.6 1.8 0.5 0.9 … … … 3.2 0.5 2.6 1.4 2.9 1.5 3.6 … … … 6.1 … … … … … … … … … … … … … … … … … … … … 0.6 1.8 2.7 1.9 2.4 2.0 … … … 1.2

𝑋

𝑊×𝑂 𝑈

Contain word’s vectors

𝑋

𝑊×𝑂 ′

We can consider either W (context) or W’ (center) as the word’s representation. Or even take the average.

slide-32
SLIDE 32

Skipgram

32

Given the center word, predict (or, generate) the context words Input: center word Output: 2m context word each represented as a one-hot vectors Learn two matrices W: N x |V|, input matrix, word representation as center word W’: |V| x N, output matrix, word representation as context word

slide-33
SLIDE 33

33

slide-34
SLIDE 34

Skipgram

34

𝑧(𝑘) one hot vector for context words

  • 1. Input: one hot vector of the center word

𝑦

  • 2. Get the embedding of the center word

𝑤𝑑 = 𝑋 𝑦

  • 3. Generate a score vector for each context word

z = W’ 𝑤𝑑

  • 5. Turn the score vector into probabilities

ො 𝑧 = softmax(z) We want this to be close to 1 for the context words

slide-35
SLIDE 35

35

slide-36
SLIDE 36

36

Εντυπωσιακά αποτελέσματα!

slide-37
SLIDE 37

37

These representations are very good at encoding similarity and dimensions of similarity!

  • Analogies testing dimensions of similarity can

be solved quite well just by doing vector subtraction in the embedding space

Syntactically – xapple − xapples ≈ xcar − xcars ≈ xfamily − xfamilies – Similarly for verb and adjective morphological forms Semantically – xshirt − xclothing ≈ xchair − xfurniture – xking − xman ≈ xqueen − xwoman

slide-38
SLIDE 38

38

bilingual embedding with chinese in green and english in yellow

Improve language translation

By aligning the word embeddings for the two languages

slide-39
SLIDE 39

End of lecture

39

Χρησιμοποιήθηκε υλικό από ▪ CS276: Information Retrieval and Web Search, Christopher Manning and Pandu Nayak, Lecture 14: Distributed Word Representations for Information Retrieval ▪ Jordan Boyd-Graber, UMD course Natural Language Processing, ▪ https://www.analyticsvidhya.com/blog/2017/06/word-embeddings-count-word2veec/ Μια περιγραφή του skipgram: Chris McCormick http://mccormickml.com/2016/04/19/word2vec-tutorial-the-skip-gram-model/ Δείτε και το https://www.analyticsvidhya.com/blog/2017/06/word-embeddings-count-word2veec/