Keras / Tensorflow Predict Using tf.data.Dataset API
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
Using tf.data.Dataset for prediction is a good way to keep inference input handling aligned with the same pipeline style used for training. The main rule is simple: for model.predict, the dataset should usually yield features only, not (features, labels) pairs.
Build a Prediction Dataset
A common starting point is to build a dataset from NumPy arrays or tensors, then batch and prefetch it.
Batching matters because Keras models expect batch-shaped input during prediction just as they do during training.
Train a Small Model and Predict
That is the basic pattern: create the dataset, batch it, and pass it directly to predict.
Reuse a Labeled Dataset by Mapping Away the Labels
Sometimes the pipeline you already have yields (x, y) pairs because it was built for training or evaluation. Prediction should normally consume only the features.
This is often the cleanest way to reuse an existing pipeline without rebuilding it from scratch.
Keep IDs if Predictions Must Be Joined Back
Sometimes the output needs to be matched back to source records. In that case, include an identifier in the dataset and run inference with the model directly in a small loop.
This gives you tighter control than predict when ordering, IDs, or custom post-processing matter.
Why batch and prefetch Still Matter at Inference Time
People often think pipeline tuning matters only during training. In practice, inference pipelines benefit too.
- '
batchimproves throughput and keeps shapes predictable.' - '
prefetchoverlaps input preparation with model execution.' - stable ordering makes it easier to reconcile predictions with source rows.
For large prediction jobs, the input pipeline can become a meaningful part of the total runtime.
When to Call the Model Directly Instead of predict
model.predict is convenient when you just want batched outputs. Calling model(batch_x, training=False) directly is better when you need:
- custom output packaging,
- record IDs in the result,
- incremental writes,
- tighter control over the loop.
Both approaches are valid. The choice depends on how much control the inference pipeline needs.
Common Pitfalls
A common mistake is feeding a dataset that yields (x, y) pairs directly to predict and then being surprised by shape or structure issues. Prediction usually wants only x.
Another issue is forgetting to batch the dataset. That can hurt performance and also produce shapes that the model did not expect.
Teams also sometimes shuffle inference data and then struggle to match predictions back to the original records.
Summary
- '
tf.data.Datasetworks well as an input pipeline for Keras prediction.' - For
model.predict, the dataset should usually yield features only. - Batch and prefetch the dataset for reliable shapes and better throughput.
- Map
(x, y)datasets toxwhen reusing training-style pipelines for inference. - Include IDs or call the model directly when predictions must be joined back to source rows.
Related reading
- Keras / Tensorflow Weird dropout behaviour
- Keras accuracy does not change
- Keras and Error Setting an array element with a sequence
- Keras and TensorBoard - AttributeError 'Sequential' object has no attribute '_get_distribution_strategy
- Keras AttributeError 'list' object has no attribute 'ndim
- keras BatchNormalization axis clarification
- keras AssertionError Duplicate registrations for type 'experimentalOptimizer
- Keras AttributeError 'list' object has no attribute 'ndim
.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.