Challenge - 5 Problems
TensorFlow Dataset Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of creating a dataset from tensors
What is the output of this code snippet that creates a TensorFlow dataset from tensors and iterates over it?
TensorFlow
import tensorflow as tf features = tf.constant([[1, 2], [3, 4], [5, 6]]) labels = tf.constant([0, 1, 0]) dataset = tf.data.Dataset.from_tensor_slices((features, labels)) output = [] for feature, label in dataset: output.append((feature.numpy().tolist(), label.numpy().item())) print(output)
Attempts:
2 left
💡 Hint
Remember that from_tensor_slices pairs elements by index from each tensor.
✗ Incorrect
The dataset pairs each row of features with the corresponding label by index. So the first feature [1, 2] pairs with label 0, the second with 1, and the third with 0.
❓ data_output
intermediate1:30remaining
Number of elements in a dataset from tensors
Given this TensorFlow dataset created from tensors, how many elements does it contain?
TensorFlow
import tensorflow as tf features = tf.constant([[10, 20], [30, 40], [50, 60], [70, 80]]) labels = tf.constant([1, 0, 1, 0]) dataset = tf.data.Dataset.from_tensor_slices((features, labels)) count = 0 for _ in dataset: count += 1 print(count)
Attempts:
2 left
💡 Hint
Count how many pairs are created from the tensors.
✗ Incorrect
The dataset creates one element per row in the tensors. Since there are 4 rows, the dataset has 4 elements.
🔧 Debug
advanced2:00remaining
Identify the error when creating a dataset from tensors
What error will this code raise when trying to create a dataset from tensors of different shapes?
TensorFlow
import tensorflow as tf features = tf.constant([[1, 2], [3, 4]]) labels = tf.constant([0, 1, 0]) dataset = tf.data.Dataset.from_tensor_slices((features, labels))
Attempts:
2 left
💡 Hint
Check the shapes of the tensors passed to from_tensor_slices.
✗ Incorrect
The tensors must have the same size in the first dimension to pair elements by index. Here, features has shape (2, 2) and labels has shape (3,), causing a ValueError.
🚀 Application
advanced2:30remaining
Creating a batched dataset from tensors
Which option correctly creates a TensorFlow dataset from tensors and batches it with batch size 2?
TensorFlow
import tensorflow as tf features = tf.constant([[1, 2], [3, 4], [5, 6], [7, 8]]) labels = tf.constant([0, 1, 0, 1])
Attempts:
2 left
💡 Hint
Check the correct method signature for from_tensor_slices and how to batch datasets.
✗ Incorrect
Option B correctly uses from_tensor_slices with a tuple of tensors and then batches the dataset by 2 elements per batch.
🧠 Conceptual
expert3:00remaining
Understanding memory usage of datasets from tensors
When creating a TensorFlow dataset from large tensors using from_tensor_slices, what is the main memory behavior?
Attempts:
2 left
💡 Hint
Think about how TensorFlow datasets handle input tensors internally.
✗ Incorrect
TensorFlow datasets created from tensors hold references to the original tensors and slice them on demand, avoiding copying all data upfront.