给定单词的向量(而不是单词本身),获得最相似的单词 [英] Get most similar words, given the vector of the word (not the word itself)
问题描述
使用gensim.models.Word2Vec
库,您可以提供一个模型和一个单词",以查找最相似单词的列表:
Using the gensim.models.Word2Vec
library, you have the possibility to provide a model and a "word" for which you want to find the list of most similar words:
model = gensim.models.Word2Vec.load_word2vec_format(model_file, binary=True)
model.most_similar(positive=[WORD], topn=N)
我想知道是否有可能给系统输入模型和向量",并要求系统返回最相似的单词(它们的向量与给定向量非常接近).类似于:
I wonder if there is a possibility to give the system as input the model and a "vector", and ask the system to return the top similar words (which their vectors is very close to the given vector). Something similar to:
model.most_similar(positive=[VECTOR], topn=N)
我需要这种功能来进行双语设置,在该环境中,我有2种模型(英语和德语),以及一些英语单词,我需要为其找到最相似的德语候选人. 我要做的是从英语模型中获取每个英语单词的向量:
I need this functionality for a bilingual setting, in which I have 2 models (English and German), as well as some English words for which I need to find their most similar German candidates. What I want to do is to get the vector of each English word from the English model:
model_EN = gensim.models.Word2Vec.load_word2vec_format(model_file_EN, binary=True)
vector_w_en=model_EN[WORD_EN]
,然后使用这些向量查询德国模型.
and then query the German model with these vectors.
model_DE = gensim.models.Word2Vec.load_word2vec_format(model_file_DE, binary=True)
model_DE.most_similar(positive=[vector_w_en], topn=N)
我已经使用word2vec包中的原始距离函数在C中实现了此功能.但是,现在我需要将其放在python中,以便能够将其与其他脚本集成.
I have implemented this in C using the original distance function in the word2vec package. But, now I need it to be in python, in order to be able to integrate it with my other scripts.
您知道gensim.models.Word2Vec
库或其他类似库中是否已经存在执行此操作的方法?我需要自己实施吗?
Do you know if there is already a method in gensim.models.Word2Vec
library or other similar libraries which does this? Do I need to implement it by myself?
推荐答案
方法 similar_by_vector
通过矢量返回前N个最相似的单词:
The method similar_by_vector
returns the top-N most similar words by vector:
similar_by_vector(vector, topn=10, restrict_vocab=None)
这篇关于给定单词的向量(而不是单词本身),获得最相似的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!