其他分享
首页 > 其他分享> > 《word2vec Parameter Learning Explained》论文笔记

《word2vec Parameter Learning Explained》论文笔记

作者:互联网

word2vec Parameter Learning Explained

文章目录

Abstract

The word2vec model and application by Mikolov et al. have attracted a great amount of attention in recent two years. The vector representations of words learned by word2vec models have been shown to carry semantic meanings and are useful in various NLP tasks. As an increasing number of researchers would like to experiment with word2vec or similar techniques, I notice that there lacks a material that comprehensively explains the parameter learning process of word embedding models in details, thus preventing researchers that are non-experts in neural networks from understanding the working mechanism of such models.

Mikolov等人提出的word2vec模型及其应用,在近两年引起了广泛的关注。基于word2vec模型学到的单词向量表示,已经被证明具有语义意义,同时在各种NLP任务中也是有帮助的。越来越多的研究人员,希望使用word2vec或类似的技术,(但在同时)我注意到,目前缺乏一份材料,用于全面、详细地解释 词Embedding模型 的参数学习过程,(这)导致研究者们难以理解这种模型的工作机制,尤其对于不是神经网络专家的研究者。

This note provides detailed derivations and explanations of the parameter update equations of the word2vec models, including the original continuous bag-of-word (CBOW) and skip-gram (SG) models, as well as advanced optimization techniques, including hierarchical softmax and negative sampling. Intuitive interpretations of the gradient equations are also provided alongside mathematical derivations.

本文给出了word2vec模型的参数更新方程的详细推导和解释,包括原始的 连续词袋(CBOW)模型跳跃图(skip-gram, SG)模型,以及先进的优化技术,包括 分层softmax负采样。同时提供了梯度方程的直观解释,以及数学推导。

In the appendix, a review on the basics of neuron networks and backpropagation is provided. I also created an interactive demo, wevi, to facilitate the intuitive understanding of the model.

在附录中,回顾了神经网络和反向传播相关基础,同时创建了一个交互式演示——wevi,便于模型的直观理解。

1. Continuous Bag-of-Word Model

基本结构:

1.1 上下文为单个词 One-word context

我们从Mikolov等人引入的连续词袋模型(CBOW)的最简单版本开始。
We start from the simplest version of the continuous bag-of-word model (CBOW) introduced in Mikolov et al. (2013a).

Mikolov, T., Chen, K., Corrado, G., and Dean, J. (2013a). Efficient estimation of word representations in vector space. arXiv preprint arXiv:1301.3781.

假设每个上下文只考虑一个单词,即给定一个上下文单词后,模型预测一个目标词,就像一个双词模型
We assume that there is only one word considered per context, which means the model will predict one target word given one context word, which is like a bigram model.

(1)模型结构

FIG1

PS: W ′ W' W′并不是 W W W的转置

(2)输入层 -> 隐藏层

对于第k个词,编码为ont-hot向量 x x x,即 x x x中第k个元素为1,其余元素为0。将其作为模型输入:

h = W T x = W ( k , ⋅ ) T = W ( k , ⋅ ) T : = v w I T (1) h = W^T x = W^T_{(k, \cdot)} = W_{(k, \cdot)}^T := v_{w_I}^T \tag{1} h=WTx=W(k,⋅)T​=W(k,⋅)T​:=vwI​T​(1)

即 W W W 可作为(一种)word2vec查询表(Look-Up-Table):第k个单词(输入词 w I w_I wI​)的Embedding向量,就是矩阵 W W W的第k行(再转置) v w I T v_{w_I}^T vwI​T​。

其中, v w I T v_{w_I}^T vwI​T​被称为输入向量;可视作输入词作为中心词时候的Embedding。

x x x右乘 W T W^T WT,即对 W T W^T WT列变换,由于 x x x为one-hot编码(假设 x k = 1 x_k=1 xk​=1),即取 W T W^T WT某一列(第k列),等同于取 W W W某一行(第k行)。

(3)隐藏层 -> 输出层

对于输出层的第j个节点,输出的是一个分数值 u j u_j uj​:

u j = W ( : , j ) ′ T h = : v w j ′ T h (2) u_j = {W'_{(:, j)}}^T h = :{v'_{w_j}}^T h \tag{2} uj​=W(:,j)′​Th=:vwj​′​Th(2)

其中 v w j ′ v'_{w_j} vwj​′​为 W ′ W' W′的第j列,被称为输出向量;可视作输入词作为上下文时候的Embedding。

分数值 u j u_j uj​表示输入词为 w I w_I wI​时,其上下文为单词 w j w_j wj​的分值,不具有概率意义,即和不为一:

∑ j = 1 N u j ≠ 1 \sum_{j=1}^{N} u_j \neq 1 j=1∑N​uj​​=1

因此,为使得模型输出具有概率意义,需要添加softmax函数,获得条件概率分布:

