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 Dataset from_tensor_slices with nested lists
What is the output of the following code snippet when iterated over?
import tensorflow as tf data = [[1, 2], [3, 4], [5, 6]] dataset = tf.data.Dataset.from_tensor_slices(data) output = [element.numpy().tolist() for element in dataset]
TensorFlow
import tensorflow as tf data = [[1, 2], [3, 4], [5, 6]] dataset = tf.data.Dataset.from_tensor_slices(data) output = [element.numpy().tolist() for element in dataset] print(output)
Attempts:
2 left
💡 Hint
Remember that from_tensor_slices slices the first dimension, so each element corresponds to one sublist.
✗ Incorrect
The from_tensor_slices method slices the input tensor along the first dimension. Since the input is a list of lists, each element yielded is one sublist, resulting in [[1, 2], [3, 4], [5, 6]].
❓ data_output
intermediate2:00remaining
Number of elements in Dataset from_generator
Given the following generator and dataset creation code, how many elements does the dataset contain?
import tensorflow as tf
def gen():
for i in range(5):
yield i * 2
dataset = tf.data.Dataset.from_generator(gen, output_signature=tf.TensorSpec(shape=(), dtype=tf.int32))
count = sum(1 for _ in dataset)TensorFlow
import tensorflow as tf def gen(): for i in range(5): yield i * 2 dataset = tf.data.Dataset.from_generator(gen, output_signature=tf.TensorSpec(shape=(), dtype=tf.int32)) count = sum(1 for _ in dataset) print(count)
Attempts:
2 left
💡 Hint
Count how many times the generator yields values.
✗ Incorrect
The generator yields 5 values (0, 2, 4, 6, 8), so the dataset contains 5 elements.
🔧 Debug
advanced2:00remaining
Identify the error in Dataset creation from dict
What error does the following code raise when executed?
import tensorflow as tf
data = {'a': [1, 2], 'b': [3, 4, 5]}
dataset = tf.data.Dataset.from_tensor_slices(data)
for element in dataset:
print(element)TensorFlow
import tensorflow as tf data = {'a': [1, 2], 'b': [3, 4, 5]} dataset = tf.data.Dataset.from_tensor_slices(data) for element in dataset: print(element)
Attempts:
2 left
💡 Hint
Check if all lists in the dictionary have the same length.
✗ Incorrect
from_tensor_slices requires all components to have the same size along the first dimension. Here, 'a' has length 2 and 'b' has length 3, causing a ValueError.
❓ visualization
advanced2:00remaining
Visualize Dataset elements after map transformation
What is the output list after applying the map function to the dataset?
import tensorflow as tf data = [1, 2, 3, 4] dataset = tf.data.Dataset.from_tensor_slices(data) dataset = dataset.map(lambda x: x * x) output = [element.numpy() for element in dataset]
TensorFlow
import tensorflow as tf data = [1, 2, 3, 4] dataset = tf.data.Dataset.from_tensor_slices(data) dataset = dataset.map(lambda x: x * x) output = [element.numpy() for element in dataset] print(output)
Attempts:
2 left
💡 Hint
The map function squares each element.
✗ Incorrect
The map applies the lambda function x*x to each element, so the output is the squares of the original list.
🚀 Application
expert3:00remaining
Creating a Dataset from multiple numpy arrays with different shapes
You have two numpy arrays:
Which code snippet correctly creates a tf.data.Dataset that yields tuples of corresponding elements from arr1 and arr2?
import numpy as np import tensorflow as tf arr1 = np.array([[1, 2], [3, 4], [5, 6]]) arr2 = np.array([10, 20, 30])
Which code snippet correctly creates a tf.data.Dataset that yields tuples of corresponding elements from arr1 and arr2?
TensorFlow
import numpy as np import tensorflow as tf arr1 = np.array([[1, 2], [3, 4], [5, 6]]) arr2 = np.array([10, 20, 30]) # Choose the correct dataset creation code
Attempts:
2 left
💡 Hint
from_tensor_slices can take a tuple of arrays with matching first dimension.
✗ Incorrect
Option A correctly creates a dataset yielding tuples (arr1[i], arr2[i]). Option A creates a zipped dataset but is more verbose. Option A tries to add arrays element-wise which is invalid for slicing. Option A concatenates datasets, not pairs elements.