python - 理解gensim word2vec的most_similar

标签 python python-3.x nlp gensim word2vec

我不确定应该如何使用 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/

相关文章:

python - 具有可变句子长度的 Keras LSTM——传递给模型的 Numpy 数组列表,而不是模型预期的大小

python - 你能在 matplotlib 中的绘图线上添加一个数字吗?

python - 在python3中,如何测试工作空间根目录下的.py文件?

nlp - "g++ not detected"当数据集变大时,GPU 中的矩阵大小是否有任何限制?

android - 如何在python脚本中导入android

python - 使用字典将月份数字转换为月份名称的基本 Python 编程

Python 3 : urlextract package, 权限错误

python - BatchToSpaceND 实际上是如何工作的?

machine-learning - 如何根据上下文对相似类型的句子进行聚类并从中提取关键字

python-3.x - 如何制定正确的训练和测试维度以适合 elmo 嵌入模型