p r o b ( w j ∣ w I ) = e x p ( u j ) ∑ l = 1 V e x p ( u l ) : = y j (3) prob(w_j|w_I) = \frac{exp(u_j)}{\sum_{l=1}^{V} exp(u_l)} := y_j \tag{3} prob(wj​∣wI​)=∑l=1V​exp(ul​)exp(uj​)​:=yj​(3)

相当于对输出层使用softmax激活函数。上面输入层隐藏层相当于没有激活函数,为线性关系。

(4)模型意义

整合上面公式,得到条件概率分布:

p r o b ( w j ∣ w I ) = e x p ( v w j ′ T v w I ) ∑ l = 1 V e x p ( v w l ′ T v w I ) = y j (4) prob(w_j|w_I) = \frac{exp({v'_{w_j}}^T v_{w_I})}{\sum_{l=1}^{V} exp({v'_{w_l}}^T v_{w_I})} = y_j \tag{4} prob(wj​∣wI​)=∑l=1V​exp(vwl​′​TvwI​​)exp(vwj​′​TvwI​​)​=yj​(4)

可理解为,word2vec模型维护了两套向量表示,分别为:

输入词输出词的条件概率分布,被建模为:输入词输入向量,与输出词输出向量,两者之间计算内积,再softmax归一化。其中内积起到一种相似性度量的作用。

PS:可以类比Transformer的Multi-Head Attention中的Scaled Dot-Product Attention,每个单词拥有三套表示:Query, Key和Value.

(5)更新方程:隐藏层 -> 输出层

后面开始推导上述模型的参数更新方程。尽管实际的计算过程并不是按照本节的推导进行的,或者说本节的推导并不实用(具体原因后面会解释,见第3节,通过一些trick或近似,使得模型求解更加实用),但是依然想通过微分推导,获得对最原始模型(未使用trick)的直观理解。

在这里插入图片描述

根据Eq(5-7),

E = − l o g [ p ( w O ∣ w I ) ] = l o g ∑ l = 1 V e x p ( u l ) − u j ∗ E = -log \Big[p(w_O|w_I)\Big] = log \sum_{l=1}^V exp(u_l) - u_{j*} E=−log[p(wO​∣wI​)]=logl=1∑V​exp(ul​)−uj∗​

关于 u j u_j uj​,对 E E E的第1项求导,正好是 y j y_j yj​(参照Eq3中 y j y_j yj​的定义);对其第2项求导,是 j = j ∗ j=j* j=j∗的示性函数,于是有Eq8.

Eq9中第二项,参照Eq2中 u j u_j uj​的定义和Eq1中 v w j ′ v'_{w_j} vwj​′​的定义.

于是,基于随机梯度下降,得到 隐藏层 -> 输出层的权重 w i j ′ w'_{ij} wij′​的更新公式Eq10,或表示为向量的形式,即Eq11.

Eq11中,所有词的输出向量都需要更新;对比后面的Eq16,只有输入词输入向量需要更新。

直观理解

给定一个输入词后,从词表中遍历所有可能的输出词,例如词表中第j个词,检查模型对其概率密度估计 y j y_j yj​,并与期望值 t j t_j tj​(即ground truch)比较。

再次指出,输入向量表示 v w v_w vw​和输入向量表示 v w ′ v'_w vw′​,是同一单词 w w w的两种不同表示方式。

(6)更新方程:输入层 -> 隐藏层

得到 E E E关于 W ′ W' W′的更新公式之后,根据链式法则,后面继续推导 E E E关于 W W W的更新公式。

在这里插入图片描述

Eq12得到 E E E关于隐层节点 h i h_i hi​的偏导,是预测误差 e j = y j − t j e_j=y_j - t_j ej​=yj​−tj​根据 w i j ′ w'_{ij} wij′​加权求和后的结果,简记为 E H i EH_i EHi​.

E E E关于隐层所有节点 h h h的偏导,可简记为 E H EH EH,是一个N维(列)向量(对应N个隐层节点)。

Eq13为Eq1的另一种表示,便于Eq14的推导表示。

Eq14中,下角标k对应第k个输入词,i对应第i个隐层单元。Eq14可以表示为张量积的形式,得到Eq15.

Eq15中, ∂ E ∂ W \frac{\partial E}{\partial W} ∂W∂E​为V行N列,x为V行1列,EH为N行1列。

考虑到one-hot向量x的稀疏性(例如 x k = 1 x_k=1 xk​=1,即 w I = W k w_I = W_k wI​=Wk​), ∂ E ∂ W \frac{\partial E}{\partial W} ∂W∂E​ 中只有一行(第k行)是非零的。

x左乘 E H T EH^T EHT 即对 E H T EH^T EHT 行变换,取其一行,而 E H T EH^T EHT也只有一行。即创建一个V行N列的零矩阵,再将 E H T EH^T EHT复制到其第k行。

