How to load batches of CSV files using tf.data and map
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
When your training data is spread across many CSV files, the goal is usually the same: stream rows efficiently, parse them into tensors, batch them, and feed the model without loading everything into memory. TensorFlow's tf.data API is built for exactly that workflow.
The core pattern is list_files, then interleave or flat_map to read each file, then map to parse each line, followed by batch and prefetch. Once you understand that pipeline, CSV input becomes predictable and scalable.
Build a File-Based Input Pipeline
Suppose you have many files matching data/train-*.csv, each with a header and rows shaped like feature1,feature2,label.
Start by listing files:
files is a dataset of file paths, not CSV rows. Next, convert each path into a line dataset and combine them:
Using skip(1) removes the header line from every file. interleave reads from several files concurrently, which often improves throughput.
Parse Each CSV Row with map
Now define a parser. tf.io.decode_csv turns a comma-separated line into typed tensors.
Map the parser across the dataset:
At this point, each element is a pair of tensors shaped like (features, label).
Batch, Shuffle, and Prefetch
After parsing, add the training-oriented transformations:
That gives you batches ready for model training:
A complete training example looks like this:
When make_csv_dataset Is Simpler
If your CSV structure is conventional and you want TensorFlow to handle more of the parsing, tf.data.experimental.make_csv_dataset can be easier:
This returns batches of feature dictionaries plus labels. It is convenient, but the manual TextLineDataset plus map approach gives you finer control and teaches the underlying mechanics.
Why map Matters
map is where feature engineering usually lives. You can normalize columns, cast dtypes, derive labels, or combine fields before the batch reaches the model.
This keeps preprocessing close to the data pipeline and lets TensorFlow parallelize it.
Common Pitfalls
- Calling
batchbefore parsing lines usually makes the parser logic harder because each element becomes a batch of strings. - Forgetting
skip(1)when files contain headers causes CSV parsing failures. - Using the wrong
record_defaultstypes can silently coerce data incorrectly or fail at runtime. - Shuffling with too small a buffer reduces randomness, especially across many files.
- Building the dataset without
prefetchcan leave the model waiting on input instead of training continuously.
Summary
- Use
Dataset.list_filesto discover many CSV files. - Use
interleaveandTextLineDatasetto stream lines from multiple files efficiently. - Parse each line with
mapandtf.io.decode_csv. - Add
shuffle,batch, andprefetchfor training performance. - Reach for
make_csv_datasetwhen you want a higher-level CSV loader with less manual control.
Related reading
- How to load Image Masks Labels for Image Segmentation in Keras
- How to load new parts of Dataset dynamically during training of an Estimator?
- How to load only specific weights on Keras
- How to load only specific weights on Keras
- How to load sparse data with TensorFlow?
- How to load TF hub model from local system
- How to load tfjs model into python using keras/tensorflow
- How to load_weights to a Keras model from a Tensorflow checkpoint
.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.