公式输入请参考:在线Latex公式

词嵌入的由来:

1-of-N Encoding

apple=[1 0 0 0 0]
bag=[0 1 0 0 0]
cat=[0 0 1 0 0]
dog=[0 0 0 1 0]
elephant=[0 0 0 0 1]
这种词表示方式缺点很明显:
维度大
稀疏
各个词之间关系完全被忽略

Word Class

这个是上节课的Dimension Reduction的方法,化繁为简。

Class1 Class2 Class3
dog cat bird ran jumped walk flower tree apple

但是这个方法也没有办法表示Class之间的关系,例如第二个class是第一个class可以做的事情。

Word Embedding

李宏毅学习笔记14.Unsupervised Learning: Word Embedding
一看就知道这个方法维度比1-of-N Encoding要少很多很多。

Word Embedding咋做

Word Embedding是一种非监督的机器学习方法(Generating Word Vector is unsupervised)。做法很简单,就是让Machine 阅读很多的文章,自然会知道词的Embedding是什么样子。
Machine learn the meaning of words from reading a lot of documents without supervision
它的输入是一个词,输出是一个词对应的向量。
李宏毅学习笔记14.Unsupervised Learning: Word Embedding
在训练的过程中,我们只知道输入,不知道输入对应的输出是什么,那如何找到这个function呢?
A word can be understood by its context
You shall know a word by the company it keeps
就是要根据上下文去理解词的含义。
例如,让机器学习:
李宏毅学习笔记14.Unsupervised Learning: Word Embedding
蔡英文、马英九are something very similar

如何找出词的含义有两个方法:

·Count based

If two words wiw_i and wjw_j frequently co-occur, V(wi)V(w_i) and V(wj)V(w_j) would be close to each other.
常见的算法是斯坦福的:Glove Vector:
其中心思想是
V(wi)V(wj)Ni,jV(w_i)\cdot V(w_j) \Leftrightarrow N_{i,j}
左边是V(wi)V(wj)V(w_i)和 V(w_j)的内积,右边是同一个文档中wiw_iwjw_j同时出现的次数。
左边和右边越接近越好。

·Perdition based

根据前i-1个词,预测第i个词。
李宏毅学习笔记14.Unsupervised Learning: Word Embedding
这个算法的输入是:1-of-N Encoding
输出是每一个词作为下一个词出现的概率。
如果有10W个词,那么输出就是10W维。
李宏毅学习笔记14.Unsupervised Learning: Word Embedding
由于输入每个一个词的表示都不一样,所以可以把隐藏层的第一层隐藏层的输入,也就是词嵌入:
Take out the input of the neurons in the first layer. Use it to represent a word ww
李宏毅学习笔记14.Unsupervised Learning: Word Embedding
如上图所示,隐藏层的第一层z1z_1z2z_2就是词嵌入。咋整的?
例如有两段训练语料:
李宏毅学习笔记14.Unsupervised Learning: Word Embedding
当无论蔡英文、马英九作为输入的时候,我们都希望模型的输出中,宣誓就职应该有最大的概率是下一个词。那么蔡英文、马英九应该有相似的词嵌入(如下图),才会有这个结果:
李宏毅学习笔记14.Unsupervised Learning: Word Embedding
也就是说当一个模型在考虑下一个词的概率的过程中,就自动的把词嵌入给学到了。

扩展

但是只看wi1w_{i-1},然后预测wiw_i,貌似非常困难啊,一个词后面可能出现的词的可能性太多种了,于是对这个算法进行了扩展,例如考虑wi1w_{i-1}wi2w_{i-2}然后预测wiw_i
模型就变成了这样(把wi1w_{i-1}wi2w_{i-2}接到一起作为输入):
李宏毅学习笔记14.Unsupervised Learning: Word Embedding
这里需要注意的是,训练的时候,wi1w_{i-1}wi2w_{i-2}两个向量的第一个位置同时对应隐藏层的z1z_1z2z_2,注意看下图的箭头颜色
李宏毅学习笔记14.Unsupervised Learning: Word Embedding
以此类推:
李宏毅学习笔记14.Unsupervised Learning: Word Embedding
这样做有两个好处:
1、共享参数,减少计算量;
2、一个词对应一个词嵌入,否则会有多个词嵌入。

数学表达

