0
0
TensorFlowml~20 mins

tf.data.Dataset creation in TensorFlow - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
TensorFlow Dataset Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A[[1], [2], [3], [4], [5], [6]]
B[1, 2, 3, 4, 5, 6]
C[[1, 3, 5], [2, 4, 6]]
D[[1, 2], [3, 4], [5, 6]]
Attempts:
2 left
💡 Hint
Remember that from_tensor_slices slices the first dimension, so each element corresponds to one sublist.
data_output
intermediate
2: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)
A0
B10
C5
D1
Attempts:
2 left
💡 Hint
Count how many times the generator yields values.
🔧 Debug
advanced
2: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)
ATypeError: Expected list or tuple
BValueError: All components must have the same size
CNo error, prints elements
DAttributeError: 'dict' object has no attribute 'numpy'
Attempts:
2 left
💡 Hint
Check if all lists in the dictionary have the same length.
visualization
advanced
2: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)
A[1, 4, 9, 16]
B[1, 2, 3, 4]
C[2, 4, 6, 8]
D[1, 8, 27, 64]
Attempts:
2 left
💡 Hint
The map function squares each element.
🚀 Application
expert
3:00remaining
Creating a Dataset from multiple numpy arrays with different shapes
You have two numpy arrays:
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
Adataset = tf.data.Dataset.from_tensor_slices((arr1, arr2))
Bdataset = tf.data.Dataset.from_tensor_slices(arr1).concatenate(tf.data.Dataset.from_tensor_slices(arr2))
Cdataset = tf.data.Dataset.from_tensor_slices(arr1).zip(tf.data.Dataset.from_tensor_slices(arr2))
Ddataset = tf.data.Dataset.from_tensor_slices(arr1 + arr2)
Attempts:
2 left
💡 Hint
from_tensor_slices can take a tuple of arrays with matching first dimension.