5
votes

I train and save a gensim word2vec model:

W2V_MODEL_FN = r"C:\Users\models\w2v.model"

model = Word2Vec(X, size=150, window=3, min_count=2, workers=10)
model.train(X, total_examples=len(X), epochs=50)
model.save(W2V_MODEL_FN)

And then:

w2v_model = Word2Vec.load(W2V_MODEL_FN)

On one enviroment it works perfectly but in another I get the error:

{AttributeError}Can't get attribute 'Word2VecKeyedVectors' on module 'gensim.models.keyedvectors' from 'C:\Users\Anaconda3_New\envs\ISP_env\lib\site-packages\gensim\models\keyedvectors.py'>

So I guess it might be a package version issue?

But I couldn't figure what it is. Any ideas?

Thanks!

2
solved by upgrading to gensim 3.4oren_isp
You can write that as an answer.jar

2 Answers

1
votes

Thanks to @oren_jsp answer to question.

pip3 install --upgrade gensim --user

It solved my problem.

0
votes

My case was a different issue.

My code looked like this:

# main.py
from enum import IntEnum
from gensim.models import Word2Vec


class GensimTrainingAlgo(IntEnum):
    SG = 1
    CBOW = 0


model = Word2Vec(X, size=150, window=3, min_count=2, workers=10, sg=GensimTrainingAlgo.SG)
model.save('/tmp/path')

It turns out I was breaking pickle. If you want to be able to save/load a function, it must be pickleable. This means it must be named and importable.

I fixed it by making sure the enum was defined outside the main.py file.

# utils.py
from enum import IntEnum


class GensimTrainingAlgo(IntEnum):
    SG = 1
    CBOW = 0
# main.py
from gensim.models import Word2Vec

model = Word2Vec(X, size=150, window=3, min_count=2, workers=10, sg=GensimTrainingAlgo.SG)
model.save('/tmp/path')