问题描述
在 numpy
中,您可以将 2d 数组与 3d 数组相乘,如下例所示:
根据我的理解,np.matmul()
取W
并沿X
的第一个维度广播它.但在 tensorflow
中是不允许的:
那么 np.matmul()
上面的 tensorflow
是否有等价物?tensorflow
中将 2d 张量与 3d 张量相乘的最佳实践是什么?
尝试使用 tf.tile 在乘法之前匹配矩阵的维度.numpy 的自动广播功能似乎没有在 tensorflow 中实现.您必须手动完成.
W_T = tf.tile(tf.expand_dims(W,0),[3,1,1])
这应该可以解决问题
将 numpy 导入为 np将张量流导入为 tfX = np.random.randn(3,4,5)W = np.random.randn(5,5)_X = tf.constant(X)_W = tf.constant(W)_W_t = tf.tile(tf.expand_dims(_W,0),[3,1,1])使用 tf.Session() 作为 sess:打印(sess.run(tf.matmul(_X,_W_t)))
In numpy
you can multiply a 2d array with a 3d array as below example:
>>> 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]
from my understanding, np.matmul()
takes W
and broadcast it along the first dimension of X
. But in tensorflow
it is not allowed:
>>> _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].
So is there a equivalent for what np.matmul()
does above in tensorflow
? And what's the best practice in tensorflow
for multiplying 2d tensor with 3d tensor?
Try using tf.tile to match the dimension of the matrix before multiplication. The automatic broadcast feature of numpy doesnt seem to be implemented in tensorflow. You have to do it manually.
W_T = tf.tile(tf.expand_dims(W,0),[3,1,1])
This should do the trick
import numpy as np
import tensorflow as tf
X = np.random.randn(3,4,5)
W = np.random.randn(5,5)
_X = tf.constant(X)
_W = tf.constant(W)
_W_t = tf.tile(tf.expand_dims(_W,0),[3,1,1])
with tf.Session() as sess:
print(sess.run(tf.matmul(_X,_W_t)))
这篇关于如何在张量流中用 3d 张量对 2d 张量进行 matmul?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!