计算Keras中两个张量之间的余弦相似度 [英] Computing cosine similarity between two tensors in Keras
问题描述
我一直在关注一个教程,该教程显示了如何制作word2vec模型.
I have been following a tutorial that shows how to make a word2vec model.
本教程使用以下代码:
similarity = merge([target, context], mode='cos', dot_axes=0)
(未提供其他信息,但我想这来自keras.layers
)
similarity = merge([target, context], mode='cos', dot_axes=0)
(no other info was given, but I suppose this comes from keras.layers
)
现在,我已经对merge
方法进行了一些研究,但对此却知之甚少.
据我了解,它已被许多功能取代,例如layers.Add(), layers.Concat()...
.
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()...
.
我应该使用什么?有一个.Dot()
,它有一个axis
参数(看起来是正确的),但是没有mode
参数.
What should I use? There's .Dot()
, that has an axis
parameter (which seems to be correct) but no mode
parameter.
在这种情况下我可以使用什么?
What can I use in this case?
推荐答案
Keras文档中有一些尚不清楚的事情,我认为理解这些至关重要:
There are a few things that are unclear from the Keras documentation that I think are crucial to understand:
对于Merge
的keras文档中的每个函数,都有一个小写和大写的定义,即add()
和Add()
.
For each function in the keras documentation for Merge
, there is a lower case and upper case one defined i.e. add()
and Add()
.
在Github上,farizrahman4u
概述了差异:
On Github, farizrahman4u
outlines the differences:
Merge is a layer.
Merge takes layers as input
Merge is usually used with Sequential models
merge is a function.
merge takes tensors as input.
merge is a wrapper around Merge.
merge is used in Functional API
Using Merge:
left = Sequential()
left.add(...)
left.add(...)
right = Sequential()
right.add(...)
right.add(...)
model = Sequential()
model.add(Merge([left, right]))
model.add(...)
using merge:
a = Input((10,))
b = Dense(10)(a)
c = Dense(10)(a)
d = merge([b, c])
model = Model(a, d)
要回答您的问题,由于不推荐使用Merge
,因此我们必须为cosine similarity
自己定义和构建一个层.通常,这将涉及使用小写的函数,这些函数将我们包装在Lambda
中,以创建可在模型中使用的层.
To answer your question, since Merge
has been deprecated, we have to define and build a layer ourselves for the cosine similarity
. In general this will involve using those lowercase functions, which we wrap within a Lambda
to create a layer that we can use within a model.
from keras import backend as K
def cosine_distance(vests):
x, y = vests
x = K.l2_normalize(x, axis=-1)
y = K.l2_normalize(y, axis=-1)
return -K.mean(x * y, axis=-1, keepdims=True)
def cos_dist_output_shape(shapes):
shape1, shape2 = shapes
return (shape1[0],1)
distance = Lambda(cosine_distance, output_shape=cos_dist_output_shape)([processed_a, processed_b]
根据您的数据,您可能需要删除L2规范化.关于该解决方案要注意的重要一点是,它是使用Keras函数api构建的,例如K.mean()
-我认为在定义自定义图层甚至损失函数时这是必需的.
Depending on your data, you may want to remove the L2 normalization. What is important to note about the solution is that it is built using the Keras function api e.g. K.mean()
- I think this is necessary when defining custom layer or even loss functions.
希望我很清楚,这是我的第一个SO答案!
Hope I was clear, this was my first SO answer!
这篇关于计算Keras中两个张量之间的余弦相似度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!