如何在张量流中缩放张量?

Gin*_*ger 0 tensorflow

例如,我的张量形状是[128,128,3]并且它的范围是随机的,那么我想将这个张量中的所有num缩放到[0,255],我应该使用tensorflow中的哪个函数来做它?谢谢

bot*_*aio 5

您可以自己进行缩放.

// x is your tensor
current_min = tf.reduce_min(x)
current_max = tf.reduce_max(x)
target_min = 0
target_max = 255

// scale to [0; 1]
x = (x - current_min) / (current_max - current_min)

// scale to [target_min; target_max]
x = x * (target_max - target_min) + target_min
Run Code Online (Sandbox Code Playgroud)

当所有值相等时,您只需处理边缘情况.