SlideShare a Scribd company logo
1 of 65
Download to read offline
word2vec
word2vec 之拼圖冒險
Yiwei Chen
2016.10
關於這份投影片
● 不熟 NN 的人接觸 word2vec 的心得
● 只有概念,沒有實作
Agenda
● Word embedding
● word2vec
● word2vec learning - simplified
● word2vec - CBOW and skip-gram
● Evaluation / Applications
Word Embedding
Represent a word: R1
V 個字: apple, bag, cat, …
● 以 ID 表示
● 沒有遠近關係
● 沒有特別意義
apple 1
bag 2
cat 3
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)
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
...
Represent a word: RD
● 能否把所有字放在較低維度 vector space
○ D 通常比 V 還小很多
● 且「相似」的字分佈在附近
water
milk
Paris
London
Represent a word: RD
● Continuous distributed word representations
● 低維度向量
● 「相似」的字分佈在附近
water
milk
Paris
London
Word
Embedding
word2vec
word2vec
● 其中一種生出 word embedding 的技巧
● 2013 由 Tomas Mikolov 等人提出
● 同時有 open source 程式 (in C)
MAGIC!
1. 不但相似的字會在附近
2. 而且兩個字的「關係」可以用「減法」來表示
woman
− man
≈ queen
− king
syntacticsemantic
Learning from context
● 我在日本買了 yatsuhashi
● 分一塊 yatsuhashi 給朋友
● yatsuhashi 很甜
用 yatsuhashi 造句: __?__ yatsuhashi __?__
Learning from context - By-product
● 在日本買了 yatsuhashi
● 我分一塊 yatsuhashi 給朋友
● yatsuhashi 很甜
● __?__ yatsuhashi __?__
● 由看過眾多文本,訓練一個模型猜測 __?__
○ 但非主要目的
● 目的:藉由學習的過程獲得 yatsuhashi 的特徵
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” 也值得一看)
可是我看不懂...
What !?
An intro paper
● “word2vec Parameter Learning Explained” from
Xin Rong
● 給不熟 Neural Network 的人
● 專注在 word2vec learning 數學式上
word2vec learning - simplified
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
○ ...
Feedforward NN, linear activation
context target
x1
2
V
u1
u2
uV
… and softmax
context (target | context)
1
2
1
2
VV
One-hot encoding as input
pigs
drink
water
men
eat
pigs
drink
water
men
eat
訓練完之後,希望每筆 training data 丟進去...
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 近似的機率
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)
等等,那些點和線是怎麼回事
Matrix operation
context
1
2
3
4
5
h1
h2
h3
42
V×D
= { ki
}
Matrix operation
context
1
2
3
4
5
h1
h2
h3w:,2
V×D
= { ki
}
=
Matrix operation
context (target | context)
1
2
3
4
5
1
2
3
4
5
W W ’
Maximize p(target | context)
● 已知 training corpus: (context, target)
○ (w(1), w(2)), (w(2), w(3)), (w(3), w(4)), …
● Maximize 條件機率的乘積
where
我們能夠給前字推後字了
context (target | context)
1
2
3
4
5
1
2
3
4
5
W W ’
所以我說那個 word2vec 呢?
context
W
pigs
drink
water
men
eat
v_drink
1
2
3
4
5
所以我說那個 word2vec 呢?
把 input (1-hot) 編碼進去 hidden layer 預測 (target | context)
pigs
drink
water
men
eat
0
1
0
0
0
=
Learning from context - By-product
● 在日本買了 yatsuhashi
● 我分一塊 yatsuhashi 給朋友
● yatsuhashi 很甜
● __?__ yatsuhashi __?__
● 由看過眾多文本,訓練一個模型猜測 __?__
○ 但非主要目的
● 目的:藉由學習的過程獲得 yatsuhashi 的特徵
聯想
Autoencoder
截圖自林軒田的教學影片 : 機器學習技法
https://www.youtube.com/watch?v=eBVPQ4fgs_k&index=51&list=PLXVfgk9fNX2IQOYPmqjqWsNUFl2kpk1U2
Recommendation (matrix factorization)
● User: input word (例如 context)
● Item: output word (例如 target)
● Rating: input word 和 output word 的契合度
?2,2
ru,v
n
m
≅
R PT
Q
p2
T
pu
T
q2
qv×
k
number of latent dimensions
Given: some ru,v
Find: p1
, ..., pm
and q1
, ..., qn
Foobar2Vec
● 把 word 帶換成任何東西 (item, queries/ads, …)
● 用同樣的計算框架
Why MAGIC??
1. 相似的字會在附近
○ 因為「相似」的字多半有同樣的 context
2. 兩個字的「關係」可以用「減法」來表示
○ 我不知道...
syntacticsemantic
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
Train the network
One training sample at a time
● 用 gradient descent: 算出目標函數的 gradient
○ 移動 W, W ’ 的每個值,往 gradient 的反方向移動
● Stochastic gradient descent: 一次只看一個 word
○ 原 gradient 是個別 gradient 的總和
● 掃過 wordI
, wordO
,每一次當做在解
做一次的 gradient 移動
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
○ 看待成分類,而非機率估計
Visual Demo
● https://ronxin.github.io/wevi/ from Xin Rong
word2vec - CBOW and skip-gram
What !?
W
W
W
W
● 每一個框是 V 個點 (或 D個
點)
● 每”一條”線代表 V 個點和 D
個點的 full connections
● Weight matrix 要一樣
W
word2vec 的 vec 就是
Input 到 projection layer 的變換
1
2
3
4
5
1
2
3
4
5
W
W ’
wordt-1
wordt
1
2
3
4
5
1
2
3
4
5
W
W ’
wordt-1
wordt
1
2
3
4
5
W
`
wordt+1
Evaluation / Application
MAGIC!
1. 不但相似的字會在附近
2. 而且兩個字的「關係」可以用「減法」來表示
woman
− man
≈ queen
− king
syntacticsemantic
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 本身預測
Machine translation
Feature Engineering
● 當做其他 learning model 的 features?
雜感
從不懂 NN 讀 word2vec
● 從構造發想,從公式解決
● 萬變不離其宗
○ FM vs. MF
○ Shallow NN vs. MF
● 但是解決問題的 “feasibility” 很重要
○ 什麼情況用什麼手法?
● 有工具也很重要
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
Reference
● 歷史:
http://sebastianruder.com/word-embeddings-1/ind
ex.html
● 應用 - 語言翻譯:Exploiting Similarities among
Languages for Machine Translation. T. Mikolov et
al, 2013
● 該取 input 或 output representation:Combining
Distributed Vector Representations for Words. J.
Garten et al, 2015.
Backup Slides
三層會不會太少 太簡單
實際上更早有bengio提出一個方法,是更複雜的架構
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)
Subsampling of frequent words
● Corpus 裡的每個字有一定機率會被踢掉
○ 出現頻率越高,越有可能被踢掉
● 所以罕見字的影響力稍微高一些

More Related Content

What's hot

2009年ERP提案簡報
2009年ERP提案簡報2009年ERP提案簡報
2009年ERP提案簡報Sam Yang
 
4 1 詳細設計法_長瀬_新
4 1 詳細設計法_長瀬_新4 1 詳細設計法_長瀬_新
4 1 詳細設計法_長瀬_新dentohcomit
 
やさしく知りたいC言語
やさしく知りたいC言語やさしく知りたいC言語
やさしく知りたいC言語uru nru
 
企業資源規劃(Erp)系統導入規劃
企業資源規劃(Erp)系統導入規劃企業資源規劃(Erp)系統導入規劃
企業資源規劃(Erp)系統導入規劃Simon Huang
 
PGBR2015 - O uso de funções de similaridade e distância entre strings adaptad...
PGBR2015 - O uso de funções de similaridade e distância entre strings adaptad...PGBR2015 - O uso de funções de similaridade e distância entre strings adaptad...
PGBR2015 - O uso de funções de similaridade e distância entre strings adaptad...Diogo L. V. G. Rubert
 
Pythonとdeep learningで手書き文字認識
Pythonとdeep learningで手書き文字認識Pythonとdeep learningで手書き文字認識
Pythonとdeep learningで手書き文字認識Ken Morishita
 
PEP8を読んでみよう
PEP8を読んでみようPEP8を読んでみよう
PEP8を読んでみよう2bo 2bo
 
xtsパッケージで時系列解析
xtsパッケージで時系列解析xtsパッケージで時系列解析
xtsパッケージで時系列解析Nagi Teramo
 
專案管理 週年慶購物計畫 李媛惠
專案管理 週年慶購物計畫 李媛惠專案管理 週年慶購物計畫 李媛惠
專案管理 週年慶購物計畫 李媛惠vincent su
 
DataRobotによる予測モデルを用いた シミュレーションと最適化(抜粋)
DataRobotによる予測モデルを用いた シミュレーションと最適化(抜粋)DataRobotによる予測モデルを用いた シミュレーションと最適化(抜粋)
DataRobotによる予測モデルを用いた シミュレーションと最適化(抜粋)Yuya Yamamoto
 
企業における自然言語処理技術利用の最先端
企業における自然言語処理技術利用の最先端企業における自然言語処理技術利用の最先端
企業における自然言語処理技術利用の最先端Yuya Unno
 

What's hot (17)

2009年ERP提案簡報
2009年ERP提案簡報2009年ERP提案簡報
2009年ERP提案簡報
 
BERTによる文書系AIの取り組みと、Azureを用いたテーブルデータの説明性実現!
BERTによる文書系AIの取り組みと、Azureを用いたテーブルデータの説明性実現!BERTによる文書系AIの取り組みと、Azureを用いたテーブルデータの説明性実現!
BERTによる文書系AIの取り組みと、Azureを用いたテーブルデータの説明性実現!
 
第2回DARM勉強会
第2回DARM勉強会第2回DARM勉強会
第2回DARM勉強会
 
4 1 詳細設計法_長瀬_新
4 1 詳細設計法_長瀬_新4 1 詳細設計法_長瀬_新
4 1 詳細設計法_長瀬_新
 
optimization c code on blackfin
optimization c code on blackfinoptimization c code on blackfin
optimization c code on blackfin
 
やさしく知りたいC言語
やさしく知りたいC言語やさしく知りたいC言語
やさしく知りたいC言語
 
企業資源規劃(Erp)系統導入規劃
企業資源規劃(Erp)系統導入規劃企業資源規劃(Erp)系統導入規劃
企業資源規劃(Erp)系統導入規劃
 
PGBR2015 - O uso de funções de similaridade e distância entre strings adaptad...
PGBR2015 - O uso de funções de similaridade e distância entre strings adaptad...PGBR2015 - O uso de funções de similaridade e distância entre strings adaptad...
PGBR2015 - O uso de funções de similaridade e distância entre strings adaptad...
 
C言語講習会1
C言語講習会1C言語講習会1
C言語講習会1
 
Pythonとdeep learningで手書き文字認識
Pythonとdeep learningで手書き文字認識Pythonとdeep learningで手書き文字認識
Pythonとdeep learningで手書き文字認識
 
SEM分析の理論
SEM分析の理論SEM分析の理論
SEM分析の理論
 
PEP8を読んでみよう
PEP8を読んでみようPEP8を読んでみよう
PEP8を読んでみよう
 
xtsパッケージで時系列解析
xtsパッケージで時系列解析xtsパッケージで時系列解析
xtsパッケージで時系列解析
 
專案管理 週年慶購物計畫 李媛惠
專案管理 週年慶購物計畫 李媛惠專案管理 週年慶購物計畫 李媛惠
專案管理 週年慶購物計畫 李媛惠
 
DataRobotによる予測モデルを用いた シミュレーションと最適化(抜粋)
DataRobotによる予測モデルを用いた シミュレーションと最適化(抜粋)DataRobotによる予測モデルを用いた シミュレーションと最適化(抜粋)
DataRobotによる予測モデルを用いた シミュレーションと最適化(抜粋)
 
企業における自然言語処理技術利用の最先端
企業における自然言語処理技術利用の最先端企業における自然言語処理技術利用の最先端
企業における自然言語処理技術利用の最先端
 
HiroshimaR5_Intro
HiroshimaR5_IntroHiroshimaR5_Intro
HiroshimaR5_Intro
 

Word2vec (中文)