Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

TensorFlow · Module

Dataset.from_tensor_slices

Creates a Dataset by slicing supplied tensors along their first dimension. Each resulting element preserves the input structure and contains one corresponding slice.

How it works

The method builds a dataset by taking corresponding slices along the first dimension of the supplied tensors. It removes that dimension from each element while retaining the tensors’ nested structure, so tuples or dictionaries become structured dataset elements.

Syntax

tf.data.Dataset.from_tensor_slices(tensors, name=None) -> 'DatasetV2'

Parameters

NameTypeDescription
tensorsTensors or nested structures of tensorsThe tensors or nested tensor structure to slice along the first dimension. Inputs supplied together must have matching first-dimension sizes.
namestringOptional name for the operation.

Returns

A DatasetV2 whose elements are first-dimension slices of the supplied tensors, with the input structure preserved.

Key points

  • Use it to turn in-memory tensor data into an iterable Dataset.
  • Dataset length follows the size of dimension zero.
  • Tuples and other supported nested structures remain structured in each dataset element.
  • Each element has the original tensor shape with its leading dimension removed.
  • A vector such as [1, 2, 3] yields three scalar dataset values.
  • A matrix is emitted row by row, with each row becoming one element.

Common mistakes

  • Every tensor supplied together must have an equal length along its first dimension; otherwise the slices cannot be aligned.
  • Do not confuse this method with from_tensors: from_tensors creates one dataset element, whereas this method creates one element per first-dimension slice.

Example: Slice feature rows and labels

import tensorflow as tf

features = tf.constant([[1, 10], [2, 20], [3, 30]])
labels = tf.constant([0, 1, 0])
dataset = tf.data.Dataset.from_tensor_slices((features, labels))

for feature, label in dataset:
    print(feature.numpy(), label.numpy())

Creates three dataset elements, each pairing one row of features with the corresponding label.

When to use it

Create supervised datasets from features and labels

Training from in-memory arrays

Stream one sample at a time from a tensor

Iterating over rows or examples

Build elements from tuples or nested tensor structures

Preserving structured inputs

Interview questions

QWhat does Dataset.from_tensor_slices do?

It returns a Dataset whose elements are formed from first-dimension slices of the input. The first dimension becomes the dataset length, and that dimension is removed from each produced element.

Follow-up: What happens when a 2D tensor is supplied? Each row becomes one 1D tensor element, preserving the remaining dimensions.

QWhat constraint applies when multiple tensors are supplied?

All inputs are sliced along dimension zero in lockstep. Therefore, their first dimensions must have the same size so each produced element can contain corresponding pieces.

Follow-up: How would you represent paired inputs such as examples and labels? Pass them as a tuple, producing tuple-shaped dataset elements.

QHow does the input rank affect each dataset element?

A 1D input produces scalar elements, while a 2D input produces 1D elements. Nested input structures are retained in the resulting dataset element specification.

Follow-up: When should from_tensors be used instead? Use it when the entire input should be one dataset element rather than several slices.

  • tf.data.Dataset.from_tensors
  • tf.data.Dataset.batch
  • tf.data.Dataset.map
  • tf.data.Dataset.as_numpy_iterator

Source: tf.data.Dataset. Checked 2026-10-15.