How to use Tensorflow dataset API with training and validation sets
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
tf.data is the standard way to build efficient input pipelines in TensorFlow. It helps you scale from tiny local experiments to large training jobs without rewriting your data logic. This guide shows how to build training and validation datasets correctly and wire them into model.fit.
Build a Dataset from In-Memory Arrays
A good starting point is from_tensor_slices, then split into train and validation sets using deterministic slicing.
Using reshuffle_each_iteration=False preserves a stable split across runs. That is useful when comparing experiments.
Add Preprocessing with map
Most real projects need preprocessing. Keep it in your dataset pipeline so training and evaluation stay consistent.
For image data, this is where you decode files, resize images, and optionally apply augmentations only on training data.
Train with Separate Training and Validation Datasets
Once datasets are prepared, use validation_data in model.fit.
Validation metrics now represent generalization on held-out data, not performance on the training subset.
Pipeline Performance Techniques
tf.data performance usually depends on order and buffering. A common pattern is:
shufflemapbatchprefetch
Add cache if data fits memory or if preprocessing is expensive and deterministic.
If your dataset comes from files, consider interleave for parallel reads. For distributed training, ensure each worker sees a suitable shard.
Working with Validation Split from Keras Utilities
For image folders, tf.keras.utils.image_dataset_from_directory supports validation split directly.
The shared seed and matching split settings are essential to avoid overlap.
Validate Dataset Cardinality and Epoch Behavior
Input bugs often come from unknown dataset size or accidental infinite repetition. For bounded datasets, inspect cardinality so you can reason about steps_per_epoch and validation coverage.
If you call repeat() on training data, either leave steps_per_epoch explicit or remove repeat for small local experiments. Otherwise, training may never end as expected. Keep validation finite so each epoch reports comparable metrics.
Common Pitfalls
- Applying random augmentations to validation data. Validation should reflect real inference conditions.
- Reshuffling differently while splitting. If split logic is inconsistent, train and validation samples can overlap.
- Forgetting
prefetch, causing the model to wait on input pipeline work. - Using tiny shuffle buffers. Small buffers reduce randomness and may bias batches.
- Caching before random transforms when you intended fresh augmentation each epoch.
Summary
- Use
takeandskipor directory split utilities to build explicit train and validation datasets. - Keep preprocessing in
tf.datapipelines for consistency and reproducibility. - Feed validation data through
validation_datainmodel.fit. - Optimize pipelines with
map,batch,cache, andprefetchin sensible order. - Protect split integrity so your validation metrics remain trustworthy.
Related reading
- How to use tensorflow debugging tool tfdbg on tf.estimator in Tensorflow?
- How to use tensorflow feature_columns as input to a keras model
- How to use TensorFlow in OOP style?
- How to use TensorFlow metrics in Keras
- How to use tensorflow on spyder?
- How to use Tensorflow Optimizer without recomputing activations in reinforcement learning program that returns control after each iteration?
- How to use tfa.seq2seq.BahdanauAttention with tf.keras functional API?
- How to use ThreeTenABP in Android Project

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the 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.