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!
You can try something like:
import keras.backend as K
C = K.sum(A * B,axis=-1,keepdims=True)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With