Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
546 views
in Technique[技术] by (71.8m points)

python - Computing cosine similarity between two tensors in 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...