首页 文章

Tensorflow将3D批量张量与2D权重相乘

提问于
浏览
0

我有两个张量,形状如下所示,

batch.shape = [?, 5, 4]
weight.shape = [3, 5]

通过将权重乘以批处理中的每个元素,我想得到

result.shape = [?, 3, 4]

实现这一目标的最有效方法是什么?

1 回答

  • 1

    试试这个:

    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])
    

相关问题