Error while loading Word2Vec model in gensim

27,863

Solution 1

Fixed the problem with:

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

Solution 2

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.

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

Methods

  • load_word2vec_format
  • save_word2vec_format

Attributes

  • syn0norm
  • syn0
  • vocab
  • index2word

These have been moved to KeyedVectors class.

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)
Share:
27,863
Tarantula
Author by

Tarantula

Updated on July 10, 2022

Comments

  • Tarantula
    Tarantula almost 2 years

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

    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 ?