当前位置: 代码迷 >> 综合 >> gensim : AttributeError: The vocab attribute was removed from KeyedVector in Gensim 4.0.0.
  详细解决方案

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

热度:47   发布时间:2023-12-08 07:23:36.0

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
  相关解决方案