TensorFlow tf.data Pipelines 1 β Questions and Answers
Question 1: Which tf.data method applies a function to each element of a dataset?
- dataset.map() (Correct answer)
- dataset.apply()
- dataset.transform()
- dataset.process()
Correct answer: dataset.map()
dataset.map() applies a given function to every element in the dataset, enabling preprocessing transformations.
Question 2: What does dataset.batch(32) do in tf.data?
- Groups consecutive elements into batches of 32 (Correct answer)
- Shuffles 32 elements
- Repeats dataset 32 times
- Takes first 32 elements
Correct answer: Groups consecutive elements into batches of 32
dataset.batch(n) combines n consecutive dataset elements into a single batched element.
Question 3: Which tf.data method shuffles dataset elements?
- dataset.shuffle(buffer_size) (Correct answer)
- dataset.randomize()
- dataset.mix()
- dataset.permute()
Correct answer: dataset.shuffle(buffer_size)
dataset.shuffle() randomly shuffles elements within a buffer of the specified size before drawing the next element.
Question 4: What does dataset.prefetch(tf.data.AUTOTUNE) do?
- Prepares the next batch while the current one is being processed (Correct answer)
- Caches dataset to memory
- Parallelizes map operations
- Compresses dataset elements
Correct answer: Prepares the next batch while the current one is being processed
prefetch() overlaps the data preprocessing and model execution to reduce training bottlenecks.
Question 5: Which function creates a tf.data.Dataset from a list of NumPy arrays?
- tf.data.Dataset.from_tensor_slices() (Correct answer)
- tf.data.Dataset.from_array()
- tf.data.Dataset.load()
- tf.data.Dataset.from_numpy()
Correct answer: tf.data.Dataset.from_tensor_slices()
from_tensor_slices() slices the first dimension of each array to create individual dataset elements.
Question 6: What does dataset.repeat(3) do in tf.data?
- Iterates through the dataset 3 times (Correct answer)
- Repeats each element 3 times
- Duplicates the dataset into 3 copies
- Creates 3 parallel data streams
Correct answer: Iterates through the dataset 3 times
dataset.repeat(n) repeats the entire dataset n times, or indefinitely if no argument is given.
Which tf.data method applies a function to each element of a dataset?