xi1x_{i-1}表示wi1w_{i-1},用xi2x_{i-2}表示wi2w_{i-2},隐藏层的输入用zz表示
xi1x_{i-1}xi2x_{i-2}的长度都是V|V|
zz的长度是Z|Z|
他们的关系如下:
z=W1xi2+W2xi1z=W_1x_{i-2}+W_2x_{i-1}
其中,权重矩阵W1W_1,W2W_2的大小都是V×Z|V|\times|Z|
这里,我们强制:
W1=W2=Wz=W(xi2+xi1)W_1=W_2=W\Rightarrow z=W(x_{i-2}+x_{i-1})
现在就是如何使得W1=W2W_1=W_2
原ppt中用的字母有点歧义,我这里先说下,下面的wiw_i不是指词,而是指词wi2w_{i-2}对应的参数。
同样wjw_j是指词wi1w_{i-1}对应的参数,如下图所示:
李宏毅学习笔记14.Unsupervised Learning: Word Embedding
如果要使得wi=wjw_i=w_j,需要两个步骤:
1、为wiw_iwjw_j初始化相同的值
2、在反向传播更新参数采用的公式如下:
wiwiηCwiηCwjw_i\leftarrow w_i-\eta\frac{\partial C}{\partial w_i}-\eta\frac{\partial C}{\partial w_j}
wjwjηCwjηCwiw_j\leftarrow w_j-\eta\frac{\partial C}{\partial w_j}-\eta\frac{\partial C}{\partial w_i}

训练

首先搜集数据,例如:
李宏毅学习笔记14.Unsupervised Learning: Word Embedding
然后训练:
李宏毅学习笔记14.Unsupervised Learning: Word Embedding
红色箭头是代表Minimizing cross entropy关系。

Various Architectures

·Continuous bag of word(CBOW)model
上下文预测中间的词(predicting the word given its context)
李宏毅学习笔记14.Unsupervised Learning: Word Embedding
·Skip-gram
根据给定词预测上下文(predicting the context given a word)
李宏毅学习笔记14.Unsupervised Learning: Word Embedding
上面的NN其实只用一层就可以了,这样速度会很快,可以做很多个epoch。

词嵌入的可视化效果

李宏毅学习笔记14.Unsupervised Learning: Word Embedding
上图是国家和首都的关系,动词的三个时态的关系可视化。
Fu, Ruiji, et al."Learning semantic hierarchies via word embeddings."Proceedings of the 52th Annual Meeting of the Association for Computational Linguistics: Long Papers. Vol.1.2014.
李宏毅学习笔记14.Unsupervised Learning: Word Embedding

多语言词嵌入

Bilingual Word Embeddings for Phrase-Based Machine Translation, Will Zou, Richard Socher, Daniel Cer and Christopher Manning, EMNLP, 2011
李宏毅学习笔记14.Unsupervised Learning: Word Embedding

多领域嵌入

Richard Socher, Milind Ganjoo, Hamsa Sridhar, Osbert Bastani, Christopher D.Manning, Andrew Y. Ng, Zero-Shot Learning Through Cross-Modal Transfer, NIPS, 2013
李宏毅学习笔记14.Unsupervised Learning: Word Embedding
李宏毅学习笔记14.Unsupervised Learning: Word Embedding

Document Embedding

·word sequences with different lengths ->the vector with the same length
李宏毅学习笔记14.Unsupervised Learning: Word Embedding
之前把文档转成词袋,然后进行嵌入的方法是有缺陷的:
李宏毅学习笔记14.Unsupervised Learning: Word Embedding
李宏毅学习笔记14.Unsupervised Learning: Word Embedding
Reference: Hinton, Geoffrey E., and Ruslan R.Salakhutdinov."Reducing the dimensionality of data with neural networks."Science 313.5786 (2006):504-507
缺点是词袋模型缺乏词的位置信息,这样会有问题,例如:
white blood cells destroying an infection
an infection destroying white blood cells
这两句话用词袋表示的话是一样的
李宏毅学习笔记14.Unsupervised Learning: Word Embedding
如何解决这个问题老师直接给参考文献,基本在paper带读里面有
· Paragraph Vector: Le, Quoc, and Tomas Mikolov."Distributed Representations of Sentences and Documents."ICML,2014
· Seq2seq Auto-encoder: Li, Jiwei, Minh-Thang Luong, and Dan Jurafsky."A hierarchical neural autoencoder for paragraphs and documents."arXiv preprint, 2015
· Skip Thought:Ryan Kiros, Yukun Zhu, Ruslan Salakhutdinov, Richard S.Zemel, Antonio Torralba, Raquel Urtasun, Sanja Fidler,“Skip-Thought Vectors” arXiv preprint,2015.
以上是无监督的方式
下面是监督的方式
· Huang, Po-Sen, et al."Learning deep structured semantic models for web search using clickthrough data."ACM,2013.
· Shen, Yelong, et al."A latent semantic model with convolutional-pooling structure for information retrieval."ACM,2014.
· Socher, Richard, et al."Recursive deep models for semantic compositionality over a sentiment treebank."EMNLP,2013.
· Tai, Kai Sheng, Richard Socher, and Christopher D. Manning."Improved semantic representations from tree-structured long short-term memory networks."arXiv preprint,2015.

相关文章:

  • 2021-11-11
  • 2021-07-12
  • 2021-04-22
  • 2021-04-03
  • 2021-10-24
  • 2022-12-23
  • 2021-12-08
  • 2021-08-04
猜你喜欢
  • 2021-09-21
  • 2021-08-02
  • 2021-08-01
  • 2021-09-04
  • 2021-04-12
  • 2021-06-22
  • 2021-06-08
相关资源
相似解决方案