简体   繁体   English

广播np.dot vs tf.matmul以进行张量矩阵乘法(形状必须为2级,但为3级错误)

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

Let's say I have the following tensors: 假设我有以下张量:

X = np.zeros((3,201, 340))
Y = np.zeros((340, 28))

Making a dot product of X, Y is successful with numpy, and yields a tensor of shape (3, 201, 28). 用numpy成功制作X,Y的点积,并产生形状的张量(3,201,28)。 However with tensorflow I get the following error: Shape must be rank 2 but is rank 3 error ... 但是使用tensorflow我得到以下错误: Shape must be rank 2 but is rank 3 error ...

minimal code example: 最小代码示例:

X = np.zeros((3,201, 340))
Y = np.zeros((340, 28))
print(np.dot(X,Y).shape) # successful (3, 201, 28)
tf.matmul(X, Y) # errornous

Any idea how to achieve the same result with tensorflow? 知道如何使用张量流实现相同的结果吗?

Since, you are working with tensors , it would be better (for performance) to use tensordot there than np.dot . 因为,你正在与tensors ,这将是更好(性能)使用tensordot那里比np.dot NumPy allows it (numpy.dot) to work on tensors through lowered performance and it seems tensorflow simply doesn't allow it. NumPy允许它(numpy.dot)通过降低性能来处理tensors ,而tensorflow似乎根本不允许这样做。

So, for NumPy, we would use np.tensordot - 因此,对于NumPy,我们将使用np.tensordot

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

For tensorflow , it would be with tf.tensordot - 对于tensorflow ,它将与tf.tensordot

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

Related post to understand tensordot . 相关文章了解tensordot

Tensorflow doesn't allow for multiplication of matrices with different ranks as numpy does. Tensorflow不允许像numpy一样乘以不同等级的矩阵。

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. 通过将一个矩阵“堆叠”在另一个矩阵之上,这实质上将矩阵3转换为矩阵3,然后转换为矩阵2。

You can use this: tf.reshape(tf.matmul(tf.reshape(Aijk,[i*j,k]),Bkl),[i,j,l]) 您可以使用以下代码: 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. 其中i,j和k是矩阵一的维数,而k和l是矩阵2的维数。

Taken from here . 取自这里

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:Shape必须是2级,但是'MatMul'的排名是3 - ValueError: Shape must be rank 2 but is rank 3 for 'MatMul' ValueError:形状必须为 2 级,但“MatMul”为 1 级 - ValueError: Shape must be rank 2 but is rank 1 for 'MatMul' tf.multiply vs tf.matmul计算点积 - tf.multiply vs tf.matmul to calculate the dot product np.dot() 与 Python 广播 - np.dot() with Python broadcasting tensorflow-tf.confusion_matrix()引发错误ValueError:Shape(2,2048,2)必须具有等级2 - tensorflow - tf.confusion_matrix() throws error ValueError: Shape (2, 2048, 2) must have rank 2 ValueError: Shape must be rank 2 but is rank 1 for 'MatMul' (op: 'MatMul') with input shape: [2], [2,3] - ValueError: Shape must be rank 2 but is rank 1 for 'MatMul' (op: 'MatMul') with input shapes: [2], [2,3] tf.concat给出“形状必须至少为2级但为1级”错误,即使两个张量的形状相同 - tf.concat giving 'Shape must be at least rank 2 but is rank 1' error even if both tensors are the same shape tf.sparse_to_dense:形状必须为等级1,但等级为0 - tf.sparse_to_dense: Shape must be rank 1 but is rank 0 tf.sets.intersection的错误:ValueError:形状必须至少为2级,但对于'DenseToDenseSetOperation_12'为1级 - Error for tf.sets.intersection: ValueError: Shape must be at least rank 2 but is rank 1 for 'DenseToDenseSetOperation_12' tensorflow 中的 tf.matmul(X,weight) 与 tf.matmul(X,tf.traspose(weight)) - tf.matmul(X,weight) vs tf.matmul(X,tf.traspose(weight)) in tensorflow
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM