本文介绍了Gen3 Word2Vec在Python3中缺少vocab的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Word2Vec的gensim实现.我有以下代码片段:

I'm using gensim implementation of Word2Vec. I have the following code snippet:

print('training model')
model = Word2Vec(Sentences(start, end))
print('trained model:', model)
print('vocab:', model.vocab.keys())

当我在python2中运行它时,它按预期运行.最后的印刷品是词汇表中的所有单词.

When I run this in python2, it runs as expected. The final print is all the words in the vocabulary.

但是,如果我在python3中运行它,则会收到错误消息:

However, if I run it in python3, I get an error:

trained model: Word2Vec(vocab=102, size=100, alpha=0.025)
Traceback (most recent call last):
  File "learn.py", line 58, in <module>
    train(to_datetime('-4h'), to_datetime('now'), 'model.out')
  File "learn.py", line 23, in train
    print('vocab:', model.vocab.keys())
AttributeError: 'Word2Vec' object has no attribute 'vocab'

这是怎么回事? gensim word2vec与python3不兼容吗?

What is going on? Is gensim word2vec not compatible with python3?

推荐答案

您在两个地方都使用了相同版本的gensim吗? Gensim 1.0.0将vocab移动到帮助对象,因此,在gensim 1.0.0之前的版本中(在Python 2或3中),您可以使用:

Are you using the same version of gensim in both places? Gensim 1.0.0 moves vocab to a helper object, so whereas in pre-1.0.0 versions of gensim (in Python 2 or 3), you can use:

model.vocab

...在gensim 1.0.0+中,您应该改用(在Python 2或3中)...

...in gensim 1.0.0+ you should instead use (in Python 2 or 3)...

model.wv.vocab

这篇关于Gen3 Word2Vec在Python3中缺少vocab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 15:22