了解 gensim word2vec 的 most_similar

Understanding gensim word2vec's most_similar

我不确定应该如何使用 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 采用正例和负例,并尝试在向量 space 中找到与正向量尽可能近和尽可能远的点尽可能远离消极的。对吗?

此外,有没有一种方法可以让我们将两点之间的关系映射到另一点并得到结果(参见 man-king woman-X 示例)?

您可以在其源代码中查看 most_similar() 的确切功能:

https://github.com/RaRe-Technologies/gensim/blob/develop/gensim/models/keyedvectors.py#L485

并不是“在向量 space 中找到尽可能接近正向量并尽可能远离负向量的点”。相反,如原始 word2vec 论文中所述,它执行向量运算:添加正向量,减去负向量,然后从结果位置列出最接近该角度的已知向量。

这足以解决 man : king :: woman :: ? 式的类比,通过这样的调用:

sims = wordvecs.most_similar(positive=['king', 'woman'], 
                             negative=['man'])

(您可以将此视为“从 'king'-向量开始,添加 'woman'-向量,减去 'man'-向量,从您结束的地方开始,报告排名最接近该点的词向量(同时省略了 3 个查询向量中的任何一个)。”)