Complete the code to cast a tensor to float32.
import tensorflow as tf x = tf.constant([1, 2, 3]) y = tf.cast(x, [1])
tf.cast converts the tensor to the specified data type. Here, tf.float32 is the correct type for floating point numbers.
Complete the code to cast a float tensor to int32.
import tensorflow as tf x = tf.constant([1.5, 2.7, 3.9]) y = tf.cast(x, [1])
tf.int32 converts the float tensor to integers by truncating the decimal part.
Fix the error in casting a boolean tensor to float32.
import tensorflow as tf x = tf.constant([True, False, True]) y = tf.cast(x, [1])
Boolean tensors can be cast to float32 to get 1.0 for True and 0.0 for False.
Fill both blanks to cast a tensor of integers to boolean and then back to int64.
import tensorflow as tf x = tf.constant([0, 1, 2, 0]) y = tf.cast(x, [1]) z = tf.cast(y, [2])
First cast integers to boolean (0 becomes False, others True), then cast back to int64 (False=0, True=1).
Fill all three blanks to create a tensor, cast it to float64, then to int32, and finally to boolean.
import tensorflow as tf x = tf.constant([10, 0, 5]) y = tf.cast(x, [1]) z = tf.cast(y, [2]) w = tf.cast(z, [3])
The tensor is first cast to float64, then to int32 (truncating decimals if any), and finally to boolean (0 becomes False, others True).