In numpy
you can multiply a 2d array with a 3d array as below example:
>>> X = np.random.randn(3,5,4) # [3,5,4]
... W = np.random.randn(5,5) # [5,5]
... out = np.matmul(W, X) # [3,5,4]
from my understanding, np.matmul()
takes W
and broadcast it along the first dimension of X
. But in tensorflow
it is not allowed:
>>> _X = tf.constant(X)
... _W = tf.constant(W)
... _out = tf.matmul(_W, _X)
ValueError: Shape must be rank 2 but is rank 3 for 'MatMul_1' (op: 'MatMul') with input shapes: [5,5], [3,5,4].
So is there a equivalent for what np.matmul()
does above in tensorflow
? And what's the best practice in tensorflow
for multiplying 2d tensor with 3d tensor?