[英]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,)))
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的维数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.