Read in Large CSV File and feed into 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
If the CSV file is large, the wrong approach is to load the whole thing into pandas and then convert it into TensorFlow tensors. That works for small experiments but quickly becomes memory-bound and slow. The scalable pattern is to stream the data through tf.data, parse rows lazily, batch them, and prefetch so model training overlaps with input work.
The Goal: Stream Instead of Materialize Everything
A large CSV may be gigabytes in size. If you read it all into memory first, you pay for:
- the raw text file
- the parsed DataFrame
- the converted NumPy arrays
- the final TensorFlow tensors
That is unnecessary for training. TensorFlow is designed to consume data incrementally.
Use make_csv_dataset for Tabular Data
TensorFlow provides a high-level helper for CSV input pipelines.
This reads the file in a streaming fashion, parses columns automatically, and yields batches ready for model training.
For many tabular training jobs, this is the simplest correct answer.
Control Column Types Explicitly When Needed
If type inference is not reliable, declare the CSV schema yourself.
This avoids parsing surprises and makes the training pipeline more stable.
Lower-Level Control with TextLineDataset
If the file format is unusual or you need custom parsing, build the pipeline yourself.
This is more verbose, but it gives full control over parsing, feature engineering, and error handling.
Batch, Shuffle, and Prefetch Correctly
The input pipeline matters almost as much as the model for training throughput.
A typical order is:
- read the file
- parse rows
- shuffle
- batch
- prefetch
Example:
prefetch allows TensorFlow to prepare the next batch while the current batch is being used by the model.
Feeding the Dataset into a Model
Once the pipeline yields batches of features and labels, use it directly in model.fit.
This avoids the need to first convert the whole CSV into one giant tensor.
When Pandas Still Makes Sense
Pandas is still useful for:
- inspecting a sample of the file
- understanding column names and missing values
- prototyping transformations on a small subset
For example:
But pandas should not be the default ingestion path for truly large training data unless you have already confirmed the whole dataset fits comfortably in memory.
Consider TFRecord for Repeated Training
CSV is easy to inspect but inefficient for repeated large-scale training. If the same dataset is used many times, preprocessing once into TFRecord can make the pipeline faster and more consistent. CSV is fine to start with, but it is not always the best long-term storage format for TensorFlow training.
That is a performance optimization, not a requirement. The main first step is to stop materializing giant CSVs into memory.
Common Pitfalls
The biggest mistake is reading a very large CSV fully into pandas and only then trying to hand it to TensorFlow. Another is skipping batching and prefetching, which starves the model during training. Developers also often rely on automatic type inference and then get unstable parsing behavior when the CSV contains mixed or missing values. Finally, building a tf.data pipeline but still converting everything to NumPy first defeats most of the benefit.
Summary
- For large CSV files, stream data through
tf.datainstead of loading everything eagerly. - '
tf.data.experimental.make_csv_datasetis the simplest high-level pipeline for many tabular cases.' - Use
TextLineDatasetplusdecode_csvwhen you need custom parsing. - Batch, shuffle, and prefetch to keep training throughput healthy.
- Use pandas only for inspection or small experiments, not as the default ingestion path for very large training data.
Related reading
- Read mixed data types from CSV row via tf.TextLineReader and tf.decode_csv
- Read mnist images into Tensorflow
- Read only mode in keras
- Reading data from bucket in Google ml-engine tensorflow
- Real world examples of Machine Learning?
- Reason of having high AUC and low accuracy in a balanced dataset
- Read .mat files in Python
- Read only the first line of a file?
.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.