Search code examples
pythontensorflowmatrix-multiplication

Multiplying multidimensional matrices in tensorflow


When im trying to use tf.matmul to multiply a two Matrices with the shapes [41,22,512] & [512] I get the following Error:

ValueError: Shape must be rank 2 but is rank 3 for 'MatMul' (op: 'MatMul') 
with input shapes: [41,22,512], [512]..

I'd normally think that such a multiplication outputs a tensor with shape [41,22,1] or [41,22].


Solution

  • Matmul wont work as the inner dimensions are not valid matrix multiplication arguments, instead you can do:

    tf.reduce_sum(x * y, axis=2)