In numpy
您可以将 2d 数组与 3d 数组相乘,如下例所示:
>>> X = np.random.randn(3,5,4) # [3,5,4]
... W = np.random.randn(5,5) # [5,5]
... out = np.matmul(W, X) # [3,5,4]
据我了解,np.matmul()
takes W
并沿着第一维度广播它X
。但在tensorflow
不允许:
>>> _X = tf.constant(X)
... _W = tf.constant(W)
... _out = tf.matmul(_W, _X)
ValueError: Shape must be rank 2 but is rank 3 for 'MatMul_1' (op: 'MatMul') with input shapes: [5,5], [3,5,4].
那么是否有一个等价的东西np.matmul()
上面做的tensorflow
?最好的做法是什么tensorflow
将 2d 张量与 3d 张量相乘?