Lecture 01 : Introduction and Word Vectors

Lecture 01 : Introduction and Word Vectors

slides 鏈接1 鏈接2

note 鏈接

vedio 鏈接

1.詞的表示

1.1 WordNet

  • missing nuance 無法表示細微的差別
  • missing new meaning of words
  • Subjective
  • 人力
  • 無法計算相似性

1.2 Onehot

  • 維度高
  • There is no natural notion of similarity for one-hot vectors! 任何兩個詞都是正交的,沒有計算相似性的方法。

解決方法: learn to encode similarity in the vectors themselves 學習在向量本身中編碼相似性

1.3 Word vectors

word vectors\word embeddings\word representations :distributed representation

Distributional semantics : A word’s meaning is given by the words that frequently appear close-by

2.SVD Based Methods

首先在一個大的數據集中循環計算共現矩陣X,然後對X進行奇異值分解得到USVTUSV^T,然後用U的行代表詞的詞向量。

2.1 Word-Document Matrix

word ii 出現在document jj 中:XijX_{ij} 這個矩陣很大RvMR^{|v|*M},因爲M非常大

2.2 Window based Co-occurrence Matrix

計算每個單詞在一個特定大小的窗口中出現的次數

2.3 Applying SVD to the cooccurrence matrix

得到X=USVTX=USV^T,根據所捕獲的期望方差百分比,在某個指數k處將它們截斷.

i=1kσii=1Vσi\frac{\sum_{i=1}^k \sigma _i}{\sum_{i=1}^{|V|} \sigma_i}

U1:V,1:kU_{1:|V|,1:k} 是我們的詞向量矩陣。

很難用在大型文檔

3.Iteration Based Methods - Word2vec

不需要全局信息,而是不斷迭代,一次學習一次迭代。按照一定的目標訓練模型。在每個迭代中,我們運行我們的模型,評估錯誤,並遵循一個更新規則,最終學習到詞向量。

  • Go through each position t in the text, which has a center word
    c and context (“outside”) words o

  • Use the similarity of the word vectors for c and o to calculate
    the probability of o given c (or vice versa) 需要再理解 根據c和o
    的相似性計算給定c得到o的概率或者相反。

  • Keep adjusting the word vectors to maximize this probability

兩種算法:

  1. Skip-grams (SG)

Predict context (”outside”) words (position independent) given centerword

  1. Continuous Bag of Words (CBOW)

Predict center word from (bag of) context words

訓練方法
1、Negative sampling
2、hierarchical softmax

3.1 Continuous Bag of Words Mode(CBOW)

VUV、U分別是輸入、輸出詞向量矩陣

steps

  1. 對上下文大小爲m的生成 one hot詞向量:(x(cm),...,x(c+m)RV)(x^{(c-m)},...,x^{(c+m)}\in R^{|V|})

  2. one hot詞向量依次與V相乘的到詞嵌入

  3. 得到平均向量v^=vcm+...+vc+m2m\hat v=\frac{v_{c-m}+...+v_{c+m}}{2m}

  4. 生成分數向量 z=Uv^RVz=U\hat v \in R^{|V|}

  5. 將分數轉化爲概率 y^=softmax(z)RV\hat y=softmax(z) \in R^{|V|}

  6. 希望y^\hat y匹配yyyy也是一個one hot 向量。

然後我們需要一個度量yy^y和\hat y距離的函數,我們採用cross entropy

H(y^,y)=j=1Vyjlog(y^j)H(\hat y,y)=-\sum_{j=1}^{|V|}y_j\log(\hat y_j)

所以:

minimizeJ=logP(wcwcm,...,wc+m)=logP(ucv^)=logexp(ucT)j=1Vexp(ujTv^)=ucTv^+logj=1Vexp(ujTv^) minimize J = -\log P(w_c|w_{c-m},...,w_{c+m}) \\ = -\log P(u_c|\hat v)\\ =-\log \frac{exp(u_c^T)}{\sum_{j=1}^{|V|}exp(u_j^T\hat v)}\\ =-u_c^T\hat v+\log \sum_{j=1}^{|V|}exp(u_j^T\hat v)

3.2 Skip-Gram Model

中心詞預測上下文,“跳轉爲上下文”,(Here we callthe word “jumped” the context)

steps:

  1. 生成中心詞的one hot 向量xRVx\in R^{|V|}

  2. 生成中心詞的詞嵌入 vc=VxRnv_c=Vx \in R^n

  3. 生成分數向量 z=Uvcz=Uv_c

  4. 轉爲概率 y^=softmax(z)\hat y=softmax(z)

5.希望yy^y與\hat y相近

然後需要有一個目標函數,我們用貝葉斯的假設條件,給定中心字,所有輸出字都是完全獨立的。

