TensorFlow
tf.data.Dataset
from_tensor_slices
print dataset
machine learning

How to print the result of tf.data.Dataset.from_tensor_slices?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

tf.data.Dataset.from_tensor_slices creates a dataset by slicing input tensors along the first dimension. To print what it contains, you do not print the dataset object itself in a meaningful way. You iterate over its elements, inspect a small sample with take, or convert elements to NumPy values when eager execution is enabled.

Start by iterating over the dataset

For a simple one-dimensional tensor, each dataset element is one slice from the first axis:

python
1import tensorflow as tf
2
3values = tf.constant([10, 20, 30])
4dataset = tf.data.Dataset.from_tensor_slices(values)
5
6for item in dataset:
7    print(item)

In TensorFlow 2, this runs eagerly by default and prints tensor objects such as tf.Tensor(10, shape=(), dtype=int32). That is already useful, but most debugging sessions are easier if you print plain values.

Convert elements to NumPy for cleaner output

If you want readable numbers or arrays, call .numpy() on each element:

python
for item in dataset:
    print(item.numpy())

That produces ordinary Python-facing values instead of the full tensor wrapper. It is usually the fastest way to answer "what is actually inside this dataset?"

Structured slices need structured printing

from_tensor_slices is often used with pairs of features and labels, or even dictionaries of inputs. The dataset preserves that structure.

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

If you build the dataset from a dictionary, print it as a dictionary:

python
1dataset = tf.data.Dataset.from_tensor_slices({
2    "age": tf.constant([25, 30]),
3    "score": tf.constant([0.8, 0.9])
4})
5
6for row in dataset:
7    print({k: v.numpy() for k, v in row.items()})

That pattern is helpful when debugging multi-input models.

Use take for safe inspection

Real datasets may be large, shuffled, batched, or even infinite after repeat(). In those cases, only inspect a few elements:

python
for item in dataset.take(3):
    print(item)

This keeps logs small and prevents accidentally iterating forever. It is especially important after adding repeat, streaming sources, or expensive preprocessing steps.

as_numpy_iterator() is convenient

Another clean option is converting the dataset to a NumPy iterator:

python
for item in dataset.as_numpy_iterator():
    print(item)

This is often the simplest inspection method when every element can be represented as NumPy arrays. It removes the need to call .numpy() on each tensor manually.

What you print depends on the current stage of the dataset pipeline. If you add batch, the elements are no longer scalars or single rows. They are batches.

python
1batched = tf.data.Dataset.from_tensor_slices(values).batch(2)
2
3for batch in batched:
4    print(batch.numpy())

Likewise, after map, you are printing transformed results, not the original slices. That makes take a very practical debugging tool for preprocessing pipelines.

element_spec helps when printing is not enough

If you mainly want to understand structure and shape, inspect the dataset specification too:

python
print(dataset.element_spec)

This tells you the expected tensor shapes and dtypes without consuming elements. It is not a replacement for printing sample data, but it is often the quickest way to confirm whether the pipeline shape matches your expectations.

Common Pitfalls

  • Printing the whole dataset when a small take would be enough for debugging.
  • Forgetting that from_tensor_slices slices along the first dimension.
  • Expecting plain Python values when TensorFlow is printing tensor objects.
  • Ignoring dataset transformations and then being surprised that the printed shape changed.
  • Iterating forever over a dataset that has been repeated without a stopping condition.

Summary

  • Iterate over the dataset to inspect elements created by from_tensor_slices.
  • Use .numpy() or as_numpy_iterator() for readable eager-mode output.
  • Use take(n) to inspect a small sample safely.
  • Print tuples and dictionaries according to the dataset structure.
  • Check element_spec when you need shape and dtype information in addition to sample values.

Course illustration
Course illustration

All Rights Reserved.