Tag Archives: gensim

Gensim Error: AttributeError: The vocab attribute was removed from KeyedVector in Gensim 4.0.0.

gensim error: AttributeError: The vocab attribute was removed from KeyedVector in Gensim 4.0.0.
Use KeyedVector’s .key_to_index dict, .index_to_key list, and methods .get_vecattr(key, attr) and .set_vecattr(key, attr, new_val) instead.
Solution:
1. directly modify the code
Find all the modules of vocab.keys() and modify them if they are mods defined by gensim.

Show:

## Wrong
model = KeyedVectors.load_word2vec_format(word2vec_glove_file)
words = np.random.choice(list(model.vocab.keys()), sample)

## Right
model = KeyedVectors.load_word2vec_format(word2vec_glove_file)
words = np.random.choice(list(model.key_to_index.keys()), sample)

Method 2: install the original version

!pip install gensim==3.0

python gensim AttributeError: ‘Doc2Vec‘ object has no attribute ‘dv‘

python3

gensim 4.0.1

My code: doc2vec reported an error when loading the doc2vec model file

from gensim.models import Doc2Vec

doc2vec_model = Doc2Vec.load('data/doc2vec.model')

“AttributeError: ‘Doc2Vec’ object has no attribute ‘dv’”

Reason 1: there may be some problems with the latest version. Change the version!!!

Reason 2: another code   Change model.dv to = & gt;    model.docvecs  

resolvent:

I uninstalled gensim PIP uninstall gensim

Reinstall PIP install gensim = = 3.8.3

Solved!!! I hope it will be useful to you.