gensim icon indicating copy to clipboard operation
gensim copied to clipboard

How to load 3.8.3 gensim word2vec model by new version 4.2.0 gensim model

Open Atsuyoshi-Funahashi opened this issue 3 years ago • 6 comments

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

Atsuyoshi-Funahashi avatar Jun 21 '22 02:06 Atsuyoshi-Funahashi

Can you show the loading code, plus the full exception?

piskvorky avatar Jun 21 '22 08:06 piskvorky

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.

Atsuyoshi-Funahashi avatar Jun 21 '22 08:06 Atsuyoshi-Funahashi

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.

piskvorky avatar Jun 21 '22 09:06 piskvorky

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.

Atsuyoshi-Funahashi avatar Jun 22 '22 02:06 Atsuyoshi-Funahashi

You don't need any special permissions to open a PR – simply create the PR and we will review it.

piskvorky avatar Jun 22 '22 06:06 piskvorky

Thank you. I have created the PR #3359 .

funasshi avatar Jun 22 '22 07:06 funasshi