我不确定应该如何使用 gensim 的 Word2Vec 的most_similar 方法。假设您想要测试以下经过验证的示例:男人站在国王面前,女人站在 X;找到 X。我认为这就是您可以使用此方法执行的操作,但从我得到的结果来看,我认为这不是真的。
The documentation内容如下:
Find the top-N most similar words. Positive words contribute positively towards the similarity, negative words negatively.
This method computes cosine similarity between a simple mean of the projection weight vectors of the given words and the vectors for each word in the model. The method corresponds to the word-analogy and distance scripts in the original word2vec implementation.
我假设,most_similar
采用正例和反例,并尝试在向量空间中找到与正向量尽可能接近且尽可能远离的点来自消极的。这是正确的吗?
此外,是否有一种方法可以让我们将两点之间的关系映射到另一点并得到结果(参见男人-国王女人-X 示例)?
最佳答案
您可以在其源代码中查看 most_similar()
的具体作用:
https://github.com/RaRe-Technologies/gensim/blob/develop/gensim/models/keyedvectors.py#L485
这并不完全是“在向量空间中找到尽可能接近正向量并尽可能远离负向量的点”。相反,正如原始 word2vec 论文中所述,它执行向量算术:添加正向量,减去负向量,然后从结果位置列出最接近该角度的已知向量。
这足以解决 man : king::Woman::?
式的类比,通过如下调用:
sims = wordvecs.most_similar(positive=['king', 'woman'],
negative=['man'])
(您可以将其视为“从‘国王’向量开始,添加‘女人’向量,减去‘男人’向量,从您结束的位置开始,报告最接近该点的排名词向量(同时省略 3 个查询向量中的任何一个)。”)
关于python - 理解gensim word2vec的most_similar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54580260/