Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert tf.int64 to tf.float32?

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)' 
like image 616
Yee Liu Avatar asked Feb 24 '16 08:02

Yee Liu


People also ask

How do I change the datatype in TensorFlow?

In Tensorflow 2, you can cast the datatype of a tensor to a new datatype by using the tf. cast function.

How do you transpose in TensorFlow?

transpose(x, perm=[1, 0]) . As above, simply calling tf. transpose will default to perm=[2,1,0] . To take the transpose of the matrices in dimension-0 (such as when you are transposing matrices where 0 is the batch dimension), you would set perm=[0,2,1] .

What are Dtypes in TensorFlow?

Classes. class DType : Represents the type of the elements in a Tensor .

What is TF cast?

The "tf. cast" function casts a tensor to new type. The operation "cast" support the data types of int32, int64, float16, float32, float64, complex64, complex128, bfloat16, uint8, uint16, uint32, uint64, int8, int16. Only the real part of "x" is returned in case of casting from complex types to real types.


1 Answers

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) 
like image 181
Mark McDonald Avatar answered Sep 28 '22 11:09

Mark McDonald