Changing the scale of a tensor in tensorflow

agupta231 picture agupta231 · Jul 14, 2016 · Viewed 17.1k times · Source

Sorry if I messed up the title, I didn't know how to phrase this. Anyways, I have a tensor of a set of values, but I want to make sure that every element in the tensor has a range from 0 - 255, (or 0 - 1 works too). However, I don't want to make all the values add up to 1 or 255 like softmax, I just want to down scale the values.

Is there any way to do this?

Thanks!

Answer

Will Glück picture Will Glück · Jul 14, 2016

You are trying to normalize the data. A classic normalization formula is this one:

normalize_value = (value − min_value) / (max_value − min_value)

The implementation on tensorflow will look like this:

tensor = tf.div(
   tf.subtract(
      tensor, 
      tf.reduce_min(tensor)
   ), 
   tf.subtract(
      tf.reduce_max(tensor), 
      tf.reduce_min(tensor)
   )
)

All the values of the tensor will be betweetn 0 and 1.

IMPORTANT: make sure the tensor has float/double values, or the output tensor will have just zeros and ones. If you have a integer tensor call this first:

tensor = tf.to_float(tensor)

Update: as of tensorflow 2, tf.to_float() is deprecated and instead, tf.cast() should be used:

tensor = tf.cast(tensor, dtype=tf.float32) # or any other tf.dtype, that is precise enough