Doc2Vec Model
1 Review: Bag-of-words
此模型将每个文档转换为固定长度的整数向量。例如,给定句子:
John likes to watch movies. Mary likes movies too.
John also likes to watch football games. Mary hates football.
模型输出向量:
[1, 2, 1, 1, 2, 1, 1, 0, 0, 0, 0]
[1, 1, 1, 1, 0, 1, 0, 1, 2, 1, 1]
每个向量有10个元素,其中每个元素计算文档中特定单词出现的次数。元素的顺序是任意的。在上面的示例中,元素的顺序对应于以下单词:
["John", "likes", "to", "watch", "movies", "Mary", "too", "also", "football", "games", "hates"]
.
词袋模型出奇地有效,但有几个缺点。
-
首先,它们丢失了所有关于词序的信息:“约翰喜欢玛丽”和“玛丽喜欢约翰”对应的是相同的向量。有一种解决方案:bag of n-grams模型将长度为n的单词短语作为固定长度的向量来表示文档,以捕获局部词序,但会受到数据稀疏性和高维性的影响
-
其次,该模型不会尝试学习基础单词的含义,因此,向量之间的距离并不总是反映出含义上的差异。
-
Word2Vec模型解决了第二个问题。
2 word2vec
Word2Vec
is a more recent model that embeds words in a lower-dimensional vector space using a shallow neural network.
产生一组词向量,其中在向量空间中靠在一起的向量根据上下文具有相似的含义,而彼此远离的词向量具有不同的含义。
但是,如果我们想为整个文档计算向量呢?我们可以对文档中每个单词的向量进行平均,尽管这是快速而粗略的,但它通常很有用。但是,有更好的方法…
3 Paragraph Vector,即Doc2Vec
Le和Mikolov在2014年引入了Doc2Vec算法,该算法通常优于Word2Vec向量的这种简单平均。
有两种实现:
- Paragraph Vector - Distributed Memory (PV-DM)
- Paragraph Vector - Distributed Bag of Words (PV-DBOW)
3.1 PV-DM
PV-DM类似于Word2Vec CBOW
通过对神经网络进行训练,以基于上下文词向量和整个文档的词向量的平均值来预测中心词的任务,从而获得doc向量。
3.1 PV-DBOW
与Word2Vec SG类似
4 那么Doc2vec是怎么预测新的句子Paragraph vector呢?
https://zhuanlan.zhihu.com/p/269101901?utm_source=wechat_session
训练完了以后,就会得到训练样本中所有的词向量和每句话对应的句子向量,那么Doc2vec是怎么预测新的句子Paragraph vector呢?
其实在预测新的句子的时候,还是会将该Paragraph vector随机初始化,放入模型中再重新根据随机梯度下降不断迭代求得最终稳定下来的句子向量。不过在预测过程中,模型里的词向量、投影层到输出层的softmax weights参数是不会变的,这样在不断迭代中只会更新Paragraph vector,其它参数均已固定,只需很少的时间就能计算出待预测的Paragraph vector。
5 gensim包
很好的一个nlp工具包。
https://radimrehurek.com/gensim/auto_examples/tutorials/run_doc2vec_lee.html
特别说明:
大数据量的时候
model.build_vocab()的输入可以是个迭代器。
其他
corpus和document
The file we’re reading is a corpus. Each line of the file is a document.
引用
- Gensim进阶教程:训练word2vec与doc2vec模型:https://www.cnblogs.com/iloveai/p/gensim_tutorial2.html
- 官方例子:https://radimrehurek.com/gensim/auto_examples/tutorials/run_doc2vec_lee.html
- https://www.pythonf.cn/read/141699
- 《Distributed Representations of Sentences and Documents》
- https://github.com/lybroman/Chinese-sentiment-analysis-with-Doc2Vec