machine learning
deep learning
tensorflow
data processing
model training

tensorflowYour input ran out of data

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

The TensorFlow message "Your input ran out of data" means the training loop expected more batches than the dataset actually produced. In practice, this usually happens because steps_per_epoch is too large, the dataset is finite and not repeated, or the generator stops earlier than the model configuration assumes.

What the Error Really Means

When you call model.fit, TensorFlow needs enough batches to satisfy the requested training schedule. If the input pipeline ends first, training cannot continue and you get the exhaustion error.

The mismatch is usually between one of these pairs:

  • dataset length versus steps_per_epoch
  • validation dataset length versus validation_steps
  • finite dataset versus multi-epoch training expectations
  • generator output versus the batch count TensorFlow expects

So the problem is usually in input-pipeline accounting, not in the model architecture itself.

A Common Failing Pattern

Here is a simple example that can fail because the dataset has only a few batches but steps_per_epoch asks for more.

python
1import tensorflow as tf
2
3x = tf.range(20, dtype=tf.float32)
4y = x * 2
5
6dataset = tf.data.Dataset.from_tensor_slices((x, y)).batch(4)
7
8model = tf.keras.Sequential([
9    tf.keras.layers.Input(shape=(1,)),
10    tf.keras.layers.Dense(1)
11])
12model.compile(optimizer="adam", loss="mse")
13
14model.fit(dataset, epochs=3, steps_per_epoch=10)

This dataset only yields 5 batches per pass, so asking for 10 steps per epoch creates the mismatch.

Fix 1: Let TensorFlow Infer the Length

If your dataset has a well-defined finite size, the simplest fix is often to remove steps_per_epoch and let TensorFlow use the dataset length automatically.

python
model.fit(dataset, epochs=3)

This is the cleanest solution when the dataset is already batched and finite.

Fix 2: Repeat the Dataset Intentionally

If you truly want a fixed number of steps regardless of the base dataset size, repeat the dataset explicitly.

python
dataset = tf.data.Dataset.from_tensor_slices((x, y)).batch(4).repeat()
model.fit(dataset, epochs=3, steps_per_epoch=10)

Now TensorFlow can keep drawing batches because the dataset is infinite.

Use this only when you understand the sampling behavior you want. Repetition is not a bandage; it changes the data stream.

Fix 3: Correct Generator Logic

If you use a Python generator or keras.utils.Sequence, the generator itself may be the issue. It must yield batches consistently for as long as TensorFlow expects.

For example, a generator that returns only a few batches and then stops will cause the same error even if the rest of the training code looks correct.

That means you should verify:

  • batch count per epoch
  • whether the generator resets correctly
  • whether __len__ is correct for a Sequence
  • whether preprocessing filters out more samples than expected

Validation Data Can Trigger the Same Problem

Do not focus only on the training dataset. The validation pipeline can also run out of data when validation_steps is larger than the available validation batches.

So if training seems fine but the error appears near the validation phase, inspect the validation input pipeline with the same rigor.

A Quick Diagnostic Check

A practical first debugging step is to count batches manually.

python
1count = 0
2for _ in dataset:
3    count += 1
4print(count)

If the number of available batches is lower than the configured steps, you have found the mismatch directly.

Common Pitfalls

Specifying steps_per_epoch by habit even when the dataset length is already known is a common mistake.

Using .repeat() without understanding its effect can also hide data-accounting issues while changing the actual sampling pattern.

Another frequent problem is forgetting that filtering, sharding, or dropping remainder batches can reduce the effective dataset size.

Finally, generator-based pipelines often fail because they stop earlier than the training loop configuration expects.

Summary

  • the error means TensorFlow expected more batches than the input pipeline produced
  • the usual causes are incorrect steps_per_epoch, missing .repeat(), or a generator that ends too early
  • if the dataset is finite and well-defined, let TensorFlow infer the number of steps when possible
  • repeat the dataset only when an infinite or cycling stream is actually intended
  • check validation pipelines too, because they can trigger the same exhaustion error

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Practice ML system design

All Rights Reserved.