tensorflow
machine learning
deep learning
AI
data processing

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 message saying input ran out of data means training expected more batches than the dataset could produce. This is usually a mismatch between dataset cardinality, epoch configuration, and steps_per_epoch. The fix is to size steps from real batch counts or make dataset repetition explicit.

Core Sections

Understand the Failure Condition

Keras fit consumes batches until epoch target is reached. Failure occurs when:

  • dataset is finite
  • configured steps require more batches than available
  • dataset is not repeated

A common trigger is setting large steps_per_epoch by sample count guess instead of actual batch count.

Baseline Working Setup Without Explicit Steps

If dataset is finite and you do not need strict step count, omit steps_per_epoch.

python
1import tensorflow as tf
2
3x = tf.random.normal([100, 8])
4y = tf.random.uniform([100], maxval=2, dtype=tf.int32)
5
6ds = tf.data.Dataset.from_tensor_slices((x, y)).shuffle(100).batch(16)
7
8model = tf.keras.Sequential([
9    tf.keras.layers.Input(shape=(8,)),
10    tf.keras.layers.Dense(16, activation="relu"),
11    tf.keras.layers.Dense(2, activation="softmax"),
12])
13
14model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
15model.fit(ds, epochs=3)

Keras infers available batches from dataset cardinality.

When You Need steps_per_epoch

Use explicit steps only when required, for example infinite datasets or strict experiment comparability.

python
train_ds = ds.repeat()
model.fit(train_ds, epochs=5, steps_per_epoch=10)

Here repeat ensures enough data for configured steps.

Compute Step Count Correctly

If you want deterministic step count for finite data, compute from sample count and batch size.

python
1num_samples = 100
2batch_size = 16
3steps_per_epoch = num_samples // batch_size  # or math.ceil for remainder
4print(steps_per_epoch)

Choose floor or ceil intentionally based on whether you want partial final batch.

Check Dataset Cardinality in Debugging

Cardinality is a quick signal for finite versus infinite datasets.

python
card = tf.data.experimental.cardinality(ds)
print(card.numpy())

Special values can indicate unknown or infinite cardinality, which changes how you configure fit.

drop_remainder Interaction

When batching with drop_remainder=True, final partial batch is discarded. This reduces available batches and can cause unexpected shortfall.

python
ds_drop = tf.data.Dataset.from_tensor_slices((x, y)).batch(16, drop_remainder=True)
print(tf.data.experimental.cardinality(ds_drop).numpy())

Recalculate steps whenever batching options change.

Multi-Worker and Distributed Training

In distributed settings, effective per-worker batch behavior can differ because of sharding and drop rules. Verify cardinality and steps on the actual distributed input pipeline, not only on local prototype.

Validation Data Can Also Run Out

This error is not limited to training dataset. If validation dataset is finite and validation_steps is set too high, the same failure can occur during evaluation phase.

python
1val_ds = tf.data.Dataset.from_tensor_slices((x, y)).batch(20)
2model.fit(
3    ds,
4    epochs=2,
5    validation_data=val_ds,
6    validation_steps=3
7)

Keep validation step sizing aligned with validation cardinality, especially after changing batch size.

Practical Prevention Checklist

Before long runs:

  1. print one batch shape
  2. inspect cardinality
  3. verify batch size and drop remainder
  4. verify steps against expected available batches
  5. run one short epoch smoke test

This prevents wasting time on avoidable configuration errors.

Keep Step Math with Dataset Code

Store step-count calculations near dataset construction code rather than scattered in training configs. This reduces mismatch risk when batch size or sharding logic changes.

Common Pitfalls

  • Setting steps_per_epoch larger than available finite dataset batches.
  • Forgetting .repeat() when using explicit steps on finite input.
  • Changing batch settings and not updating step calculations.
  • Confusing sample count with batch count.
  • Ignoring cardinality checks before long training jobs.

Summary

  • Input-ran-out-of-data errors come from step and dataset mismatch.
  • Omit explicit steps for simple finite dataset training.
  • Use .repeat() when fixed steps are required.
  • Recompute step counts after batch or drop settings change.
  • Add a short smoke run to catch configuration mistakes early.

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.