python - 使用Word2vec判断一组词中哪两个词最相似
问题描述
我正在尝试在 Word2vec 周围使用 python 包装器。我有一个词嵌入或一组词,可以在下面看到,我试图从中确定哪两个词彼此最相似。
我怎样才能做到这一点?
['建筑师','护士','外科医生','祖母','爸爸']
解决方案
根据您的评论,鉴于您使用的是 gensim 的 word2vec:
为您的嵌入加载或训练模型,然后在您的模型上,您可以调用:
min_distance = float('inf')
min_pair = None
word2vec_model_wv = model.wv # Unsure if this can be done in the loop, but just to be safe efficiency-wise
for candidate_word1 in words:
for candidate_word2 in words:
if candidate_word1 == candidate_word2:
continue # ignore when the two words are the same
distance = word2vec_model_wv.distance(candidate_word1, candidate_word2)
if distance < min_distance:
min_pair = (candidate_word1, candidate_word2)
min_distance = distance
也可能是相似性(我不完全确定是否有区别)。https://radimrehurek.com/gensim/models/keyedvectors.html#gensim.models.keyedvectors.WordEmbeddingsKeyedVectors.similarity
如果相似度随着词的接近而变大,正如我所期望的那样,那么您将希望最大化而不是最小化,只需用相似度调用替换距离函数调用。基本上,这只是对的简单最小/最大函数。
推荐阅读
- javascript - Angular 反应形式和递归
- python - 在 Colab 上运行 Keras 模型后,Google Drive Storage 已满
- r - 计算 R 中两个变量的平均比率的最佳方法是什么?
- sql - 在 SQLite 中计算时差
- typescript - Angular 9 - 重定向到相同的 URL 和 REFRESH 页面,但应该只调用当前页面 API 调用
- amazon-web-services - Amazon S3 ls --recursive 计算的对象数量多于从控制台获取大小
- javascript - 在使用本地存储从另一个表保存的 HTML TABLE 中打印 JSON,以便我在另一个页面上打印我的表
- php - 使用 Lumen 在数据库中存储发布请求字段
- python - 当预测值相同时,train_test_split 影响结果
- angular - Firebase 可以不用任何中间层直接连接到 Angular 吗?