2. Which of the following is the correct syntax to create a dataset from a tensor data_tensor using TensorFlow?
easy
A. dataset = tf.data.Dataset.from_tensor_slices(data_tensor)
B. dataset = tf.data.Dataset.create_from_tensor(data_tensor)
C. dataset = tf.data.Dataset.tensor_slices(data_tensor)
D. dataset = tf.data.from_tensor_slices(data_tensor)
Solution
Step 1: Recall the correct method name
The correct TensorFlow method to create a dataset from tensor slices is tf.data.Dataset.from_tensor_slices().
Step 2: Check syntax correctness
dataset = tf.data.Dataset.from_tensor_slices(data_tensor) matches the exact syntax. Options A, B, and D use incorrect method names or missing parts.
Final Answer:
dataset = tf.data.Dataset.from_tensor_slices(data_tensor) -> Option A
Quick Check:
Correct method name and syntax = dataset = tf.data.Dataset.from_tensor_slices(data_tensor) [OK]
Hint: Use exact method: Dataset.from_tensor_slices() [OK]
Common Mistakes:
Using wrong method names
Missing Dataset class before method
Confusing with other dataset creation functions
3. What will be the output of the following code?
import tensorflow as tf
x = tf.constant([[1, 2], [3, 4], [5, 6]])
dataset = tf.data.Dataset.from_tensor_slices(x)
for element in dataset:
print(element.numpy())
medium
A. [[1 2]
[3 4]
[5 6]]
B. [[1], [2], [3], [4], [5], [6]]
C. [1, 2, 3, 4, 5, 6]
D. [1 2]
[3 4]
[5 6]
Solution
Step 1: Understand from_tensor_slices behavior
The method slices the tensor row-wise, so each element is a 1D tensor representing one row.
Step 2: Analyze the loop output
Each iteration prints one row as a numpy array, so output lines are [1 2], then [3 4], then [5 6].
Final Answer:
[1 2]
[3 4]
[5 6] -> Option D
Quick Check:
Row-wise slices printed line by line = [1 2]
[3 4]
[5 6] [OK]
import tensorflow as tf
x = tf.constant([1, 2, 3])
dataset = tf.data.Dataset.from_tensor_slices(x)
for element in dataset:
print(element.numpy())
print(dataset.batch(2))
medium
A. Calling batch() after iteration does not return a new dataset.
B. print(dataset.batch(2)) prints a dataset object, not batches.
C. from_tensor_slices() requires a list, not a tensor.
D. The loop should use dataset.batch(2) instead of dataset.
Solution
Step 1: Understand batch() output
The batch() method returns a new dataset object that groups elements, but printing it directly shows the object info, not the batch contents.
Step 2: Check what print(dataset.batch(2)) does
It prints a dataset representation, not the actual batched data. To see batches, you must iterate over it.
Final Answer:
print(dataset.batch(2)) prints a dataset object, not batches. -> Option B
Quick Check:
Printing dataset.batch() shows object info, not data [OK]
Hint: Iterate to see batches; print shows object info only [OK]
You want to create a dataset that pairs each feature row with its label for training. Which code correctly creates this dataset?
hard
A. dataset = tf.data.Dataset.from_tensor_slices((features, labels))
B. dataset = tf.data.Dataset.from_tensor_slices(features).zip(labels)
C. dataset = tf.data.Dataset.from_tensor_slices(features + labels)
D. dataset = tf.data.Dataset.from_tensor_slices(features).batch(labels)
Solution
Step 1: Understand pairing tensors in dataset
To pair features and labels, pass a tuple of tensors to from_tensor_slices(). This creates dataset elements as (feature_row, label) pairs.
Step 2: Evaluate each option
dataset = tf.data.Dataset.from_tensor_slices((features, labels)) correctly uses a tuple. dataset = tf.data.Dataset.from_tensor_slices(features).zip(labels) tries to zip a tensor, which is invalid. dataset = tf.data.Dataset.from_tensor_slices(features + labels) adds tensors incorrectly. dataset = tf.data.Dataset.from_tensor_slices(features).batch(labels) misuses batch() with labels.
Final Answer:
dataset = tf.data.Dataset.from_tensor_slices((features, labels)) -> Option A