广播 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)

假设我有以下张量:

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

使用 numpy 对 X、Y 进行点积是成功的,并产生形状为 (3, 201, 28) 的张量。 但是使用tensorflow我得到以下错误:Shape must be rank 2 but is rank 3 error ...

最小代码示例:

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

知道如何使用 tensorflow 实现相同的结果吗?

Tensorflow 不允许像 numpy 那样对具有不同秩的矩阵进行乘法。

为了解决这个问题,您可以重塑矩阵。这本质上是一个矩阵, 比方说,排名 3 与排名 2 "stacking the matrices" 一个在另一个之上。

你可以使用这个: tf.reshape(tf.matmul(tf.reshape(Aijk,[i*j,k]),Bkl),[i,j,l])

其中 i、j 和 k 是矩阵 1 的维度,k 和 l 是矩阵 2 的维度。

摘自 here.

由于您正在使用 tensors,因此使用 tensordotnp.dot 更好(为了性能)。 NumPy 允许它 (numpy.dot) 通过降低性能在 tensors 上工作,但似乎 tensorflow 根本不允许它。

因此,对于 NumPy,我们将使用 np.tensordot -

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

对于tensorflow,它将是 tf.tensordot -

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

.