[英]Multiply 4-d tensor with 1-d tensor
我有一个秩4张量与尺寸[batch_size, num_rows, num_cols, num_values]
和秩1张量与尺寸[num_values]
我想计算第四列中的值与等级1 Tensor的点积,得出尺寸为[batch_size, num_rows, num_cols, 1]
的等级4 Tensor,然后可以将其tf.squeeze
转换为具有尺寸[batch_size, num_rows, num_cols]
。 有谁知道我该怎么做?
您可以使用tensordot
或reduce_sum
:
a = tf.constant(np.random.rand(2, 3, 5, 7))
b = tf.constant(np.random.rand(7))
tf.tensordot(a, b, [-1, -1]) # <tf.Tensor 'Tensordot_1:0' shape=(2, 3, 5) dtype=float64>
tf.reduce_sum(a * b, axis=-1) # <tf.Tensor 'Tensordot_1:0' shape=(2, 3, 5) dtype=float64>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.