0
0
TensorFlowml~20 mins

Dataset from tensors 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 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)
A[([1, 2], 1), ([3, 4], 0), ([5, 6], 1)]
B[([1, 2], 0), ([3, 4], 0), ([5, 6], 0)]
C[([1, 2], 0), ([3, 4], 1), ([5, 6], 0)]
DSyntaxError
Attempts:
2 left
💡 Hint
Remember that from_tensor_slices pairs elements by index from each tensor.
data_output
intermediate
1: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)
A4
B2
C3
D0
Attempts:
2 left
💡 Hint
Count how many pairs are created from the tensors.
🔧 Debug
advanced
2: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))
AValueError: All input tensors must have the same first dimension size
BTypeError: Cannot convert list to tensor
CSyntaxError: invalid syntax
DNo error, dataset created successfully
Attempts:
2 left
💡 Hint
Check the shapes of the tensors passed to from_tensor_slices.
🚀 Application
advanced
2: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])
Adataset = tf.data.Dataset.from_tensor_slices(features, labels).batch(2)
Bdataset = tf.data.Dataset.from_tensor_slices((features, labels)).batch(2)
Cdataset = tf.data.Dataset.from_tensors((features, labels)).batch(2)
Ddataset = tf.data.Dataset.from_tensor_slices([features, labels]).batch(2)
Attempts:
2 left
💡 Hint
Check the correct method signature for from_tensor_slices and how to batch datasets.
🧠 Conceptual
expert
3: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?
AThe entire tensor data is copied into the dataset memory immediately
BThe dataset compresses the tensors to save memory
CThe dataset stores only the first slice of the tensors in memory
DThe dataset holds references to the original tensors without copying data
Attempts:
2 left
💡 Hint
Think about how TensorFlow datasets handle input tensors internally.