Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate vector-wise dot product in Keras?

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]).

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])

But that is not what I want.

Thank you!

like image 293
zhuzilin Avatar asked May 08 '17 17:05

zhuzilin


2 Answers

You can try something like:

import keras.backend as K

C = K.sum(A * B,axis=-1,keepdims=True)
like image 82
Daniel Möller Avatar answered Nov 15 '22 11:11

Daniel Möller


The batch_dot function is right for you, just include the correct axis. Assuming that A.shape = (2,3,4) and B.shape = (2,3,1), you will get C that has shape (2,4,1).

C = K.batch_dot(A, B, axes=1)
like image 25
Autonomous Avatar answered Nov 15 '22 11:11

Autonomous