所以:

minimizeJ=logP(wcm,...,wc+mwc)=logj=0,jm2mP(ucm+jvc)=logj=0,jm2mexp(ucm+jTvc)k=1Vexp(ukTvc)j=0,jm2mucm+jTvc+2mlogk=1Vexp(ukTvc)minimize J=-\log P(w_{c-m},...,w_{c+m}|w_c)\\ =-\log \prod_{j=0,j\neq m}^{2m}P(u_{c-m+j}|v_c)\\ =-\log \prod_{j=0,j\neq m}^{2m}\frac{exp(u_{c-m+j}^T v_c)}{\sum{k=1}{|V|}exp(u_k^T v_c)}\\ -\sum_{j=0,j\neq m}{2m}u_{c-m+j}^{T}v_c+2m\log\sum_{k=1}^{|V|}exp(u_k^Tv_c)

3.3 Negative Sampling

觀察上面兩個模型的目標函數,每一次迭代都要在V|V|量的數據上計算,十分耗費時間。所以不在整個數據上計算,我們只採樣在一個噪聲分佈Pn(w)P_n(w)進行負樣本,其概率分佈與詞彙表頻率的排序相匹配。

假設word and context (w,c)(w,c),P(D=1w,c)P(D=0w,c)P(D=1|w,c)、P(D=0|w,c)分別表示w與c是正樣本和負樣本。

假設w c是正樣本的概率爲:

P(D=1w,c,θ)=σ(vcTvw)=11+e(vcTvw)P(D=1|w,c,\theta)=\sigma(v_c^T v_w)=\frac{1}{1+e^{(-v_c^T v_w)}}

重新定義目標函數:

在這裏插入圖片描述
在這裏插入圖片描述
對於skip-gram:
在這裏插入圖片描述

對於CBOW:
在這裏插入圖片描述

{u^kk=1,...,K}\{\hat u_k|k=1,...,K\}是從Pn(w)P_n(w)中採樣的。那麼Pn(w)P_n(w)是什麼樣的呢?

what seems to work best isthe Unigram Model raised to the power of 3/4.爲什麼是3/4呢?

在這裏插入圖片描述

3.4 Hierarchical Softmax

在實際應用中,對於不常用的詞,Hierarchical Softmax的效果較好,而對於常用的詞和低維向量,負採樣效果較好。

樹每個葉子節點表示一個詞,

In this model, the probability of a word w given a vector wi,P(w|wi), is equal to the probability of a random walk starting in the root and ending in the leaf node corresponding to w. cost is O(logV)O(\log|V|)

L(w)L(w) : the number of nodes in the path from the root to the leaf w

n(w,i)n(w,i) : the i-th node on this path with associated vector Vn(w,i)V_{n(w,i)}n(w,1)n(w,1) 是根節點,n(w,L(w))n(w,L(w))是w的父親節點。

對於每一個內部節點 inner node n,我們選擇一個孩子節點ch(n)(always the left node)

所以概率爲:

在這裏插入圖片描述

並且保證了
σ(vnTvwi)+σ(vnTvwi)=1\sigma(v_n^T v_{w_i})+\sigma(-v_n^T v_{w_i})=1
w=1VP(wwi)=1\sum_{w=1}^{|V|}P(w|w_i)=1

To train the model, our goal is still to minimize the negative loglikelihood−logP(w|wi). But instead of updating output vectors per word, we update the vectors of the nodes in the binary tree that arein the path from root to leaf node.
The speed of this method is determined by the way in which the binary tree is constructed and words are assigned to leaf nodes.Mikolov et al. use a binary Huffman tree, which assigns frequentwords shorter paths in the tree.

3.5 objective function

likelihood:

L(θ)=t=1Tmjm;j0P(wt+jwt;θ)L(\theta)=\prod_{t=1}^T\prod_{-m\leq j\leq m;j\neq 0}P(w_{t+j}|w_t;\theta)

objective function J(θ)J(\theta)

J(θ)=1TlogL(θ)J(\theta)=-\frac{1}{T}\log{L(\theta)}

最小化目標函數就是最大化預測概率

vwv_w :w is a center word
uwu_w :w is a context word

P(oc)=exp(uoTvc)wVexp(uwTvc)P(o|c)=\frac{exp(u_o^T v_c)}{\sum_{w\in V} exp(u_w^T v_c)}

  • Exponentiation makes anything positive 冪運算使值爲正
  • every word has two vectors v和u向量

理解:在wt+jwtw_{t+j}和w_t知道時P(wt+jwt;θ)P(w_{t+j}|w_t;\theta)代表已知的這種結果不同的參數θ\theta發生這種結果的概率,使乘積最大就是最大可能的參數。P()P()是我們假設的概率分佈模型。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章