文章目录
【一】 Word2Vec(词向量)
- Word2Vec,作为 NLP 领域深度学习的基础,是每个工程师都绕不过的重点话题。虽然从18年年底就诞生出了 Bert,GPT 等预训练模型,横扫 NLP 领域,但其还是基于 Word2Vec 的向量和思想训练出来的。
【1.1】 Skip-Gram(通过 中心词 预测 上下文)
![](https://i-blog.csdnimg.cn/blog_migrate/00ed0969ef871186747b66d97ba27bda.png)
-
U
U
U(Context,作为上下文),
V
V
V(Center,作为中心词) 两个矩阵
- f ( U , V ) = P ( W i ∣ W j ) f (U, V) = P (W_i | W_j) f(U,V)=P(Wi∣Wj),基于假设, W i , W j W_i, W_j Wi,Wj 的相似度越高,概率值越大
![](https://i-blog.csdnimg.cn/blog_migrate/30c9ab950990ff7db0c91360427dcc61.png)
- 直接思路
S
o
f
t
m
a
x
Softmax
Softmax 形式算出概率最大的那个单词 (
∣
V
∣
|V|
∣V∣ 表示词库)
P ( W i ∣ W j ; θ ) = e U W i × V W j ∑ c ϵ ∣ V ∣ e U c × V W j P (W_i | W_j ; θ) = {\frac{e^{\bf U_{W_i}\times V_{W_j}}}{\sum_{c\,\epsilon |V|}{e^{\bf U_{c}\times V_{W_j}}}}} P(Wi∣Wj;θ)=∑cϵ∣V∣eUc×VWjeUWi×VWj
- 目标函数 (
∣
V
∣
|V|
∣V∣ 表示词库,不好优化,
∣
V
∣
|V|
∣V∣ 词库可能非常大,通常采用 Negative Sampling)
L = arg max θ ∏ w ϵ c e n t e r ∏ c ϵ c o n t e x t e U c × V w ∑ c ′ ϵ ∣ V ∣ e U c ′ × V W {\bf L} = \mathop{\arg\max}_{\theta} \; \prod_{w\,\epsilon\, center} \; \prod_{c\,\epsilon\, context} {\frac{e^{\bf U_{c}\times V_{w}}}{\sum_{c^{'}\epsilon \,|V|}{e^{\bf U_{c^{'}}\times V_{W}}}}} L=argmaxθwϵcenter∏cϵcontext∏∑c′ϵ∣V∣eUc′×VWeUc×Vw
= arg max θ ∑ w ϵ c e n t e r ∑ c ϵ c o n t e x t U c × V w − l o g ∑ c ′ ϵ ∣ V ∣ e U c ′ × V w = \mathop{\arg\max}_{\theta} \; \sum_{w\,\epsilon\, center} \; \sum_{c\,\epsilon\, context} {\bf U_{c}}\times{\bf V_{w}} -log \sum_{{c^{'}}\epsilon\, |V|} {{e}^{\bf U_{c^{'}}\times \,{\bf V_w}}} =argmaxθwϵcenter∑cϵcontext∑Uc×Vw−logc′ϵ∣V∣∑eUc′×Vw
- 巧妙思路(表示成 LR 的分类形式)
P ( W i ∣ W j ; θ ) → P ( D = 1 ∣ W i , W j ; θ ) = 1 1 + e ( − U W i × V W j ) P (W_i | W_j ; θ) \;→\; P(D=1\,|\,W_i,W_j;\,\theta) = {\frac{1}{1+{e^{(-U_{W_i}\times V_{W_j})}}}} P(Wi∣Wj;θ)→P(D=1∣Wi,Wj;θ)=1+e(−UWi×VWj)1
- 目标函数(
D
=
1
D=1
D=1 表示
W
i
W_i
Wi 和
W
j
W_j
Wj 同时出现,
D
′
=
0
D^{'}=0
D′=0 表示
W
i
W_i
Wi 和
W
j
W_j
Wj 不能同时出现)
L = arg max θ ∏ ( w , c ) ϵ D P ( D = 1 ∣ W i , W j ; θ ) ∏ ( w , c ) ϵ D ′ P ( D ′ = 0 ∣ W i , W j ; θ ) {\bf L} = \mathop{\arg\max}_{\theta} \; \prod_{{(w,\,c)}\,\epsilon \,D} \; P(D=1\,|\,W_i,W_j;\,\theta) \; \prod_{{(w,\,c)}\,\epsilon \,D^{'}} \; P(D^{'}=0\,|\,W_i,W_j;\,\theta) L=argmaxθ(w,c)ϵD∏P(D=1∣Wi,Wj;θ)(w,c)ϵD′∏P(D′=0∣Wi,Wj;θ)
= arg max θ ∏ ( w , c ) ϵ D 1 1 + e ( − U c × V w ) ∏ ( w , c ) ϵ D ′ 1 1 + e ( U c × V w ) = \mathop{\arg\max}_{\theta} \; \prod_{{(w,\,c)}\,\epsilon \,D} \; {\frac{1}{1+{e^{(-U_c\times V_w)}}}} \; \prod_{{(w,\,c)}\,\epsilon \,D^{'}} \; {\frac{1}{1+{e^{(U_c\times V_w)}}}} \; =argmaxθ(w,c)ϵD∏1+e(−Uc×Vw)1(w,c)ϵD′∏1+e(Uc×Vw)1
= arg max θ ∑ ( w , c ) ϵ D l o g σ ( U c , V w ) + ∑ ( w , c ) ϵ D ′ l o g σ ( − U c , V w ) = \mathop{\arg\max}_{\theta} \; \sum_{{(w,\,c)}\,\epsilon \,D} log\,\sigma(U_c,\,V_w) \;\;+ \sum_{{(w,\,c)}\,\epsilon \,D^{'}} log\,\sigma(-\,U_c,\,V_w) =argmaxθ(w,c)ϵD∑logσ(Uc,Vw)+(w,c)ϵD′∑logσ(−Uc,Vw)
- Negative Sampling(负采样方法,解决 负样本太大的问题,
c
′
ϵ
N
(
w
)
{{c^{'}}\,\epsilon \,N(w)}
c′ϵN(w) 表示 随机采样负样本)
L = arg max θ ∑ ( w , c ) ϵ D 【 l o g σ ( U c , V w ) + ∑ c ′ ϵ N ( w ) l o g σ ( − U c ′ , V w ) 】 {\bf L}= \mathop{\arg\max}_{\theta} \; \sum_{{(w,\,c)}\,\epsilon \,D} 【\;\; log\,\sigma(U_c,\,V_w) \;\;+ \sum_{{c^{'}}\,\epsilon \,N(w)} log\,\sigma(-\,U_{c^{'}},\,V_w) \;\;】 L=argmaxθ(w,c)ϵD∑【logσ(Uc,Vw)+c′ϵN(w)∑logσ(−Uc′,Vw)】
【1.2】 CBOW(通过 上下文 预测 中心词)
【1.3】 训练代码(GitHub)
已传到 GitHub 上
python word2vec.py -train wiki.final.txt -model output_10_dim -dim 10 -cbow 0 -negative 10 -window 5 -min-count 5
【1.4】 Disadvantage(缺点)
- (无)一词多义
- (无)全局视角
- (不)考虑语序
- (高)不确定性(Gaussian Embedding,高斯嵌入,加了个斜方差矩阵,与原向量形成高斯分布,KL-Diverge 计算分布的相似度)
- (低)可解释性
- (不解决)未登记的词(OOV,Out-of-Vocab)
【1.5】 Subword Model(拆分单词,解决 OOV 问题)
g
o
i
n
g
=
>
g
o
+
i
n
g
r
e
a
d
e
r
=
>
r
e
a
d
+
e
r
.
.
.
going => go + ing \,\,\,\,\,\,\,\,\, reader => read + er \,\,\,\,\,\,\,\,\, ...
going=>go+ingreader=>read+er...
【二】 Glove(Global Vectors)
- 整体架构
![](https://i-blog.csdnimg.cn/blog_migrate/884648c32597de6c4345194c904bb514.png)
- 构造共现矩阵
![](https://i-blog.csdnimg.cn/blog_migrate/2bc384f941369eda8c9bc05d38b49e67.png)
![](https://i-blog.csdnimg.cn/blog_migrate/cb52fbab18f564ccccfc5ca4bd923aa2.png)
- 初步模型
![](https://i-blog.csdnimg.cn/blog_migrate/21e9a7026faa9330f0bc8af44671d314.png)
- 最终形式
![](https://i-blog.csdnimg.cn/blog_migrate/bc1bbc11eb6601bf4146231fc4ec317f.png)
- 损失函数
![](https://i-blog.csdnimg.cn/blog_migrate/7ffb23311658332c0506f3be8bfb026c.png)