计算 Keras 中两个张量之间的余弦相似度 [英] Computing cosine similarity between two tensors in Keras

查看:41
本文介绍了计算 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(), which 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 understanding:

对于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 已被弃用,我们必须为余弦相似度 自己定义和构建一个层.一般来说,这将涉及使用那些小写函数,我们将它们包装在 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