Tensorflow create a tfrecords file from csv
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
To convert CSV data into a TFRecord file, you read each row, turn it into a tf.train.Example, serialize that example, and write it with tf.io.TFRecordWriter. The main design decision is how each CSV column maps to TensorFlow feature types such as int64, float, or bytes.
TFRecord is useful because it gives TensorFlow a compact binary input format that works well with tf.data pipelines, especially when datasets get large.
Define Helper Functions for Features
A tf.train.Example stores named features, and each feature must be encoded with the right protobuf wrapper.
These helpers keep the row-conversion logic readable.
Convert CSV Rows into Serialized Examples
Suppose a CSV file has columns name, age, and score. You can map each row like this:
This is the core conversion workflow.
Read the TFRecord Back to Verify It
Do not stop after writing the file. Verify the schema by parsing it back.
This catches mismatches early, such as storing a numeric column as bytes accidentally.
When CSV Is Not the Final Training Format
TFRecord is most useful when:
- the dataset is large
- you want efficient streaming during training
- examples have a stable schema
- you plan to reuse the binary dataset multiple times
If your CSV is tiny and used once for quick experimentation, converting to TFRecord may be unnecessary. The value appears once repeated training or distributed input pipelines matter.
Think About Missing Values and Lists
Real CSV files are usually messier than examples. Decide in advance how to handle:
- missing numeric values
- empty strings
- categorical columns
- variable-length list data flattened into CSV text
The conversion should encode those choices consistently, because the parsing code later depends on them.
For repeated pipelines, it is also worth versioning the schema in documentation or code comments. TFRecord is efficient, but once the binary files exist, it is much harder to inspect them casually than it is with CSV, so explicit schema discipline pays off.
Common Pitfalls
- Writing every CSV column as bytes because it feels easiest, then dealing with awkward parsing later.
- Forgetting to cast string values from CSV into the correct numeric Python types before building features.
- Creating TFRecords without writing matching parsing logic to verify the schema.
- Ignoring missing values until the writer crashes or produces inconsistent examples.
- Converting tiny one-off datasets to TFRecord when a direct CSV pipeline would have been simpler.
Summary
- Convert CSV to TFRecord by mapping each row to a serialized
tf.train.Example. - Use feature helper functions to keep type handling explicit and correct.
- Verify the output by reading the TFRecord back with a matching parse spec.
- TFRecord is most useful for repeated training and efficient TensorFlow input pipelines.
- Good schema decisions during conversion prevent painful parsing bugs later.
Related reading
- TensorFlow create dataset from numpy array
- TensorFlow create dataset from numpy array
- Tensorflow create minibatch from numpy array 2 GB
- Tensorflow create tf.NodeDef and set attributes
- Tensorflow Creating a graph in a class and running it outside
- Tensorflow Cross Device Communication
- tensorflow creating mask of varied lengths
- Tensorflow Data API - prefetch
.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.