Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Computing cosine similarity between two tensors in Keras

Tags:

python

keras

I have been following a tutorial that shows how to make a word2vec model.

This tutorial uses this piece of code:

similarity = merge([target, context], mode='cos', dot_axes=0) (no other info was given, but I suppose this comes from keras.layers)

Now, I've researched a bit on the merge method but I couldn't find much about it. From what I understand, it has been replaced by a lot of functions like layers.Add(), layers.Concat()....

What should I use? There's .Dot(), which has an axis parameter (which seems to be correct) but no mode parameter.

What can I use in this case?

like image 844
IndieDev Avatar asked Jun 23 '18 16:06

IndieDev


1 Answers

The Dot layer in Keras now supports built-in Cosine similarity using the normalize = True parameter.

From the Keras Docs:

keras.layers.Dot(axes, normalize=True)

normalize: Whether to L2-normalize samples along the dot product axis before taking the dot product. If set to True, then the output of the dot product is the cosine proximity between the two samples.

Source

like image 163
Kyle Piira Avatar answered Oct 04 '22 22:10

Kyle Piira