Broadcasting np.dot vs tf.matmul for tensor-matrix multiplication (Shape must be rank 2 but is rank 3 error)

依然范特西╮ 提交于 2019-12-06 16:34:02

Since, you are working with tensors, it would be better (for performance) to use tensordot there than np.dot. NumPy allows it (numpy.dot) to work on tensors through lowered performance and it seems tensorflow simply doesn't allow it.

So, for NumPy, we would use np.tensordot -

np.tensordot(X, Y, axes=((2,),(0,)))

For tensorflow, it would be with tf.tensordot -

tf.tensordot(X, Y, axes=((2,),(0,)))

Related post to understand tensordot.

Tensorflow doesn't allow for multiplication of matrices with different ranks as numpy does.

To cope with this, you can reshape the matrix. This essentially casts a matrix of, say, rank 3 to one with rank 2 by "stacking the matrices" one on top of the other.

You can use this: tf.reshape(tf.matmul(tf.reshape(Aijk,[i*j,k]),Bkl),[i,j,l])

where i, j and k are the dimensions of matrix one and k and l are the dimensions of matrix 2.

Taken from here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!