本文介绍了在python上使用gensim Word2Vec的不同模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试应用在python gensim库中实现的word2vec模型.我有一个句子列表(每个句子是一个单词列表).

I am trying to apply the word2vec model implemented in the library gensim in python. I have a list of sentences (each sentences is a list of words).

例如,让我们有:

sentences=[['first','second','third','fourth']]*n

我实现了两个相同的模型:

and I implement two identical models:

model = gensim.models.Word2Vec(sententes, min_count=1,size=2)
model2=gensim.models.Word2Vec(sentences, min_count=1,size=2)

我意识到,取决于n的值,模型有时是相同的,有时是不同的.

I realize that the models sometimes are the same, and sometimes are different, depending on the value of n.

例如,如果n = 100,我得到

For instance, if n=100 I obtain

print(model['first']==model2['first'])
True

同时,对于n = 1000:

while, for n=1000:

print(model['first']==model2['first'])
False

怎么可能?

非常感谢!

推荐答案

查看 gensim 文档,运行 Word2Vec 时会有一些随机性:

Looking at the gensim documentation, there is some randomization when you run Word2Vec:

因此,如果要获得可重复的结果,则需要设置种子:

Thus if you want to have reproducible results, you will need to set the seed:

In [1]: import gensim

In [2]: sentences=[['first','second','third','fourth']]*1000

In [3]: model1 = gensim.models.Word2Vec(sentences, min_count = 1, size = 2)

In [4]: model2 = gensim.models.Word2Vec(sentences, min_count = 1, size = 2)

In [5]: print(all(model1['first']==model2['first']))
False

In [6]: model3 = gensim.models.Word2Vec(sentences, min_count = 1, size = 2, seed = 1234)

In [7]: model4 = gensim.models.Word2Vec(sentences, min_count = 1, size = 2, seed = 1234)

In [11]: print(all(model3['first']==model4['first']))
True

这篇关于在python上使用gensim Word2Vec的不同模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 16:10