首页 > 解决方案 > 加载 GoogleNews-vectors-negative300.bin 和 predict_output_word

问题描述

我尝试加载 GoogleNews-vectors-negative300.bin 并尝试 predict_output_word 方法,

我测试了三种方式,但都失败了,每种方式的代码和错误如下所示。

import gensim
from gensim.models import Word2Vec
  1. 首先:

我首先使用了这一行:

model=Word2Vec.load_word2vec_format('GoogleNews-vectors-negative300.bin',binary=True)


print(model.wv.predict_output_word(['king','man'],topn=10))

错误:

DeprecationWarning: Deprecated. Use gensim.models.KeyedVectors.load_word2vec_format instead.
  1. 第二:

然后我尝试了:

model = gensim.models.KeyedVectors.load_word2vec_format('GoogleNews-vectors-negative300.bin',binary=True)


print(model.wv.predict_output_word(['king','man'],topn=10))

错误:

AttributeError: 'Word2VecKeyedVectors' object has no attribute 'predict_output_word'
  1. 第三个:model = gensim.models.Word2Vec.load('GoogleNews-vectors-negative300.bin') print(model.wv.predict_output_word(['king','man'],topn=10))

错误:_pickle.UnpicklingError:无效的加载键,'3'。

我在

https://radimrehurek.com/gensim/models/word2vec.html

但仍然不知道 predict_output_word 所在的命名空间。

有人可以帮忙吗?

谢谢。

标签: word2vec

解决方案


GoogleNews组向量只是原始向量——没有完整的训练模型(包括内部权重)。所以:

  • 无法作为全功能gensim Word2Vec模型加载
  • 可以作为仅查找加载KeyedVectors,但该对象本身没有进一步模型训练或其他功能所需的数据或协议

Google 尚未发布用于创建GoogleNews向量集的完整模型。

另请注意,predict_output_word()函数 ingensim应被视为实验好奇心。它在分层-softmax 模型中不起作用(因为生成排名预测并不那么简单)。它与训练期间使用的上下文窗口权重不完全匹配。

预测单词并不是 word2vec 算法的真正重点——许多实现不提供任何接口来在稀疏批量训练过程之外进行单个单词预测。相反,word2vec 使用(草率地)尝试进行预测来训练词向量,这些词向量被证明对其他非词预测目的有用。


推荐阅读