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)'
In Tensorflow 2, you can cast the datatype of a tensor to a new datatype by using the tf. cast function.
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] .
Classes. class DType : Represents the type of the elements in a Tensor .
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With