Bird
Raised Fist0
TensorFlowml~10 mins

tf.data.Dataset creation 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 create a TensorFlow dataset from a list of numbers.

TensorFlow
import tensorflow as tf
numbers = [1, 2, 3, 4, 5]
dataset = tf.data.Dataset.[1](numbers)
Drag options to blanks, or click blank then click option'
Afrom_tensor_slices
Bfrom_list
Cfrom_array
Dfrom_data
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-existent method like from_list or from_array.
Trying to create a dataset directly from a list without using the correct method.
2fill in blank
medium

Complete the code to create a dataset from a range of numbers from 0 to 9.

TensorFlow
import tensorflow as tf
dataset = tf.data.Dataset.[1](10)
Drag options to blanks, or click blank then click option'
Arange
Bfrom_list
Cfrom_generator
Dfrom_tensor_slices
Attempts:
3 left
💡 Hint
Common Mistakes
Using from_tensor_slices with an integer instead of a list or tensor.
Using from_generator without defining a generator function.
3fill in blank
hard

Fix the error in the code to create a dataset from a generator function.

TensorFlow
import tensorflow as tf

def gen():
    for i in range(3):
        yield i

dataset = tf.data.Dataset.[1](gen, output_signature=tf.TensorSpec(shape=(), dtype=tf.int32))
Drag options to blanks, or click blank then click option'
Arange
Bfrom_generator
Cfrom_tensor_slices
Dfrom_list
Attempts:
3 left
💡 Hint
Common Mistakes
Using from_tensor_slices which expects a tensor or list, not a generator.
Not providing the output_signature argument.
4fill in blank
hard

Fill both blanks to create a dataset from a list of tuples and specify the output types.

TensorFlow
import tensorflow as tf

data = [(1, 2.0), (3, 4.0), (5, 6.0)]
dataset = tf.data.Dataset.[1](data, output_signature=(tf.TensorSpec(shape=(), dtype=tf.int32), tf.TensorSpec(shape=(), dtype=[2])))
Drag options to blanks, or click blank then click option'
Afrom_generator
Btf.float32
Cfrom_tensor_slices
Dtf.int32
Attempts:
3 left
💡 Hint
Common Mistakes
Using from_generator without a generator function.
Mismatching the output_signature types with the data.
5fill in blank
hard

Fill all three blanks to create a dataset from a generator with correct output signature for strings.

TensorFlow
import tensorflow as tf

def gen():
    yield "apple"
    yield "banana"
    yield "cherry"

dataset = tf.data.Dataset.[1](gen, output_signature=tf.TensorSpec(shape=(), dtype=[2], name=[3]))
Drag options to blanks, or click blank then click option'
Afrom_tensor_slices
Btf.string
C"fruit"
Dfrom_generator
Attempts:
3 left
💡 Hint
Common Mistakes
Using from_tensor_slices instead of from_generator.
Setting dtype to a numeric type instead of tf.string.
Not providing a name or using an invalid name.

Practice

(1/5)
1. What is the main purpose of tf.data.Dataset in TensorFlow?
easy
A. To compile TensorFlow models
B. To create neural network layers
C. To visualize data in graphs
D. To manage and prepare data efficiently for TensorFlow models

Solution

  1. Step 1: Understand the role of tf.data.Dataset

    tf.data.Dataset is designed to handle data input pipelines, making data loading and preprocessing easier for TensorFlow models.
  2. Step 2: Differentiate from other TensorFlow components

    Creating layers, visualization, and compiling models are handled by other TensorFlow modules, not tf.data.Dataset.
  3. Final Answer:

    To manage and prepare data efficiently for TensorFlow models -> Option D
  4. Quick Check:

    tf.data.Dataset = data management [OK]
Hint: Remember: Dataset is for data, not model building [OK]
Common Mistakes:
  • Confusing dataset with model layers
  • Thinking it visualizes data
  • Assuming it compiles models
