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:
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:
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.
If you build the dataset from a dictionary, print it as a dictionary:
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:
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:
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.
Print after transformations, not before assumptions
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.
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:
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
takewould be enough for debugging. - Forgetting that
from_tensor_slicesslices 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()oras_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_specwhen you need shape and dtype information in addition to sample values.

