TensorFlow
TFRecords
CSV
Machine Learning
Data Preprocessing

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.

Practice ML system design

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.

python
1import tensorflow as tf
2
3
4def bytes_feature(value: str) -> tf.train.Feature:
5    return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value.encode("utf-8")]))
6
7
8def float_feature(value: float) -> tf.train.Feature:
9    return tf.train.Feature(float_list=tf.train.FloatList(value=[value]))
10
11
12def int64_feature(value: int) -> tf.train.Feature:
13    return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))

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:

python
1import csv
2import tensorflow as tf
3
4
5def make_example(row: dict) -> tf.train.Example:
6    features = {
7        "name": bytes_feature(row["name"]),
8        "age": int64_feature(int(row["age"])),
9        "score": float_feature(float(row["score"])),
10    }
11    return tf.train.Example(features=tf.train.Features(feature=features))
12
13
14with tf.io.TFRecordWriter("people.tfrecord") as writer:
15    with open("people.csv", newline="", encoding="utf-8") as f:
16        reader = csv.DictReader(f)
17        for row in reader:
18            example = make_example(row)
19            writer.write(example.SerializeToString())

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.

python
1import tensorflow as tf
2
3feature_spec = {
4    "name": tf.io.FixedLenFeature([], tf.string),
5    "age": tf.io.FixedLenFeature([], tf.int64),
6    "score": tf.io.FixedLenFeature([], tf.float32),
7}
8
9
10def parse_record(serialized):
11    return tf.io.parse_single_example(serialized, feature_spec)
12
13
14dataset = tf.data.TFRecordDataset(["people.tfrecord"]).map(parse_record)
15for item in dataset.take(2):
16    print(item)

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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Practice ML system design

All Rights Reserved.