How to convert tf.int64 to tf.float32?

Yee Liu picture Yee Liu · Feb 24, 2016 · Viewed 56.5k times · Source

I tried:

test_image = tf.convert_to_tensor(img, dtype=tf.float32)

Then following error appears:

ValueError: Tensor conversion requested dtype float32 for Tensor with dtype int64: 'Tensor("test/ArgMax:0", shape=TensorShape([Dimension(None)]), dtype=int64)'

Answer

Mark McDonald picture Mark McDonald · Jun 14, 2016

You can cast generally using:

tf.cast(my_tensor, tf.float32)

Replace tf.float32 with your desired type.


Edit: It seems at the moment at least, that tf.cast won't cast to an unsigned dtype (e.g. tf.uint8). To work around this, you can cast to the signed equivalent and used tf.bitcast to get all the way. e.g.

tf.bitcast(tf.cast(my_tensor, tf.int8), tf.uint8)