Tarantula
Tarantula

Reputation: 19902

Error while loading Word2Vec model in gensim

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 ?

Upvotes: 10

Views: 32979

Answers (3)

Nayantara Jeyaraj
Nayantara Jeyaraj

Reputation: 2706

Currently, as models.Word2Vec has been deprecated, you need to use the models.KeyedVectors.load_word2vec_format instead of models.Word2Vec.load_word2vec_format as shown below.

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

Upvotes: 0

Tarantula
Tarantula

Reputation: 19902

Fixed the problem with:

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

Upvotes: 11

Prakhar Agarwal
Prakhar Agarwal

Reputation: 2852

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)

Upvotes: 6

Related Questions