2. Which of the following is the correct way to create a tf.data.Dataset from a Python list [1, 2, 3]?
easy
A. dataset = tf.data.Dataset.from_tensor_slices([1, 2, 3])
B. dataset = tf.data.Dataset.from_list([1, 2, 3])
C. dataset = tf.data.Dataset.create([1, 2, 3])
D. dataset = tf.data.Dataset.make([1, 2, 3])

Solution

  1. Step 1: Recall correct Dataset creation methods

    The method from_tensor_slices is the standard way to create a dataset from a list or tensor by slicing elements.
  2. Step 2: Identify incorrect method names

    Methods like from_list, create, and make do not exist in TensorFlow's Dataset API.
  3. Final Answer:

    dataset = tf.data.Dataset.from_tensor_slices([1, 2, 3]) -> Option A
  4. Quick Check:

    Use from_tensor_slices for lists [OK]
Hint: Use from_tensor_slices to convert lists to datasets [OK]
Common Mistakes:
  • Using non-existent methods like from_list
  • Confusing Dataset creation with model creation
  • Trying to call Dataset directly
3. What will be the output of the following code?
import tensorflow as tf
list_data = [10, 20, 30]
dataset = tf.data.Dataset.from_tensor_slices(list_data)
for item in dataset:
    print(item.numpy())
medium
A. Tensor objects printed
B. [10, 20, 30]
C. 10 20 30 (each on a new line)
D. Error: Cannot iterate dataset

Solution

  1. Step 1: Understand from_tensor_slices behavior

    This method creates a dataset where each element is one item from the list, so iteration yields 10, then 20, then 30.
  2. Step 2: Analyze the loop and print statement

    Calling item.numpy() converts each tensor element to a Python number, printing each on its own line.
  3. Final Answer:

    10 20 30 (each on a new line) -> Option C
  4. Quick Check:

    Iterate dataset prints each element [OK]
Hint: from_tensor_slices yields one element per iteration [OK]
Common Mistakes:
  • Expecting a list printed at once
  • Not calling .numpy() to get values
  • Thinking iteration causes error
4. Identify the error in the following code snippet:
import tensorflow as tf
list_data = [1, 2, 3]
dataset = tf.data.Dataset.from_tensor(list_data)
medium
A. Method from_tensor does not exist
B. list_data should be a tensor, not a list
C. Dataset cannot be created from lists
D. Missing parentheses in Dataset call

Solution

  1. Step 1: Check Dataset API methods

    There is no method called from_tensor in the tf.data.Dataset API.
  2. Step 2: Correct method usage

    The correct method to create a dataset from a list or tensor is from_tensor_slices.
  3. Final Answer:

    Method from_tensor does not exist -> Option A
  4. Quick Check:

    Use from_tensor_slices, not from_tensor [OK]
Hint: Check method names carefully in Dataset API [OK]
Common Mistakes:
  • Using non-existent methods
  • Confusing from_tensor_slices with from_tensor
  • Assuming Dataset accepts lists directly without slicing
5. You want to create a tf.data.Dataset from a generator function that yields tuples of (features, label). Which of the following is the correct way to create this dataset?
hard
A. dataset = tf.data.Dataset.from_tensors(generator_func)
B. dataset = tf.data.Dataset.from_generator(generator_func, output_types=(tf.float32, tf.int32))
C. dataset = tf.data.Dataset.from_tensor_slices(generator_func())
D. dataset = tf.data.Dataset.from_list(generator_func)

Solution

  1. Step 1: Understand dataset creation from generators

    Use from_generator to create a dataset from a Python generator function, specifying output types.
  2. Step 2: Analyze other options

    from_tensor_slices expects a tensor or list, not a generator function; from_tensors creates a dataset with one element; from_list does not exist.
  3. Final Answer:

    dataset = tf.data.Dataset.from_generator(generator_func, output_types=(tf.float32, tf.int32)) -> Option B
  4. Quick Check:

    Use from_generator with output_types for generators [OK]
Hint: Use from_generator with output_types for generator functions [OK]
Common Mistakes:
  • Using from_tensor_slices on generator functions
  • Calling non-existent from_list method
  • Not specifying output_types with from_generator