word2vec (中文)

65
word2vec word2vec 之拼圖冒險 Yiwei Chen 2016.10

Upload: yiwei-chen

Post on 15-Apr-2017

339 views

Category:

Science


1 download

TRANSCRIPT

Page 1: Word2vec (中文)

word2vec word2vec 之拼圖冒險

Yiwei Chen2016.10

Page 2: Word2vec (中文)

關於這份投影片

● 不熟 NN 的人接觸 word2vec 的心得

● 只有概念,沒有實作

Page 3: Word2vec (中文)

Agenda

● Word embedding● word2vec● word2vec learning - simplified● word2vec - CBOW and skip-gram● Evaluation / Applications

Page 4: Word2vec (中文)

Word Embedding

Page 5: Word2vec (中文)

Represent a word: R1

V 個字: apple, bag, cat, …

● 以 ID 表示

● 沒有遠近關係

● 沒有特別意義

apple 1

bag 2

cat 3

Page 6: Word2vec (中文)

Represent a word: RV

V 個字: apple, bag, cat, …

● 以 one-hot encoding 表示

● 離散

● 每個字彼此距離一樣 -- 與相似度無關

● 還是跟 ID 差不多

apple (1, 0, 0, 0, …, 0)

bag (0, 1, 0, 0, …, 0)

cat (0, 0, 1, 0, …, 0)

Page 7: Word2vec (中文)

Represent a word: RV

● 如果有句子、文件供學習

○ Pigs drink water. Men drink water. Men eat pigs.● 例如 co-occurrence matrix:有關連了

● 維度仍然很高

pigs drink water men eat

pigs 0 1 1 1 1

drink 1 0 2 1 0

...

Page 8: Word2vec (中文)

Represent a word: RD

● 能否把所有字放在較低維度 vector space○ D 通常比 V 還小很多

● 且「相似」的字分佈在附近

watermilk

ParisLondon

Page 9: Word2vec (中文)

Represent a word: RD

● Continuous distributed word representations● 低維度向量

● 「相似」的字分佈在附近

watermilk

ParisLondon

Word Embedding

Page 10: Word2vec (中文)

word2vec

Page 11: Word2vec (中文)

word2vec

● 其中一種生出 word embedding 的技巧

● 2013 由 Tomas Mikolov 等人提出

● 同時有 open source 程式 (in C)

Page 12: Word2vec (中文)

MAGIC!

1. 不但相似的字會在附近

2. 而且兩個字的「關係」可以用「減法」來表示

woman − man ≈ queen − king

syntacticsemantic

Page 13: Word2vec (中文)

Learning from context

● 我在日本買了 yatsuhashi● 分一塊 yatsuhashi 給朋友

● yatsuhashi 很甜

用 yatsuhashi 造句: __?__ yatsuhashi __?__

Page 14: Word2vec (中文)

Learning from context - By-product

● 在日本買了 yatsuhashi● 我分一塊 yatsuhashi 給朋友

● yatsuhashi 很甜

● __?__ yatsuhashi __?__

● 由看過眾多文本,訓練一個模型猜測 __?__○ 但非主要目的

● 目的:藉由學習的過程獲得 yatsuhashi 的特徵

Page 15: Word2vec (中文)

2 important papers

1. “Efficient Estimation of Word Representations in Vector Space”

2. “Distributed Representations of Words and Phrases and their Compositionality”

(這兩篇之前的 “Linguistic Regularities in Continuous Space Word Representations” 也值得一看)

Page 16: Word2vec (中文)

可是我看不懂...

Page 17: Word2vec (中文)
Page 18: Word2vec (中文)

What !?

Page 19: Word2vec (中文)

An intro paper

● “word2vec Parameter Learning Explained” from Xin Rong

● 給不熟 Neural Network 的人

● 專注在 word2vec learning 數學式上

Page 20: Word2vec (中文)

word2vec learning - simplified

Page 21: Word2vec (中文)

Learn from context

● 給定大量文章

○ Pigs drink water. Men drink water. Men eat pigs.● 訓練一個類神經網路:input context, output target

