Tensorflow batch_size or steps is required for Tensor or NumPy input data
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The TensorFlow and Keras training APIs need a clear plan for how input data will be consumed. When you pass raw tensors or NumPy arrays and the framework cannot infer that plan, you can hit an error saying that batch_size or steps is required for tensor or NumPy input data.
Why the Error Happens
Model APIs such as fit, evaluate, and predict do not process an entire dataset as one giant block unless you explicitly make that choice. They work in batches, which means Keras needs to know one of two things:
- how many samples belong in each batch
- how many batches should be consumed
With a plain NumPy array or eager tensor, the usual fix is batch_size. With iterators, repeated datasets, or streaming pipelines, the usual fix is a step count such as steps_per_epoch or steps.
The goal is not to satisfy a random argument check. The framework genuinely needs to know how to break the work into manageable units.
Use batch_size for Arrays and Finite Tensors
For ordinary in-memory training data, specify batch_size directly.
This is the most common fix because arrays and eager tensors usually represent a finite dataset that you already have in memory.
The same idea applies to prediction:
If you omit batching in a code path that requires it, Keras cannot decide how to iterate.
Use steps for Repeated or Streaming Input
If you use tf.data.Dataset and repeat it forever, Keras no longer knows when an epoch should stop. In that case, a step count is the right answer.
Here the dataset is intentionally infinite because of .repeat(). Without steps_per_epoch, the training loop would not know when to end an epoch.
Prefer tf.data When Input Logic Gets Complex
If your input pipeline already needs shuffling, mapping, batching, prefetching, or file loading, move to tf.data instead of passing raw arrays through increasingly complex argument combinations.
Once the dataset is already batched and finite, you often do not need a separate batch_size argument.
batch_size and steps Solve Different Problems
A common misunderstanding is treating batch_size and steps as interchangeable. They are related, but they mean different things.
- '
batch_sizecontrols how many samples are processed per batch.' - '
stepscontrols how many batches are consumed.'
If you have 1000 samples and batch_size=32, then one epoch is roughly 32 steps. If you also set steps_per_epoch=10, you are telling Keras to stop early after only 10 batches.
That can be useful, but it should be deliberate.
Debugging Checklist
When this error appears, check these questions in order:
- Is the input a raw NumPy array or tensor rather than a dataset object?
- Did I forget to pass
batch_sizeforfit,evaluate, orpredict? - Did I call
.repeat()on a dataset without providingsteps_per_epoch? - Am I mixing a generator-style pipeline with array-style arguments?
In practice, one of those cases usually explains the failure.
Common Pitfalls
The biggest pitfall is assuming that Keras will always guess a batching strategy from the shape alone. That is not true for every API path.
Another issue is using steps_per_epoch with finite arrays when a simple batch_size would be clearer and easier to reason about.
People also forget that repeated datasets are intentionally endless. If you repeat a dataset, you usually owe Keras a step count.
Finally, avoid combining too many input styles at once. Arrays, generators, and tf.data pipelines all work, but they should not be mixed casually.
Summary
- Keras needs either a batch size or a batch count when it cannot infer how to iterate input data.
- Use
batch_sizefor ordinary in-memory tensors or NumPy arrays. - Use
stepsorsteps_per_epochfor repeated, streaming, or generator-like input. - '
tf.datais usually the cleanest approach for nontrivial input pipelines.' - Treat
batch_sizeandstepsas separate controls, not as interchangeable flags.

