Tensorflow
Data Loading
Machine Learning
Large Datasets
Data Processing

Tensorflow Modern way to load large data

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

Modern TensorFlow data loading for large datasets is centered on tf.data, not manual in-memory arrays. The goal is to stream, parse, batch, and prefetch data efficiently so accelerators stay busy without memory blowups. A strong pipeline is explicit, measurable, and easy to tune under real workload pressure.

Core Sections

Start with File-Based Streaming

Instead of loading all records into RAM, stream from files through dataset primitives.

python
1import tensorflow as tf
2
3files = tf.data.Dataset.list_files("data/train-*.tfrecord", shuffle=True)
4raw = files.interleave(
5    tf.data.TFRecordDataset,
6    cycle_length=8,
7    num_parallel_calls=tf.data.AUTOTUNE
8)

interleave improves throughput when data is split across many files.

Parse Records with a Deterministic Schema

Define parse logic in one function and keep feature schema centralized.

python
1def parse_example(serialized):
2    spec = {
3        "features": tf.io.FixedLenFeature([128], tf.float32),
4        "label": tf.io.FixedLenFeature([], tf.int64),
5    }
6    parsed = tf.io.parse_single_example(serialized, spec)
7    return parsed["features"], parsed["label"]
8
9parsed_ds = raw.map(parse_example, num_parallel_calls=tf.data.AUTOTUNE)

Centralized schema reduces silent mismatches across training and evaluation jobs.

Build the Input Pipeline Stages

A practical pipeline order for training:

  • shuffle
  • repeat if needed
  • batch
  • prefetch
python
1train_ds = (
2    parsed_ds
3    .shuffle(20000)
4    .batch(256, drop_remainder=True)
5    .prefetch(tf.data.AUTOTUNE)
6)

Tune shuffle buffer and batch size using measured latency and memory constraints.

Use Caching Carefully

cache can speed epochs when data fits memory or local SSD. For huge datasets, caching everything in memory is often not feasible.

python
fast_ds = parsed_ds.cache("/tmp/tf_cache")

Use file cache for medium datasets or repeated experiments on the same host.

Scale with Sharding and Parallel Readers

In multi-worker training, shard dataset to avoid duplicate example consumption.

python
options = tf.data.Options()
options.experimental_distribute.auto_shard_policy = tf.data.experimental.AutoShardPolicy.DATA
train_ds = train_ds.with_options(options)

This ensures each worker processes different slices when distribution strategy is active.

Alternative Source: Generator for Custom Formats

If your source format is not TFRecord, wrap a Python generator and then convert to tf.data pipeline stages.

python
1import numpy as np
2
3def generator():
4    for _ in range(10000):
5        x = np.random.randn(128).astype("float32")
6        y = np.random.randint(0, 2, dtype="int64")
7        yield x, y
8
9sig = (
10    tf.TensorSpec(shape=(128,), dtype=tf.float32),
11    tf.TensorSpec(shape=(), dtype=tf.int64)
12)
13
14ds = tf.data.Dataset.from_generator(generator, output_signature=sig)
15ds = ds.batch(128).prefetch(tf.data.AUTOTUNE)

This is flexible but generally slower than native TensorFlow readers for high-throughput production runs.

Monitoring Pipeline Health

If GPU usage is low, the model might be waiting on input. Measure step time and input pipeline latency separately.

Useful checks:

  • batch production time
  • host CPU utilization
  • storage throughput
  • queue prefetch depth

Optimize input first before increasing model complexity.

tf.data Service for Large Clusters

For large distributed environments, tf.data service can offload expensive input work from trainers. This helps when parsing and augmentation become bottlenecks.

Adopt this after baseline local pipeline is already stable and measurable.

Keep Pipeline and Model Versioned Together

When data parsing logic changes, version pipeline code together with model artifacts. This prevents hard-to-debug cases where a restored model receives input tensors shaped by a newer parser than the one used during training.

Common Pitfalls

  • Loading full datasets into memory and causing host memory pressure.
  • Parsing records in Python loops instead of TensorFlow dataset operations.
  • Overusing cache on datasets that do not fit available storage.
  • Forgetting dataset sharding in distributed training and duplicating samples.
  • Tuning model only while ignoring underperforming input pipeline throughput.

Summary

  • tf.data is the modern foundation for large-scale TensorFlow input pipelines.
  • Stream from files, parse with explicit schemas, then batch and prefetch.
  • Tune shuffle, batch, and cache settings based on measured system behavior.
  • Use sharding and distributed options for multi-worker correctness.
  • Monitor pipeline health continuously to keep training hardware utilized.

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.