Given a batch of n images, how to scalar multiply each image by a different scalar in tensorflow?

Viewed 386

Assume we have two TensorFlow tensors: input and weights.

input is a tensor of n images, say. So its shape is [n, H, W, C]. weights is a simple list of n scalar weights: [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.

Any help would be appreciated.

1 Answers

Thanks to user zihaozhihao:

The answer is to change the shape of weights to (-1, 1, 1, 1) and then multiply it with input.

weights = tf.reshape(weights, (-1, 1, 1, 1))
weighted_input = input * weights
Related