简体   繁体   English

给定一批 n 图像,如何将每个图像与 tensorflow 中的不同标量进行标量相乘?

[英]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 张量: inputweights

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.

相关问题 如何仅对Tensorflow中的给定索引将矩阵乘以标量? - How do I multiply a matrix by a scalar only for given indices in Tensorflow? 如何将随机 TensorFlow 张量与标量相乘? - How can I multiply a random TensorFlow tensor with a scalar? 在张量流中使用标量进行批量乘法/除法 - Batch multiplication/division with scalar in tensorflow 将标量乘以带有 tensorflow keras 后端的张量 - Multiply a scalar to a tensor with tensorflow keras backend 如何在TensorFlow中的模型中将标量张量转换为标量? - How to convert a scalar tensor to scalar in a model in TensorFlow? 如何使用批处理在TensorFlow中进行矩阵标量乘法? - How to do matrix-scalar multiplication in TensorFlow with batch? 如何将标量密集层输出转换为矩阵,其中每个元素都等于 tensorflow 2.0 中的标量? - How to convert a scalar dense layer output into a matrix with each element being equal to the scalar in tensorflow 2.0? Tensorflow (Python):如何将标量附加到张量中的每一行 - Tensorflow (Python): How to append a scalar to each row in a tensor Tensorflow:如何在python中将标量张量转换为标量变量? - Tensorflow: How to convert scalar tensor to scalar variable in python? 在Tensorflow中,如何生成标量汇总? - In Tensorflow, how do I generate a scalar summary?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM