Bird
Raised Fist0
TensorFlowml~20 mins

Dataset from tensors in TensorFlow - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What does tf.data.Dataset.from_tensor_slices() do in TensorFlow?
easy
A. It merges multiple datasets into one.
B. It converts a dataset back into tensors.
C. It creates a dataset by slicing the input tensors row-wise.
D. It shuffles the dataset randomly.

Solution

  1. Step 1: Understand the function purpose

    tf.data.Dataset.from_tensor_slices() takes tensors and creates a dataset by slicing them row-wise, so each element is one slice.
  2. Step 2: Compare with other options

    Options B, C, and D describe different dataset operations, not the slicing creation step.
  3. Final Answer:

    It creates a dataset by slicing the input tensors row-wise. -> Option C
  4. Quick Check:

    Dataset from tensor slices = row-wise slicing [OK]
Hint: Remember: from_tensor_slices splits tensors row-wise [OK]
Common Mistakes:
  • Confusing from_tensor_slices with shuffling
  • Thinking it merges datasets
  • Assuming it converts datasets back to tensors
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

  1. 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().
  2. 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.
  3. Final Answer:

    dataset = tf.data.Dataset.from_tensor_slices(data_tensor) -> Option A
  4. 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

  1. Step 1: Understand from_tensor_slices behavior

    The method slices the tensor row-wise, so each element is a 1D tensor representing one row.
  2. 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].
  3. Final Answer:

    [1 2] [3 4] [5 6] -> Option D
  4. Quick Check:

    Row-wise slices printed line by line = [1 2] [3 4] [5 6] [OK]
Hint: from_tensor_slices outputs row slices printed separately [OK]
Common Mistakes:
  • Expecting full tensor printed at once
  • Confusing row slices with flattened output
  • Assuming column-wise slicing
4. Identify the error in this code snippet:
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

  1. 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.
  2. 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.
  3. Final Answer:

    print(dataset.batch(2)) prints a dataset object, not batches. -> Option B
  4. Quick Check:

    Printing dataset.batch() shows object info, not data [OK]
Hint: Iterate to see batches; print shows object info only [OK]
Common Mistakes:
  • Expecting print to show batch data
  • Thinking batch modifies original dataset in place
  • Confusing tensor and list input types
5. You have two tensors:
features = tf.constant([[1, 2], [3, 4], [5, 6]])
labels = tf.constant([0, 1, 0])

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

  1. 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.
  2. 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.
  3. Final Answer:

    dataset = tf.data.Dataset.from_tensor_slices((features, labels)) -> Option A
  4. Quick Check:

    Tuple input pairs tensors row-wise = dataset = tf.data.Dataset.from_tensor_slices((features, labels)) [OK]
Hint: Use tuple inside from_tensor_slices to pair tensors [OK]
Common Mistakes:
  • Trying to zip a tensor directly
  • Adding tensors instead of pairing
  • Using batch() incorrectly with labels