TFRecords
data shuffling
machine learning
TensorFlow
data preprocessing

TFRecords and record shuffling

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 TFRecords

TFRecords is a simple record-oriented binary format that is widely used in the TensorFlow ecosystem for storing large datasets. The format provides significant advantages, especially when working with massive datasets and harnessing the full capabilities of cloud-based data processing. TFRecords are especially useful for training deep learning models, allowing efficient reading and shuffling of large datasets directly from disk.

What is TFRecord?

TFRecord is a file format that contains a sequence of records, with each record being a series of binary strings. A TFRecord file is a container for several such serialized `tf.train.Example` objects. Each `Example` contains Features that can be a string, float, or int data type.

Structure of a TFRecord File

A TFRecord file consists of sequences of serialized data structures, primarily characterized by the following:

  1. Header:
    • Consists of a 12 byte size header with a CRC (Cyclic Redundancy Check) entry.
  2. Serialized String:
    • The main content that contains the serialized version of the data entry.
  3. Footer:
    • Consists of a CRC checksum to ensure data integrity.

Why Use TFRecords?

  • Efficiency: Binary data can often be read faster relative to text-based data like CSV or JSON.
  • Scalability: TFRecords are especially useful for handling large datasets that do not fit into memory.
  • Integration with TensorFlow: Provides seamless integration with TensorFlow's ecosystem, including support for distributed training and performance optimization techniques such as threading, prefetching, and caching.

Creating TFRecords

To create a TFRecord file, you typically transform raw data into `tf.train.Example` objects and serialize them.

  • Parallel Reading: Combine shuffling with parallel data loading and prefetching to achieve optimal performance.
  • Prefetching: Use `prefetch` to overlap the preprocessing and model execution of a training step.
  • Autotune: Use `AUTOTUNE` to select optimal values for parameters like prefetch and parallel processing.

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.