🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEtensorflow

tensorflow Documentation

LOADING ENGINE...

tf.data.Dataset.from_tensor_slices()

AI & DATA SCIENCE // tf-data-dataset-from-tensor-slices

tf.data.Dataset.from_tensor_slices() creates a Dataset by slicing a tensor (or array) along its first dimension, producing one element per row.

Syntax

tf.data.Dataset.from_tensor_slices(tensors)

Deep Dive Course

from_tensor_slices() is the most common starting point for building a tf.data pipeline from data already in memory — passing it an array of shape (num_examples, ...) produces a Dataset yielding num_examples separate elements, each one row/slice of the original array. Passing a tuple of arrays, like (features, labels), produces a Dataset yielding matching (feature, label) pairs, one per index, which is exactly the format model.fit() expects when training with a Dataset instead of raw arrays.

1Understanding tf.data.Dataset.from_tensor_slices()

from_tensor_slices() is the most common starting point for building a tf.data pipeline from data already in memory — passing it an array of shape (num_examples, ...) produces a Dataset yielding num_examples separate elements, each one row/slice of the original array. Passing a tuple of arrays, like (features, labels), produces a Dataset yielding matching (feature, label) pairs, one per index, which is exactly the format model.fit() expects when training with a Dataset instead of raw arrays.

💡

Pass a tuple of (features, labels) arrays to from_tensor_slices() to build a Dataset that yields properly paired (feature, label) elements directly usable by model.fit(), rather than building the features and labels as two separate, unlinked Datasets.

editor.html
import tensorflow as tf

dataset = tf.data.Dataset.from_tensor_slices([1, 2, 3, 4])
for element in dataset:
    print(element.numpy())
localhost:3000

2Practical Example

Here is a real-world application of tf.data.Dataset.from_tensor_slices() showing how it is used in production TensorFlow code.

editor.html
import tensorflow as tf

features = [[1, 2], [3, 4], [5, 6]]
labels = [0, 1, 0]
dataset = tf.data.Dataset.from_tensor_slices((features, labels))
for x, y in dataset:
    print(x.numpy(), y.numpy())
localhost:3000

3Best Practices

Follow these guidelines when working with tf.data.Dataset.from_tensor_slices():

1. Use from_tensor_slices() as the standard starting point for building a tf.data pipeline from in-memory NumPy arrays or tensors

2. Pass features and labels together as a single tuple to from_tensor_slices() so the resulting Dataset yields matching (feature, label) pairs

3. Chain .batch(), .shuffle(), and .prefetch() after from_tensor_slices() to build a complete, efficient input pipeline rather than using the raw sliced dataset directly

⚠️

Tip: Pass a tuple of (features, labels) arrays to from_tensor_slices() to build a Dataset that yields properly paired (feature, label) elements directly usable by model.fit(), rather than building the features and labels as two separate, unlinked Datasets.

editor.html
import tensorflow as tf

dataset = tf.data.Dataset.from_tensor_slices([1, 2, 3, 4])
for element in dataset:
    print(element.numpy())
localhost:3000

Examples

Example 01Basic Usage
import tensorflow as tf

dataset = tf.data.Dataset.from_tensor_slices([1, 2, 3, 4])
for element in dataset:
    print(element.numpy())
Example 02Advanced Example
import tensorflow as tf

features = [[1, 2], [3, 4], [5, 6]]
labels = [0, 1, 0]
dataset = tf.data.Dataset.from_tensor_slices((features, labels))
for x, y in dataset:
    print(x.numpy(), y.numpy())

Best Practices

  • Use from_tensor_slices() as the standard starting point for building a tf.data pipeline from in-memory NumPy arrays or tensors
  • Pass features and labels together as a single tuple to from_tensor_slices() so the resulting Dataset yields matching (feature, label) pairs
  • Chain .batch(), .shuffle(), and .prefetch() after from_tensor_slices() to build a complete, efficient input pipeline rather than using the raw sliced dataset directly

Interview Question

Why does from_tensor_slices() slice along the first dimension of an array, rather than treating the whole array as one single dataset element?

Hint: Think about what the 'slices' in the function name refers to, and what shape of data model.fit() typically expects a dataset to yield.

The name from_tensor_slices() directly describes its behavior: it treats an array's first dimension as the example dimension, and slices the array along exactly that dimension, producing one dataset element per index along it — this matches the standard convention throughout TensorFlow and Keras where the first dimension of a batch of data represents individual examples. If from_tensor_slices() instead treated the whole array as a single dataset element, you'd get a dataset containing just one giant element rather than one element per training example, which wouldn't be usable with model.fit() or any batching/shuffling operation, all of which are built around a dataset yielding many separate, individual examples that can be grouped, reordered, and batched flexibly.

Exercises

MediumPractice using tf.data.Dataset.from_tensor_slices() in a real scenario.
View Solution
import tensorflow as tf

dataset = tf.data.Dataset.from_tensor_slices([1, 2, 3, 4])
for element in dataset:
    print(element.numpy())

Frequently Asked Questions

Why does from_tensor_slices() slice along the first dimension of an array, rather than treating the whole array as one single dataset element?

The name from_tensor_slices() directly describes its behavior: it treats an array's first dimension as the example dimension, and slices the array along exactly that dimension, producing one dataset element per index along it — this matches the standard convention throughout TensorFlow and Keras where the first dimension of a batch of data represents individual examples. If from_tensor_slices() instead treated the whole array as a single dataset element, you'd get a dataset containing just one giant element rather than one element per training example, which wouldn't be usable with model.fit() or any batching/shuffling operation, all of which are built around a dataset yielding many separate, individual examples that can be grouped, reordered, and batched flexibly.

Related Functions

dataset-batchdataset-shufflemodel-fit