Convert TensorFlow Tensor data type in Python
Python TensorFlow Basic: Exercise-15 with Solution
Write a Python program that converts a TensorFlow tensor from one data type to another (e.g., from int32 to float64).
Sample Solution:
Python Code:
Output:
Original Tensor (int32): [100 200 300] Converted Tensor (float64): [100. 200. 300.]
Explanation:
In the exercise above -
- Import TensorFlow as tf.
- Create a TensorFlow tensor tensor_int32 with data type int32 using tf.constant().
- Use tf.cast() to convert the tensor_int32 tensor to the float64 data type, storing the result in tensor_float64.
- Finally, we print both the original and the converted tensor.
Python Code Editor:
Previous: Specified TensorFlow Data Type in Python.
Next: Create and print TensorFlow string tensor in Python.
What is the difficulty level of this exercise?