○ Predicts (target | context)● 簡化:前一字推後一字

○ drink | pigs○ water | drink○ drink | men○ ...

Page 22: Word2vec (中文)

Feedforward NN, linear activation

context target

x1

2

V

u1

u2

uV

Page 23: Word2vec (中文)

… and softmax

context (target | context)

1

2

1

2

VV

Page 24: Word2vec (中文)

One-hot encoding as input

pigs

drink

water

men

eat

pigs

drink

water

men

eat

訓練完之後,希望每筆 training data 丟進去...

Page 25: Word2vec (中文)

pigs → drink

pigs

drink

water

men

eat

pigs

drink

water

men

eat

1

0

0

0

0

0

1

0

0

0

(target | context)… 能夠 output 近似的機率

Page 26: Word2vec (中文)

men → eat ; men → drink

pigs

drink

water

men

eat

pigs

drink

water

men

eat

0

0

0

1

0

0

0.5

0

0

0.5

(target | context)

Page 27: Word2vec (中文)

等等,那些點和線是怎麼回事

Page 28: Word2vec (中文)

Matrix operation

context

1

2

3

4

5

h1

h2

h3

42

V×D = { ki }

Page 29: Word2vec (中文)

Matrix operation

context

1

2

3

4

5

h1

h2

h3w:,2

V×D = { ki }

=

Page 30: Word2vec (中文)

Matrix operation

context (target | context)

1

2

3

4

5

1

2

3

4

5

W W ’

Page 31: Word2vec (中文)

Maximize p(target | context)

● 已知 training corpus: (context, target)○ (w(1), w(2)), (w(2), w(3)), (w(3), w(4)), …

● Maximize 條件機率的乘積

where

Page 32: Word2vec (中文)

我們能夠給前字推後字了

context (target | context)

1

2

3

4

5

1

2

3

4

5

W W ’

Page 33: Word2vec (中文)

所以我說那個 word2vec 呢?

context

W

pigs

drink

water

men

eat

v_drink

Page 34: Word2vec (中文)

1

2

3

4

5

所以我說那個 word2vec 呢?

把 input (1-hot) 編碼進去 hidden layer 預測 (target | context)

pigs

drink

water

men

eat

0

1

0

0

0

=

Page 35: Word2vec (中文)

Learning from context - By-product

● 在日本買了 yatsuhashi● 我分一塊 yatsuhashi 給朋友

● yatsuhashi 很甜

● __?__ yatsuhashi __?__

● 由看過眾多文本,訓練一個模型猜測 __?__○ 但非主要目的

● 目的:藉由學習的過程獲得 yatsuhashi 的特徵

Page 36: Word2vec (中文)

聯想

Page 37: Word2vec (中文)

Autoencoder

截圖自林軒田的教學影片 : 機器學習技法https://www.youtube.com/watch?v=eBVPQ4fgs_k&index=51&list=PLXVfgk9fNX2IQOYPmqjqWsNUFl2kpk1U2

Page 38: Word2vec (中文)

Recommendation (matrix factorization)

● User: input word (例如 context)● Item: output word (例如 target)● Rating: input word 和 output word 的契合度

Page 39: Word2vec (中文)

?2,2

ru,v

n

m

R PT Q

p2T

puT

q2 qv×

k

number of latent dimensions

Given: some ru,v

Find: p1, ..., pm and q1, ..., qn

Page 40: Word2vec (中文)

Foobar2Vec

● 把 word 帶換成任何東西 (item, queries/ads, …)● 用同樣的計算框架

Page 41: Word2vec (中文)

Why MAGIC??

1. 相似的字會在附近

○ 因為「相似」的字多半有同樣的 context2. 兩個字的「關係」可以用「減法」來表示

○ 我不知道...

syntacticsemantic

Page 42: Word2vec (中文)

Why MAGIC??

● 就算 A ≈ a , B ≈ b○ 所以 B - A ≈ b - a

● 但這不代表 B 會是最靠近 b - a + A 的那個?○ 因為我可能還有 ㄅ ≈ B ≈ b

