Irene
Irene

Reputation: 33

AttributeError: Can't get attribute on <module 'gensim.models.word2vec'

I use python 3.9.1 on macOS Big Sur with an M1 chip. And, gensim is 4.0.1

I tried to use the pre-trained Word2Vec model and I ran the code below:

from gensim.models.word2vec import Word2Vec

model_path = '/path/to/word2vec.gensim.model'

model = Word2Vec.load(model_path)

However, I got an error below:

AttributeError                            Traceback (most recent call last)
<ipython-input-11-4c1c2f93fadb> in <module>
      1 from gensim.models.word2vec import Word2Vec
      2 
----> 3 model = Word2Vec.load(model_path)

~/opt/miniconda3/lib/python3.9/site-packages/gensim/models/word2vec.py in load(cls, rethrow, *args, **kwargs)
   1932                 "Try loading older model using gensim-3.8.3, then re-saving, to restore "
   1933                 "compatibility with current code.")
-> 1934             raise ae
   1935 
   1936     def _load_specials(self, *args, **kwargs):

~/opt/miniconda3/lib/python3.9/site-packages/gensim/models/word2vec.py in load(cls, rethrow, *args, **kwargs)
   1920         """
   1921         try:
-> 1922             model = super(Word2Vec, cls).load(*args, **kwargs)
   1923             if not isinstance(model, Word2Vec):
   1924                 rethrow = True

~/opt/miniconda3/lib/python3.9/site-packages/gensim/utils.py in load(cls, fname, mmap)
    484         compress, subname = SaveLoad._adapt_by_suffix(fname)
    485 
--> 486         obj = unpickle(fname)
    487         obj._load_specials(fname, mmap, compress, subname)
    488         obj.add_lifecycle_event("loaded", fname=fname)

~/opt/miniconda3/lib/python3.9/site-packages/gensim/utils.py in unpickle(fname)
   1456     """
   1457     with open(fname, 'rb') as f:
-> 1458         return _pickle.load(f, encoding='latin1')  # needed because loading from S3 doesn't support readline()
   1459 
   1460 

AttributeError: Can't get attribute 'Vocab' on <module 'gensim.models.word2vec' from '/Users//opt/miniconda3/lib/python3.9/site-packages/gensim/models/word2vec.py'>

here is the link where I got the this model https://github.com/shiroyagicorp/japanese-word2vec-model-builder

Thank you in advance.

Upvotes: 0

Views: 1418

Answers (1)

dennlinger
dennlinger

Reputation: 11508

The problem is that the referenced repository trained a model on an incredibly old version of GenSim, which makes it incompatible with current versions.

You can potentially check whether the lifecycle meta data gives you any indication on the actual version, and then try to update your model from there. The documentation also gives some tips for upgrading your older trained models, but even those are relatively weak and point mostly to re-training. Similarly, even migrating from GenSim 3.X to 4.X is not referencing direct upgrade methods, but could give you ideas on what parameters to look out for specifically.

My suggestion would be to try loading it with any of the previous 3.X versions, and see if you have more success loading it there.

Upvotes: 2

Related Questions