我有一个案例,在numpy中特定维度的两个矩阵相乘可以正常运行,但在tensorflow中无法运行。
x = np.ndarray(shape=(10,20,30), dtype = float)
y = np.ndarray(shape=(30,40), dtype = float)
z = np.matmul(x,y)
print("np shapes: %s x %s = %s" % (np.shape(x), np.shape(y), np.shape(z)))
这个程序按预期运行并打印输出:
np shapes: (10, 20, 30) x (30, 40) = (10, 20, 40)
然而,在tensorflow中,当我试图将占位符和与上述numpy数组相同形状的变量相乘时,会出现错误。
x = tf.placeholder(tf.float32, shape=(10,20,30))
y = tf.Variable(tf.truncated_normal([30,40], name='w'))
print("tf shapes: %s x %s" % (x.get_shape(), y.get_shape()))
tf.matmul(x,y)
结果显示
tf shapes: (10, 20, 30) x (30, 40)
InvalidArgumentError:
Shape must be rank 2 but is rank 3 for 'MatMul_12'
(op: 'MatMul') with input shapes: [10,20,30], [30,40].
为什么这个操作失败了?
y = tf.Variable(tf.truncated_normal([30,40], name='w')+tf.zeros((10,30,40)))
来触发广播。相关问题(可能因错误而关闭)-- https://github.com/tensorflow/tensorflow/issues/216 - Yaroslav Bulatovnp.einsum('ijk,kl->ijl', x, y)
相同。 - Kuba