0
0
TensorFlowml~10 mins

Type casting in TensorFlow - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to cast a tensor to float32.

TensorFlow
import tensorflow as tf
x = tf.constant([1, 2, 3])
y = tf.cast(x, [1])
Drag options to blanks, or click blank then click option'
Atf.float32
Btf.int64
Ctf.bool
Dtf.string
Attempts:
3 left
💡 Hint
Common Mistakes
Using tf.int64 will keep the tensor as integers.
Using tf.bool or tf.string will cause errors or unexpected results.
2fill in blank
medium

Complete the code to cast a float tensor to int32.

TensorFlow
import tensorflow as tf
x = tf.constant([1.5, 2.7, 3.9])
y = tf.cast(x, [1])
Drag options to blanks, or click blank then click option'
Atf.float64
Btf.int32
Ctf.bool
Dtf.string
Attempts:
3 left
💡 Hint
Common Mistakes
Using tf.float64 keeps the tensor as floats.
Using tf.bool or tf.string causes errors.
3fill in blank
hard

Fix the error in casting a boolean tensor to float32.

TensorFlow
import tensorflow as tf
x = tf.constant([True, False, True])
y = tf.cast(x, [1])
Drag options to blanks, or click blank then click option'
Atf.int32
Btf.string
Ctf.float32
Dtf.bool
Attempts:
3 left
💡 Hint
Common Mistakes
Using tf.string causes runtime errors.
Using tf.bool does not change the type.
4fill in blank
hard

Fill both blanks to cast a tensor of integers to boolean and then back to int64.

TensorFlow
import tensorflow as tf
x = tf.constant([0, 1, 2, 0])
y = tf.cast(x, [1])
z = tf.cast(y, [2])
Drag options to blanks, or click blank then click option'
Atf.bool
Btf.float32
Ctf.int64
Dtf.string
Attempts:
3 left
💡 Hint
Common Mistakes
Casting directly to tf.string causes errors.
Casting to tf.float32 is not needed here.
5fill in blank
hard

Fill all three blanks to create a tensor, cast it to float64, then to int32, and finally to boolean.

TensorFlow
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])
Drag options to blanks, or click blank then click option'
Atf.float64
Btf.int32
Ctf.bool
Dtf.string
Attempts:
3 left
💡 Hint
Common Mistakes
Casting to tf.string causes errors.
Changing the order of casts may cause unexpected results.