Read big train/validation/test datasets in tensorflow
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
Large TensorFlow datasets should usually be streamed from disk instead of loaded fully into memory. The standard pattern is to keep train, validation, and test splits as separate files or shards, then build a dedicated tf.data pipeline for each split. That gives you predictable evaluation behavior and much better memory usage.
Split the Data on Disk First
For large projects, do not read one giant file into Python and then slice it into train, validation, and test arrays. Keep the splits separate on disk.
This makes the split reproducible and lets each pipeline behave differently. Training can shuffle aggressively. Validation and test can remain deterministic.
TFRecord Is a Good Default Format
TensorFlow works especially well with TFRecord because it streams efficiently and integrates cleanly with tf.data.
In real projects, use multiple shard files instead of one huge file so TensorFlow can interleave reads more effectively.
Build One Parser and Reuse It
Once the files exist, define one parsing function and reuse it across train, validation, and test pipelines.
This pattern scales much better than wrapping large NumPy arrays with from_tensor_slices.
Treat the Three Splits Differently
The pipelines should not be identical.
- training data usually needs shuffling and sometimes augmentation
- validation data should be stable and repeatable
- test data should be deterministic and untouched by training randomness
That is why separate pipelines are worth the effort. Reusing one “do everything” pipeline across all splits often introduces evaluation mistakes.
Non-TFRecord Sources Still Fit the Same Pattern
If the raw data is CSV, text, or images, the same idea still applies: stream from files and transform lazily.
TFRecord is not mandatory, but for large TensorFlow-native training workloads it is often the most scalable choice.
Feed the Pipelines Directly Into Training
tf.data.Dataset objects plug directly into Keras.
That keeps the entire workflow streaming and avoids unnecessary in-memory copies.
Common Pitfalls
- Loading huge datasets into NumPy first and then running out of RAM.
- Using one shuffled pipeline for validation and test instead of keeping evaluation deterministic.
- Writing too few shard files and limiting I/O throughput.
- Mixing train, validation, and test logic into one pipeline that is hard to reason about.
- Forgetting that preprocessing order affects performance just as much as the model does.
Summary
- Keep large train, validation, and test splits separate on disk.
- Use
tf.datato stream data instead of loading everything into memory. - TFRecord is a strong default for large TensorFlow workloads.
- Give training and evaluation different pipeline behavior on purpose.
- Feed
tf.data.Datasetobjects directly intomodel.fitandmodel.evaluatefor scalable training.
Related reading
- Recurrentshop and Keras multi-dimensional `RNN` results in a dimensions mismatch error
- reduce size of pretrained deep learning model for feature generation
- Reducing input dimensions for a deep learning model
- regarding the decoder layer definition in autoencoder model under Keras framework
- Read in Large CSV File and feed into TensorFlow
- Read mixed data types from CSV row via tf.TextLineReader and tf.decode_csv
- Read csv files in a MLFlow pipeline
- Read mnist images into Tensorflow
.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.