Get length of a dataset in Tensorflow
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Getting the length of a tf.data.Dataset is easy only when TensorFlow actually knows the dataset cardinality. Some datasets have a fixed finite size, some are infinite, and some become unknown after certain transformations. That is why there is no single answer that works for every pipeline.
The most reliable API is dataset.cardinality(). It tells you whether the size is known, infinite, or unknown, which is much more useful than assuming every dataset behaves like a Python list.
Use cardinality() First
For a simple finite dataset:
This returns 4 for a dataset whose length TensorFlow can determine statically.
That is usually the best starting point because it works directly with the tf.data abstraction instead of forcing it into plain Python container semantics.
Known, Unknown, and Infinite Cardinality
Not every dataset has a concrete finite length. For example:
An infinitely repeated dataset does not have a usable finite length. Other transformations can produce unknown cardinality, where TensorFlow cannot infer the size ahead of time even if the dataset eventually ends.
This distinction matters when you compute steps per epoch, estimate training time, or debug a pipeline.
What About len(dataset)?
In eager execution, len(dataset) may work for some simple finite datasets, but it is not the most robust or general approach. It can fail or mislead you once the pipeline becomes more dynamic.
That is why code written for real TensorFlow pipelines should usually prefer:
instead of assuming len() is always available.
Fallback: Count by Iteration
If the cardinality is unknown and you still need the exact count, you can iterate:
This works even when TensorFlow cannot infer the size statically. The tradeoff is that you must consume the dataset to count it, which can be expensive or impossible for very large or streaming pipelines.
Practical Guidance for Training Loops
If you need steps per epoch, use the known source size when possible before complex transformations obscure it. For example, if the raw dataset has 10_000 examples and you batch with size 32, calculate steps from that known number instead of trying to reverse-engineer it later from a transformed input pipeline.
For repeated or streaming datasets, you usually should not ask for length at all. Instead, define steps_per_epoch explicitly based on training design.
Common Pitfalls
- Treating every
tf.data.Datasetlike a Python list with a guaranteed length. - Using
len(dataset)and being surprised when it fails on more complex pipelines. - Forgetting that
.repeat()can create an infinite dataset. - Counting by iteration on a huge dataset just to print a number, which can be unnecessarily expensive.
- Assuming unknown cardinality means the dataset is broken. It often just means TensorFlow cannot infer the count statically.
Summary
- The preferred way to ask for dataset length in TensorFlow is
dataset.cardinality(). - A dataset can be finite, infinite, or have unknown cardinality.
- '
len(dataset)may work in simple cases, but it is not the most general solution.' - If cardinality is unknown, you can count by iterating, though that has a cost.
- For training pipelines, it is often better to reason from known source sizes or explicit
steps_per_epoch.
Related reading
- get the CUDA and CUDNN version on windows with Anaconda installe
- Get the last output of a dynamic_rnn in TensorFlow
- Getting a prediction from an ONNX model in python
- Getting different results from Keras model.evaluate and model.predict
- Get the bounding box coordinates in the TensorFlow object detection API tutorial
- Get the diagonal of a matrix in TensorFlow
- Get the label mappings from label encoder
- Get the value of some weights in a model trained by TensorFlow
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.