Reputation: 1290
I'm new to Keras and I can't seem to find an equivalent to Pytorchs bmm function or Tensorflows matmul function.
What would be the closest equivalent to this in Keras?
Upvotes: 2
Views: 1714
Reputation: 3453
Backend functions simply point back to their tensorflow/theano sources and cannot be used as is. To use them you need to wrap them into a Lambda layer:
from keras.layers import Lambda
from keras import backend as K
# this is simply defining the function
matmul = Lambda ( lambda x: K.dot(x[0], x[1]) )
# this is applying the function
tensor_product = matmul([tensor1, tensor2])
Not wrapping backend functions into Lambda layers will result in TypeError
. Alternatively you can use the Dot
layer which computes the dot product across an axis of your choice:
from keras.layers import Dot
tensor_product = Dot(axes=-1)([tensor1, tensor2])
Upvotes: 0
Reputation: 464
Tecnically.. I think the direct equivalent to tf.matmul is K.batch_dot. this way you do not use the batch dimension.
Upvotes: 0
Reputation: 2378
keras.backend.dot
From the documentation:
Multiplies 2 tensors (and/or variables) and returns a tensor.
Upvotes: 2