gensim
gensim copied to clipboard
How to load 3.8.3 gensim word2vec model by new version 4.2.0 gensim model
Problem description
Hi! I have been using gensim 3.8.1 and want to use the latest version, gensim 4.2.0.
I have been able to load models made with older gensim versions, but when I changed to the latest version, I can no longer load them. This is the error code.
Exception has occurred: AttributeError
Can't get attribute 'Vocab' on <module 'gensim.models.word2vec' from '/PATH'>
I would like to know if there is some way to load older models.
Versions
python 3.8.9 gensim 4.2.0
Can you show the loading code, plus the full exception?
Sorry I forgot to paste the code. Here is the minimum code which error occurs.
import gensim
model_path='latest-ja-word2vec-gensim-model/word2vec.gensim.model'
model = gensim.models.Word2Vec.load(model_path)
I use the pretrained japanese model from this url by curl.
I can replicate this, thanks for reporting.
And I can see what happened too – we added the legacy Vocab structure to the keyedvectors.py module, but not to word2vec.py module. Apparently some models need that too.
The fix is trivial. In word2vec.py:
# replace this line:
from gensim.models.keyedvectors import KeyedVectors, pseudorandom_weak_vector
# with this line:
from gensim.models.keyedvectors import KeyedVectors, pseudorandom_weak_vector, Vocab
@Atsuyoshi-Funahashi can you open a PR with the fix above? We'll include this fix in the next release.
In the meantime, you can apply the fix = add the Vocab import to word2vec.py yourself.
Thank you! I could fix the error by this change. OK, I create the PR. I want to push by my different account, so please give this account permission.
You don't need any special permissions to open a PR – simply create the PR and we will review it.
Thank you. I have created the PR #3359 .