[英]Given a batch of n images, how to scalar multiply each image by a different scalar in tensorflow?
Assume we have two TensorFlow tensors: input
and weights
.假设我们有两个 TensorFlow 张量: input
和weights
。
input
is a tensor of n images, say.例如, input
是 n 个图像的张量。 So its shape is [n, H, W, C].所以它的形状是[n, H, W, C]。 weights
is a simple list of n scalar weights: [w1 w2... wn]
weights
是 n 个标量权重的简单列表: [w1 w2... wn]
The aim is to scalar-multiply each image by its corresponding weight.目的是将每个图像与其相应的权重进行标量相乘。
How would one do that?如何做到这一点?
I tried to use tf.nn.conv2D with 1x1 kernels but I do not know how to reshape our rank 1 weight tensor into the required rank 4 kernel tensor.我尝试将 tf.nn.conv2D 与 1x1 内核一起使用,但我不知道如何将我们的 1 阶权重张量重塑为所需的 4 阶 kernel 张量。
Any help would be appreciated.任何帮助,将不胜感激。
Thanks to user zihaozhihao:感谢用户zihaozhihao:
The answer is to change the shape of weights
to (-1, 1, 1, 1) and then multiply it with input
.答案是将weights
的形状更改为 (-1, 1, 1, 1),然后将其与input
相乘。
weights = tf.reshape(weights, (-1, 1, 1, 1))
weighted_input = input * weights
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.