AttributeError: vocab 属性已从 Gensim 4.0.0 中的 KeyedVector 中删除

问题描述

AttributeError Traceback(最近一次调用最后一次) ~/Desktop/implimentaion/train.py 中 31 use_w2v = 真 32 ---> 33 train_df,embeddings = make_w2v_embeddings(train_df,embedding_dim=embedding_dim,empty_w2v=not use_w2v) 34 35 # 拆分以训练验证

~/Desktop/implimentaion/util.py in make_w2v_embeddings(df,embedding_dim,empty_w2v) 90 91 # 如果 word2vec 模型中缺少一个词。 ---> 92 如果单词不在 word2vec.vocab 中: 93 如果单词不在 vocabs_not_w2v 中: 94 vocabs_not_w2v_cnt += 1

~/opt/anaconda3/lib/python3.8/site-packages/gensim/models/keyedvectors.py in vocab(self) 第643话 第644话 --> 645 引发 AttributeError( 646“从 Gensim 4.0.0 中的 KeyedVector 中删除了 vocab 属性。\n” 647“使用KeyedVector的.key_to_index字典、.index_to_key列表和方法

AttributeError: vocab 属性已从 Gensim 4.0.0 中的 KeyedVector 中删除。 使用 KeyedVector 的 .key_to_index dict、.index_to_key 列表和方法 .get_vecattr(key,attr) 和 .set_vecattr(key,attr,new_val) 代替。 见https://github.com/RaRe-Technologies/gensim/wiki/Migrating-from-Gensim-3.x-to-4

解决方法

正如错误所暗示的那样,“请参阅https://github.com/RaRe-Technologies/gensim/wiki/Migrating-from-Gensim-3.x-to-4"

该页面解释了如何调整旧代码以与 gensim-4.0.0 及更高的 Gensim 版本一起使用。

(您需要更改您的 ~/Desktop/implimentaion/util.py 文件以不再使用 .vocab 属性来测试某个词是否在模型中。错误消息中显示的特定情况的修复程序可能是就像将 if word not in word2vec.vocab 更改为 if word not in word2vec 一样简单。)