Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between most_similar and similar_by_vector in gensim word2vec?

I was confused with the results of most_similar and similar_by_vector from gensim's Word2vecKeyedVectors. They are supposed to calculate cosine similarities in the same way - however:

Running them with one word gives identical results, for example: model.most_similar(['obama']) and similar_by_vector(model['obama'])

but if I give it an equation:

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

gives:

[('queen', 0.7515910863876343), ('monarch', 0.6741327047348022), ('princess', 0.6713887453079224), ('kings', 0.6698989868164062), ('kingdom', 0.5971318483352661), ('royal', 0.5921063423156738), ('uncrowned', 0.5911505818367004), ('prince', 0.5909028053283691), ('lady', 0.5904011130332947), ('monarchs', 0.5884358286857605)]

while with:

q = model['king'] - model['man'] + model['woman']
model.similar_by_vector(q)

gives:

[('king', 0.8655095100402832), ('queen', 0.7673765420913696), ('monarch', 0.695580005645752), ('kings', 0.6929547786712646), ('princess', 0.6909604668617249), ('woman', 0.6528975963592529), ('lady', 0.6286187767982483), ('prince', 0.6222133636474609), ('kingdom', 0.6208546161651611), ('royal', 0.6090123653411865)]

There is a noticable difference in cosine distance of the words queen, monarch... etc. I'm wondering why?

Thanks!

like image 730
peidaqi Avatar asked Feb 05 '23 00:02

peidaqi


1 Answers

The most_similar similar function retrieves the vectors corresponding to "king", "woman" and "man", and normalizes them before computing king - man + woman (source code: use_norm=True).

The function call model.similar_by_vector(v) just calls model.most_similar(positive=[v]). So the difference is due to most_similar having a behaviour depending on the type of input (string or vector).

Finally, when most_similar has string inputs, it removes the words from the output (that is why "king" does not appear in the results).

A piece of code to see the differences:

>>> un = False
>>> v = model.word_vec("king", use_norm=un) + model.word_vec("woman", use_norm=un) - model.word_vec("man", use_norm=un)
>>> un = True
>>> v2 = model.word_vec("king", use_norm=un) + model.word_vec("woman", use_norm=un) - model.word_vec("man", use_norm=un)
>>> model.most_similar(positive=[v], topn=6)
[('king', 0.8449392318725586), ('queen', 0.7300517559051514), ('monarch', 0.6454660892486572), ('princess', 0.6156251430511475), ('crown_prince', 0.5818676948547363), ('prince', 0.5777117609977722)]
>>> model.most_similar(positive=[v2], topn=6)
[('king', 0.7992597222328186), ('queen', 0.7118192911148071), ('monarch', 0.6189674139022827), ('princess', 0.5902431011199951), ('crown_prince', 0.5499460697174072), ('prince', 0.5377321243286133)]
>>> model.most_similar(positive=["king", "woman"], negative=["man"], topn=6)
[('queen', 0.7118192911148071), ('monarch', 0.6189674139022827), ('princess', 0.5902431011199951), ('crown_prince', 0.5499460697174072), ('prince', 0.5377321243286133), ('kings', 0.5236844420433044)]
like image 95
mcoav Avatar answered May 12 '23 20:05

mcoav