如何在Keras中计算矢量明智的点积? [英] How to calculate vector-wise dot product in Keras?
本文介绍了如何在Keras中计算矢量明智的点积?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望在Keras中计算矢量明智的点积.
详细地说,我的意思是,如果我有两个张量A
和B
,它们的形状均为(None, 30, 100)
,我想计算形状为(None, 30, 1)
的结果C,它将满足
I hope to calculate a vector wise dot product in Keras.
In detail, I mean if I have two tensor A
and B
, both with shape (None, 30, 100)
, I want to calculate the result C with shape (None, 30, 1)
which would satisfy
C[:,:,i] = dot(A[:,:,i], B[:,:,i]).
我想知道是否可行,因为后端中的batch_dot()
函数将仅返回形状(None, 30, 30)
并且仅具有关系
I wonder if that is possible, since the batch_dot()
function in the backend would only return the shape (None, 30, 30)
and only have the relationship
C[:,i,j] = dot(A[:,:,i], B[:,:,j])
但这不是我想要的.
谢谢!
推荐答案
您可以尝试以下方法:
import keras.backend as K
C = K.sum(A * B,axis=-1,keepdims=True)
这篇关于如何在Keras中计算矢量明智的点积?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文