● 更不用說 A ≈ a , B ≈ b,不代表 A - a 的方向性和 B - b 的方向性

○ B - A ≈ b - a 不見得代表 B - b ≈ A - a

A

a

B

b

Page 43: Word2vec (中文)

Train the network

Page 44: Word2vec (中文)

One training sample at a time

● 用 gradient descent: 算出目標函數的 gradient○ 移動 W, W ’ 的每個值,往 gradient 的反方向移動

● Stochastic gradient descent: 一次只看一個 word○ 原 gradient 是個別 gradient 的總和

● 掃過 wordI , wordO,每一次當做在解

做一次的 gradient 移動

Page 45: Word2vec (中文)

Back propagation

● 省略數學式

● 做一次 descent 的 computational complexity○ output → hidden: V × D○ hidden → input: D × 1 (因為 input 1-hot )

● Hierarchical softmax○ 把 V 個 output nodes 編碼成 V-leaf binary trees○ Walk through log(V) depth of nodes

● Negative sampling○ 看待成分類,而非機率估計

Page 46: Word2vec (中文)

Visual Demo

● https://ronxin.github.io/wevi/ from Xin Rong

Page 47: Word2vec (中文)

word2vec - CBOW and skip-gram

Page 48: Word2vec (中文)

What !?

Page 49: Word2vec (中文)

W

W

W

W● 每一個框是 V 個點 (或 D個

點)● 每”一條”線代表 V 個點和 D

個點的 full connections● Weight matrix 要一樣

Page 50: Word2vec (中文)

W

word2vec 的 vec 就是Input 到 projection layer 的變換

Page 51: Word2vec (中文)

1

2

3

4

5

1

2

3

4

5

W

W ’

wordt-1

wordt

Page 52: Word2vec (中文)

1

2

3

4

5

1

2

3

4

5

W

W ’

wordt-1

wordt

1

2

3

4

5

W`

wordt+1

Page 53: Word2vec (中文)

Evaluation / Application

Page 54: Word2vec (中文)

MAGIC!

1. 不但相似的字會在附近

2. 而且兩個字的「關係」可以用「減法」來表示

woman − man ≈ queen − king

syntacticsemantic

Page 55: Word2vec (中文)

Evaluation in the papers

● big : biggest = small : ???● France : Paris = Germany : ???● Accuracy and training time across

○ vec dimensionality○ Training corpus size

● I have a ???○ a) apple b) pen c) applepen○ 用 skip-gram network 本身預測

Page 56: Word2vec (中文)

Machine translation

Page 57: Word2vec (中文)

Feature Engineering

● 當做其他 learning model 的 features?

Page 58: Word2vec (中文)

雜感

Page 59: Word2vec (中文)

從不懂 NN 讀 word2vec

● 從構造發想,從公式解決

● 萬變不離其宗

○ FM vs. MF○ Shallow NN vs. MF

● 但是解決問題的 “feasibility” 很重要

○ 什麼情況用什麼手法?

● 有工具也很重要

Page 60: Word2vec (中文)

Reference

● word2vec: https://code.google.com/archive/p/word2vec/ , with the 2 word2vec papers and the tool

● Xin Rong’s explanation:○ “word2vec Parameter Learning Explained”, 2014.○ Visual inspector: https://ronxin.github.io/wevi/ ○ Tech talk:

https://www.youtube.com/watch?v=D-ekE-Wlcds

Page 62: Word2vec (中文)

Backup Slides

Page 63: Word2vec (中文)

三層會不會太少 太簡單

實際上更早有bengio提出一個方法,是更複雜的架構

Page 64: Word2vec (中文)

Negative sampling

● back propagation 時不用計算所有 V 個 softmax● 取而代之,當做在解分類問題

○ word(o) vs. 其他隨機取的字

● k: 5~20 for small data; 2~5 for big data (reported in Mikolov’s paper)

Page 65: Word2vec (中文)

Subsampling of frequent words

● Corpus 裡的每個字有一定機率會被踢掉

○ 出現頻率越高,越有可能被踢掉

● 所以罕見字的影響力稍微高一些