参考链接
FastText模型
- FastText模型是在skip-gram模型基础上提出来的,所有首需要回顾一下skip-gram模型,可以参考连接: skip-gram模型
- skip-gram模型图:
- 在skip-gram模型中对词汇表中每个词
w
w
w都对应着两个向量:
- 输人向量 u w u_w uw:是输入层到隐藏层层连接矩阵 W ∈ R V × N W∈R^{V×N} W∈RV×N的行向量
- 输出向量 v t v_t vt:是隐藏层到输出层的连接矩阵 W ′ ∈ R N × V W'∈R^{N×V} W′∈RN×V的列向量
- V V V是词汇表的大小,N是词向量的维度
FastText模型与Skip-gram模型相同部分
- FastTex模型与skip-gram模型隐藏层到输出层部分(即后半部分) 是一样的结构,都是一个将隐藏层状态向量 h t h_t ht输出到 s o f t m a x softmax softmax层得到词汇表各词的预测概率。
- 训练目标是一样的都是用当前词 w t w_t wt预测其上下文词集 C t C_t Ct
- s o f t m a x softmax softmax层也都是使用负采样 s o f t m a x softmax softmax层或者分层 s o f t m a x softmax softmax层进行优化。
FastText模型与Skip-gram模型不同部分
- FastTex模型与skip-gram模型区别在于:输出层到隐藏层部分(前部),即得到隐藏层状态向量 h t h_t ht方式:
- skip-gram模型:将当前词 w t w_t wt的one-hot编码与连接矩阵 W ∈ R V × N W∈R^{V×N} W∈RV×N相乘,得到词 w t w_t wt的输入向量 u w t u_{w_t } uwt作为隐藏层状态向量 h t h_t ht,即 h t = u w t h_t=u_{w_t } ht=uwt
- FastTex模型:将当前词的 w t w_t wt和该词的字符级的n-grams的one-hot编码相加,再将这个和与连接矩阵 W ∈ R V × N W∈R^{V×N} W∈RV×N相乘,得到隐藏层状态向量 h t h_t ht,该向量就是我们最终得到词 w t w_t wt的词向量(即FastTex模型的词向量)。计算隐藏层状态向量 h t h_t ht的细节下面进行详细解释。
字符级n-grams
- 下面举例子来说明符级n-grams(character n-grams):求词 where 的n-grams
- 在 where 前后加上 开始符< 和 结束符>,于是得到 <where>
- 我们取n-grams中 n = 3 n=3 n=3,得到 where 5个字符级tri-gram如下: < w h , w h e , h e r , e r e , r e > <wh, whe, her, ere, re> <wh,whe,her,ere,re>
- 那么 where 对应6个一个词和5个子词(Subword): < w h e r e > , < w h , w h e , h e r , e r e , r e > <where>,<wh, whe, her, ere, re> <where>,<wh,whe,her,ere,re>他们都有自己对应输入向量 u u u,将它们的输入向量求和就得到了词 where 的隐藏层状态向量 h w h e r e h_{where} hwhere, h w h e r e h_{where} hwhere也就是词 where 的词向量。
FastText模型的隐藏层计算方法
-
论文n-grams中n不是简单的取3,而是分别取3,4,5,6;这样可以得到更多的字符级n-grams(也叫子词)
-
下面讲述FastTex模型输出层到隐藏层的结构(论文中没有直接说明是我个人的理解不一定正确):
- 输入层词汇表
D
i
n
D_{in}
Din(输入层使用的词汇表):对于词汇表
D
D
D的每个词我们分别对其进行字符级n-grams提取并将这个字符级n-grams和原词一起加入输入层词汇表
D
i
n
D_{in}
Din。
- 比如对于词汇表
D
D
D中的 where 词:
- 原词: < w h e r e > <where> <where>
- 3-grams: < w h , w h e , h e r , e r e , r e > <wh, whe, her, ere, re> <wh,whe,her,ere,re>
- 4-grams: < w h , w h e , h e r , e r e , r e > <wh, whe, her, ere, re> <wh,whe,her,ere,re>
- 5-grams: < w h e r , w h e r e , h e r e > <wher, where, here> <wher,where,here>
- 6-grams: < w h e r e , w h e r e > <where, where> <where,where>
- 然后将它们都加入输入层词汇表 D i n D_{in} Din中
- 比如对于词汇表
D
D
D中的 where 词:
- 显然 D i n D_{in} Din比 D D D要大。我们将原词汇表 D D D叫做输出层词汇表 D o u t D_{out} Dout
- 因为输入层词汇表的改变所以输入层到隐藏层的连接矩阵由 W ∈ R ∣ D ∣ × N W∈R^{|D|×N} W∈R∣D∣×N变为 W ∈ R ∣ D i n ∣ × N W∈R^{|D_{in} |×N} W∈R∣Din∣×N, W W W的行有些是某个词(比如 where )的输入向量( u w h e r e u_{where} uwhere),有些是字符级n-grams(子词)( < w h , w h e , h e r <wh, whe, her <wh,whe,her等)的输入向量( u < w h u_{<wh} u<wh)
- 输入层词汇表
D
i
n
D_{in}
Din(输入层使用的词汇表):对于词汇表
D
D
D的每个词我们分别对其进行字符级n-grams提取并将这个字符级n-grams和原词一起加入输入层词汇表
D
i
n
D_{in}
Din。
-
隐藏层状态向量 h h h的计算方式:
- 首先获取当前输入出 where 的字符级n-grams: < w h , w h e , h e r , e r e , r e > , < w h , w h e , h e r , e r e , r e > < w h e r , w h e r e , h e r e > , < w h e r , w h e r e , h e r e > <wh, whe, her, ere, re>,<wh, whe, her, ere, re>\\<wher, where, here>,<wher, where, here> <wh,whe,her,ere,re>,<wh,whe,her,ere,re><wher,where,here>,<wher,where,here>
- 然后将原词的和字符级n-grams的one-hot编码进行累加得到输入向量 x w h e r e x_{where} xwhere,即将 < w h e r e <where <where>和 < w h , w h e , h e r , e r e , r e > , < w h , w h e , h e r , e r e , r e > , < w h e r , w h e r e , h e r e > , < w h e r , w h e r e , h e r e > <wh, whe, her, ere, re>,<wh, whe, her, ere, re>,<wher, where, here>,<wher, where, here> <wh,whe,her,ere,re>,<wh,whe,her,ere,re>,<wher,where,here>,<wher,where,here>的one-hot向量相加得到 x w h e r e x_{where} xwhere
- 将 x w h e r e x_{where} xwhere与连接矩阵 W ∈ R ∣ D i n ∣ × N W∈R^{|D_{in} |×N} W∈R∣Din∣×N相乘得到隐藏在状态向量 h w h e r e h_{where} hwhere,其实是将 < w h e r e > <where> <where>和 < w h , w h e , h e r , e r e , r e > , < w h , w h e , h e r , e r e , r e > , < w h e r , w h e r e , h e r e > , < w h e r , w h e r e , h e r e > <wh, whe, her, ere, re>,<wh, whe, her, ere, re>,<wher, where, here>,<wher, where, here> <wh,whe,her,ere,re>,<wh,whe,her,ere,re>,<wher,where,here>,<wher,where,here>的输入向量 u u u( W ∈ R ∣ D i n ∣ × N W∈R^{|D_{in} |×N} W∈R∣Din∣×N)中对应的行)进行相加得到 h w h e r e h_{where} hwhere
-
当FastTex模型训练完成后,where 的词向量就是将其输入后得到隐藏层状态向量 h w h e r e h_{where} hwhere
-
因为FastTex模型的使用字符级n-grams所以对于没有在训练集中出现的词也可以得到该词对词向量,因为这个词的字符级n-grams出现过。