我有两个张量,形状如下所示,
batch.shape = [?, 5, 4]weight.shape = [3, 5]
batch.shape = [?, 5, 4]
weight.shape = [3, 5]
通过将权重乘以批处理中的每个元素,我想得到
result.shape = [?, 3, 4]
实现这一目标的最有效方法是什么?
试试这个:
newbatch = tf.transpose(batch,[1,0,2]) newbatch = tf.reshape(newbatch,[5,-1]) result = tf.matmul(weight,newbatch) result = tf.reshape(result,[3,-1,4]) result = tf.transpose(result, [1,0,2])
或者更紧凑:
newbatch = tf.reshape(tf.transpose(batch,[1,0,2]),[5,-1]) result = tf.transpose(tf.reshape(tf.matmul(weight,newbatch),[3,-1,4]), [1,0,2])
1 回答
试试这个:
或者更紧凑: