Python 在 gensim 中加载 Word2Vec 模型时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32101795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:00:42  来源:igfitidea点击:

Error while loading Word2Vec model in gensim

pythongensimword2vec

提问by Tarantula

I'm getting an AttributeErrorwhile loading the gensim model available at word2vec repository:

我正在AttributeError加载 word2vec 存储库中可用的 gensim 模型:

from gensim import models
w = models.Word2Vec()
w.load_word2vec_format('GoogleNews-vectors-negative300.bin', binary=True)
print w["queen"]

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-3-8219e36ba1f6> in <module>()
----> 1 w["queen"]

C:\Anaconda64\lib\site-packages\gensim\models\word2vec.pyc in __getitem__(self, word)
    761 
    762         """
--> 763         return self.syn0[self.vocab[word].index]
    764 
    765 

AttributeError: 'Word2Vec' object has no attribute 'syn0'

Is this a known issue ?

这是一个已知的问题 ?

采纳答案by Tarantula

Fixed the problem with:

修复了以下问题:

from gensim import models
w = models.Word2Vec.load_word2vec_format('GoogleNews-vectors-negative300.bin', binary=True)
print w["queen"]

回答by Prakhar Agarwal

In order to share word vector querying code between different training algos(Word2Vec, Fastext, WordRank, VarEmbed) the authors have separated storage and querying of word vectors into a separate class KeyedVectors.

为了在不同的训练算法(Word2Vec、Fastext、WordRank、VarEmbed)之间共享词向量查询代码,作者将词向量的存储和查询分离到一个单独的类 KeyedVectors 中。

Two methods and several attributes in word2vec class have been deprecated.

word2vec 类中的两个方法和几个属性已被弃用。

Methods

方法

  • load_word2vec_format
  • save_word2vec_format
  • load_word2vec_format
  • save_word2vec_format

Attributes

属性

  • syn0norm
  • syn0
  • vocab
  • index2word
  • 同义词
  • 同步
  • 词汇
  • 索引二字

These have been moved to KeyedVectors class.

这些已移至 KeyedVectors 类。

After upgrading to this release you might get exceptions about deprecated methods or missing attributes.

升级到此版本后,您可能会收到有关已弃用方法或缺少属性的异常。

To remove the exceptions, you should use

要删除异常,您应该使用

KeyedVectors.load_word2vec_format (instead ofWord2Vec.load_word2vec_format)
word2vec_model.wv.save_word2vec_format (instead of  word2vec_model.save_word2vec_format)
model.wv.syn0norm instead of  (model.syn0norm)
model.wv.syn0 instead of  (model.syn0)
model.wv.vocab instead of (model.vocab)
model.wv.index2word instead of (model.index2word)

回答by Nayantara Jeyaraj

Currently, as models.Word2Vechas been deprecated, you need to use the models.KeyedVectors.load_word2vec_formatinstead of models.Word2Vec.load_word2vec_formatas shown below.

目前,由于models.Word2Vec已被弃用,您需要使用models.KeyedVectors.load_word2vec_format代替 ,models.Word2Vec.load_word2vec_format如下所示。

from gensim import models
w = models.KeyedVectors.load_word2vec_format('model.bin', binary=True)