Complete the code to create a TensorFlow dataset from a list of numbers.
import tensorflow as tf numbers = [1, 2, 3, 4, 5] dataset = tf.data.Dataset.[1](numbers)
The method from_tensor_slices creates a dataset by slicing the input tensor or list element-wise.
Complete the code to create a dataset from a range of numbers from 0 to 9.
import tensorflow as tf dataset = tf.data.Dataset.[1](10)
The range method creates a dataset of numbers from 0 up to (but not including) the given limit.
Fix the error in the code to create a dataset from a generator function.
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))
The from_generator method creates a dataset from a Python generator function. It requires an output signature to specify the shape and type of the elements.
Fill both blanks to create a dataset from a list of tuples and specify the output types.
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])))
from_tensor_slices creates a dataset by slicing the list of tuples. The output signature must match the types of the tuple elements: first is int32, second is float32.
Fill all three blanks to create a dataset from a generator with correct output signature for strings.
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]))
from_generator creates a dataset from the generator function. The output signature specifies the shape as scalar (), dtype as tf.string, and a name for the tensor.