0
0
TensorFlowml~10 mins

tf.data.Dataset creation 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 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.