Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spark MLLib's Word2Vec cosine similarity greater than 1

http://spark.apache.org/docs/latest/mllib-feature-extraction.html#word2vec

On the spark implementation of word2vec, when the number of iterations or data partitions are greater than one, for some reason, the cosine similarity is greater than 1.

In my knowledge, cosine similarity should always be about -1 < cos < 1. Does anyone know why?

like image 729
Jason Xie Avatar asked Sep 27 '22 09:09

Jason Xie


1 Answers

In findSynonyms method of word2vec, it does not calculate cosine similarity v1・vi / |v1| |vi|, instead it calculates v1・vi / |vi|, where v1 is the vector of the query word and vi is the vector of the candidate words. That's why the value sometimes exceeds 1. Just to find closer words, it is not necessary to divide by |v1| because it is constant.

like image 174
Kotaro Tanahashi Avatar answered Oct 11 '22 00:10

Kotaro Tanahashi