gensim报错: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.
解决方法:
1. 直接修改代码
找到所有的vocab.keys()的模块,如果是 gensim定义的model ,则将其进行修改:
展示:
## 错误代码:
model = KeyedVectors.load_word2vec_format(word2vec_glove_file)
words = np.random.choice(list(model.vocab.keys()), sample)## 正确代码:
model = KeyedVectors.load_word2vec_format(word2vec_glove_file)
words = np.random.choice(list(model.key_to_index.keys()), sample)
方法2: 安装原来的版本
!pip install gensim==3.0