Gensim 4.0.0 中 KeyedVector 的 vocab 属性移除及替代方案
Gensim 4.0.0 版本中,KeyedVector 类中的 'vocab' 属性被移除了。现在,你需要使用 'key_to_index' 和 'index_to_key' 属性,以及 'get_vecattr()' 和 'set_vecattr()' 方法来替代。
查询词是否存在于模型中:
if 'word' in model.wv.key_to_index:
print('Word is in the model.')
else:
print('Word is not in the model.')
获取词相似度:
similarity = model.wv.similarity('dog', 'cat')
print('Similarity between 'dog' and 'cat':', similarity)
添加属性和词向量:
model.wv.set_vecattr('dog', 'color', 'brown')
color = model.wv.get_vecattr('dog', 'color')
print('Color of 'dog':', color)
注意: 添加属性只对当前实例有效,不会在保存模型时被保存。如果需要在保存模型时保存属性,请使用 'KeyedVectors.add_vectors()' 方法将属性向量添加到模型中。
原文地址: https://www.cveoy.top/t/topic/onFG 著作权归作者所有。请勿转载和采集!