例如,我们使用gensim
训练word2vec模型:
from gensim import corpora, models, similarities
from gensim.models.word2vec import Word2Vec
documents = ["Human machine interface for lab abc computer applications",
"A survey of user opinion of computer system response time",
"The EPS user interface management system",
"System and human system engineering testing of EPS",
"Relation of user perceived response time to error measurement",
"The generation of random binary unordered trees",
"The intersection graph of paths in trees",
"Graph minors IV Widths of trees and well quasi ordering",
"Graph minors A survey"]
texts = [[word for word in document.lower().split()] for document in documents]
w2v_model = Word2Vec(texts, size=500, window=5, min_count=1)
当我们查询单词之间的相似度时,发现存在负相似度得分:
>>> w2v_model.similarity('graph', 'computer')
0.046929569156789336
>>> w2v_model.similarity('graph', 'system')
0.063683518562347399
>>> w2v_model.similarity('survey', 'generation')
-0.040026775040430063
>>> w2v_model.similarity('graph', 'trees')
-0.0072684112978664561
如何解释负分数?
如果是余弦相似性,范围不应该是[0,1]
吗?
Word2Vec.similarity(x,y)
函数的上下限是多少?文档中没有太多介绍:https://radimrehurek.com/gensim/models/word2vec.html#gensim.models.word2vec.Word2Vec.similarity =(
看Python包装器代码,也没有太多信息: https://github.com/RaRe-Technologies/gensim/blob/develop/gensim/models/word2vec.py#L1165
(如果可能,请指向实现相似函数的.pyx
代码。)