【NLP】_03_基础词向量


 


【一】 Word2Vec(词向量)
  • Word2Vec,作为 NLP 领域深度学习的基础,是每个工程师都绕不过的重点话题。虽然从18年年底就诞生出了 BertGPT 等预训练模型,横扫 NLP 领域,但其还是基于 Word2Vec 的向量和思想训练出来的。
    【1.1】 Skip-Gram(通过 中心词 预测 上下文)
  • U U (Context,作为上下文) V V (Center,作为中心词) 两个矩阵
     
  • f ( U , V ) = P ( W i W j ) f (U, V) = P (W_i | W_j) ,基于假设, W i , W j W_i, W_j 的相似度越高,概率值越大
  • 直接思路    S o f t m a x Softmax 形式算出概率最大的那个单词 ( 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}}}}}

 
 

  • 目标函数 ( 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}}}}}
    = 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}}}

 
 

  • 巧妙思路(表示成 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})}}}}

 
 

  • 目标函数 D = 1 D=1 表示 W i W_i W j W_j 同时出现, D = 0 D^{'}=0 表示 W i W_i W j W_j 不能同时出现)
     
    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)
    = 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)}}}} \;
    = 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)

 
 

  • Negative Sampling(负采样方法,解决 负样本太大的问题, c ϵ N ( w ) {{c^{'}}\,\epsilon \,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) \;\;】

 
 
 

    【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 \,\,\,\,\,\,\,\,\, ...

 
 


【二】 Glove(Global Vectors)
  • 整体架构
  • 构造共现矩阵
          
  • 初步模型
  • 最终形式
  • 损失函数
发布了57 篇原创文章 · 获赞 5 · 访问量 2875

猜你喜欢

转载自blog.csdn.net/qq_34330456/article/details/104371287