Bird
Raised Fist0
TensorFlowml~10 mins

Type casting in TensorFlow - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What does tf.cast(tensor, dtype) do in TensorFlow?
easy
A. Changes the data type of a tensor to the specified dtype
B. Changes the shape of a tensor
C. Creates a new tensor filled with zeros
D. Deletes a tensor from memory

Solution

  1. Step 1: Understand the purpose of tf.cast

    tf.cast is used to convert the data type of a tensor to another type, such as from float32 to int32.
  2. Step 2: Compare with other options

    Changing shape, creating zeros, or deleting tensors are done by other functions, not tf.cast.
  3. Final Answer:

    Changes the data type of a tensor to the specified dtype -> Option A
  4. Quick Check:

    tf.cast changes data type = D [OK]
Hint: tf.cast changes data type, not shape or content [OK]
Common Mistakes:
  • Confusing type casting with reshaping
  • Thinking tf.cast creates new tensors with zeros
  • Assuming tf.cast deletes tensors
2. Which of the following is the correct syntax to cast a tensor x to tf.float64?
easy
A. tf.cast(x, tf.float64)
B. tf.cast(tf.float64, x)
C. tf.convert(x, tf.float64)
D. tf.change_type(x, tf.float64)

Solution

  1. Step 1: Recall tf.cast syntax

    The correct syntax is tf.cast(tensor, dtype), where the first argument is the tensor and the second is the target data type.
  2. Step 2: Check each option

    tf.cast(x, tf.float64) matches the correct syntax. Options B, C, and D use incorrect function names or argument orders.
  3. Final Answer:

    tf.cast(x, tf.float64) -> Option A
  4. Quick Check:

    tf.cast(tensor, dtype) = A [OK]
Hint: tf.cast(tensor, dtype) always has tensor first [OK]
Common Mistakes:
  • Swapping arguments order
  • Using non-existent functions like tf.convert
  • Confusing function names
3. What is the output dtype of the following code?
import tensorflow as tf
x = tf.constant([1, 2, 3], dtype=tf.int32)
y = tf.cast(x, tf.float32)
print(y.dtype)
medium
A. tf.int32
B. tf.float32
C. tf.float64
D. tf.string

Solution

  1. Step 1: Identify original tensor dtype

    Tensor x has dtype tf.int32.
  2. Step 2: Apply tf.cast to convert dtype

    tf.cast converts x to tf.float32, so y's dtype is tf.float32.
  3. Final Answer:

    tf.float32 -> Option B
  4. Quick Check:

    tf.cast changes dtype to tf.float32 = A [OK]
Hint: tf.cast changes dtype to specified type exactly [OK]
Common Mistakes:
  • Assuming dtype stays the same after casting
  • Confusing float32 with float64
  • Expecting string dtype from numeric cast
4. Identify the error in this code snippet:
import tensorflow as tf
x = tf.constant([1.5, 2.5, 3.5])
y = tf.cast(x, tf.int32)
print(y)
medium
A. tf.cast cannot convert float to int
B. tf.constant must specify dtype explicitly
C. tf.cast requires a numpy array, not a tensor
D. No error; tf.cast truncates floats to ints correctly

Solution

  1. Step 1: Check if tf.cast supports float to int

    tf.cast can convert float tensors to int tensors by truncating the decimal part.
  2. Step 2: Verify code correctness

    The code runs without error and prints the truncated integer tensor.
  3. Final Answer:

    No error; tf.cast truncates floats to ints correctly -> Option D
  4. Quick Check:

    tf.cast truncates float to int without error = C [OK]
Hint: Casting float to int truncates decimals, no error [OK]
Common Mistakes:
  • Thinking float to int cast causes error
  • Believing dtype must be specified in tf.constant always
  • Assuming tf.cast needs numpy arrays
5. You have a tensor features with dtype tf.float64 but your model requires tf.float32. Which code snippet correctly converts features and avoids extra memory use?
hard
A. features = tf.Variable(features, dtype=tf.float32)
B. features = features.numpy().astype('float32')
C. features = tf.cast(features, tf.float32)
D. features = tf.convert_to_tensor(features, dtype=tf.float32)

Solution

  1. Step 1: Understand memory-efficient casting

    tf.cast converts tensor dtype efficiently without copying data unnecessarily.
  2. Step 2: Evaluate options for correct casting

    features = tf.cast(features, tf.float32) uses tf.cast correctly. features = tf.Variable(features, dtype=tf.float32) creates a variable which is heavier. features = features.numpy().astype('float32') converts to numpy array, which uses extra memory. features = tf.convert_to_tensor(features, dtype=tf.float32) converts but may create a new tensor, less efficient.
  3. Final Answer:

    features = tf.cast(features, tf.float32) -> Option C
  4. Quick Check:

    tf.cast is efficient dtype converter = B [OK]
Hint: Use tf.cast for efficient dtype conversion without extra copies [OK]
Common Mistakes:
  • Using numpy conversion causing extra memory use
  • Creating variables unnecessarily
  • Assuming tf.convert_to_tensor is always best