因此, W W W中仅有一行会被更新到,即第k行,即仅输入词 w I w_I wI​对应的那一行会更新;更新方式为向负梯度方向移动,步长为一定比例(学习率)的 x k ⋅ E H T = E H T x_k \cdot EH^T = EH^T xk​⋅EHT=EHT。于是有Eq16。

除了输入词 w I w_I wI​之外,其他词 w ≠ w I w \neq w_I w​=wI​的输入向量 v w v_w vw​不作更新。

对比前面的Eq11,所有词的输出向量都需要更新。

直观理解

直观的,向量 E H EH EH是词汇表中所有单词输出向量的加权和,权重系数为预测误差 e j = y j − t j e_j=y_j - t_j ej​=yj​−tj​,于是Eq16可以被理解为,将词汇表中每个词的输出向量,按照一定比例,叠加输入词输入向量上。

当我们使用训练语料库,生成 上下文-目标词 对,迭代更新模型参数时,(上面提到的)向量之间的影响会逐渐累积
As we iteratively update the model parameters by going through context-target word pairs generated from a training corpus, the effects on the vectors will accumulate.

可以想象,某个单词w的输出向量,被其 共现邻居输入向量前后拖动,就像有一条绳子一样,连接在单词w和它的相邻词的表示向量中间。
We can imagine that the output vector of a word w is dragged" back-and-forth by the input vectors of w’s co-occurring neighbors, as if there are physical strings between the vector of w and the vectors of its neighbors.

类似地,输入向量也可以被认为是被许多输出向量拖动的。
Similarly, an input vector can also be considered as being dragged by many output vectors.

这种解释可以让我们想起 重力 或者 受力分析图。
This interpretation can remind us of gravity, or force-directed graph layout.

每条虚拟绳子的平衡长度,与关联词对之间的共现强度有关,也与学习率有关。

The equilibrium length of each imaginary string is related to the strength of cooccurrence between the associated pair of words, as well as the learning rate.

经过多次迭代,输入向量输出向量之间的相对位置,最终将达到稳定。
After many iterations, the relative positions of the input and output vectors will eventually stabilize.

1.2 上下文为多个词 Multi-word context

CBOW模型:多个输入词,如Fig2。

在这里插入图片描述
在这里插入图片描述

隐层单元:不再是直接从输入词输入向量中复制(参照Eq1中,ont-hot编码的x右乘矩阵 W W W),而是对C个输入词输入向量计算平均值,于是得到Eq(17-18)。

损失函数:Eq21,与Eq7基本相同。Eq21对Eq7中的 u j u_j uj​项进行了展开,便于说明隐层单元 h h h的计算存在区别。

更新方程

2. Skip-Gram Model

和CBOW相反,基本结构:

(1)模型结构

在这里插入图片描述

(2)输入层 -> 隐藏层

Eq24: 和CBOW相同,对比Eq1;

(3)隐藏层 -> 输出层

Eq25: C个多项式分布。和CBOW相似,对比Eq3;
Eq26: Softmax之前的输出节点(分值),对比CBOW的Eq2;

在这里插入图片描述

(4)损失函数E

Eq29: 可视作按照Eq7对多个输出词 w c w_c wc​分别计算损失 E c E_c Ec​,再累加求和,对比CBOW的Eq7(单词上下文)和Eq21(多词上下文);
Eq30:由于Eq29中最终是求和关系,于是 ∂ E ∂ u c , j = ∂ E c ∂ u c , j \frac{\partial E}{\partial u_{c,j}} = \frac{\partial E_c}{\partial u_{c,j}} ∂uc,j​∂E​=∂uc,j​∂Ec​​,与CBOW的Eq8相同。
Eq31: 定义V维向量 E I EI EI,作为 c个输出词上的总误差,便于后续表示;

便于理解的,Skip-Gram中的 E I j EI_j EIj​,对应CBOW中的 e j e_j ej​;

相当于Skip-Gram的每个输出节点(例如第j个节点),有C个输出误差 e c , j e_{c,j} ec,j​, E H j EH_j EHj​对其无差别求和,从C维向量压缩到了1维标量;

在这里插入图片描述

(5)更新方程:隐藏层 -> 输出层

Eq(32-34),依次类比CBOW的Eq(9-11);其中Eq11(单词上下文)和Eq22(多词上下文)基本一致。

便于理解的,Skip-Gram中的 E I j EI_j EIj​,对应CBOW中的 e j e_j ej​;

(6)更新方程:输入层 -> 隐藏层

Eq35,类比CBOW的Eq(12-16)的推导过程;
Eq36,类比CBOE的Eq12中对 E H i EH_i EHi​的定义。

便于理解的,Skip-Gram中的 E I j EI_j EIj​,对应CBOW中的 e j e_j ej​;

在这里插入图片描述

3 Optimizing Computational Effciency

3.1 Hierarchical Softmax

在这里插入图片描述

3.2 Negative Sampling

标签:输出,word2vec,Explained,wI,向量,Learning,CBOW,隐藏,输入
来源: https://blog.csdn.net/qq_36870202/article/details/122441344