gensim most_similar with positive and negative, how does it work?

自作多情 提交于 2020-12-15 06:49:10

问题


I was reading this answer That says about Gensim most_similar:

it performs vector arithmetic: adding the positive vectors, subtracting the negative, then from that resulting position, listing the known-vectors closest to that angle.

But when I tested it, that is not the case. I trained a Word2Vec with Gensim "text8" dataset and tested these two:

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

>>> [('queen', 0.7131118178367615), ('prince', 0.6359186768531799),...]

model.wv.most_similar([model["king"] + model["woman"] - model["man"]])

>>> [('king', 0.84305739402771), ('queen', 0.7326322793960571),...]

They are clearly not the same. even the queen score in the first is 0.713 and on the second 0.732 which are not the same.

So I ask the question again, How does Gensim most_similar work? why the result of the two above are different?


回答1:


The adding and subtracting isn't all that it does; for an exact description, you should look at the source code:

https://github.com/RaRe-Technologies/gensim/blob/develop/gensim/models/keyedvectors.py#LC690:~:text=def%20most_similar,self%2C

You'll see there that the addition and subtraction is on the unit-normed version of each vector, via the get_vector(key, use_norm=True) accessor.

If you change your use of model[key] to model.get_vector(key, use_norm=True), you should see your outside-the-method calculation of the target vector give the same results as letting the method combine the positive and negative vectors.



来源:https://stackoverflow.com/questions/65059959/gensim-most-similar-with-positive-and-negative-how-does-it-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!