TensorFlow
Data Reading
Machine Learning
Python
Data Processing

How to read data into Tensorflow?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Reading data into TensorFlow is really about building an input pipeline the model can consume efficiently. For small experiments, NumPy arrays may be enough, but for real training jobs tf.data is usually the right tool because it can stream, parse, batch, shuffle, and prefetch data predictably.

Start with from_tensor_slices for in-memory data

If your data already fits comfortably in memory, the easiest entry point is tf.data.Dataset.from_tensor_slices.

python
1import tensorflow as tf
2import numpy as np
3
4features = np.array([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]], dtype=np.float32)
5labels = np.array([0, 1, 0], dtype=np.int32)
6
7dataset = tf.data.Dataset.from_tensor_slices((features, labels))
8
9for x, y in dataset:
10    print(x.numpy(), y.numpy())

This creates one dataset element per training example. To make it training-friendly, add common transformations:

python
dataset = dataset.shuffle(100).batch(2).prefetch(tf.data.AUTOTUNE)

That is enough for many small and medium-size projects.

Read text and CSV data with TextLineDataset

If the input lives in text files or CSV files, TextLineDataset is a common starting point.

python
1import tensorflow as tf
2
3lines = tf.data.TextLineDataset("data.csv")
4
5for line in lines.take(3):
6    print(line.numpy())

To turn each row into tensors, map a parser function over the dataset:

python
1import tensorflow as tf
2
3def parse_csv(line):
4    defaults = [0.0, 0.0, 0]
5    x1, x2, label = tf.io.decode_csv(line, record_defaults=defaults)
6    features = tf.stack([x1, x2])
7    return features, label
8
9dataset = (
10    tf.data.TextLineDataset("data.csv")
11    .skip(1)
12    .map(parse_csv, num_parallel_calls=tf.data.AUTOTUNE)
13    .batch(32)
14    .prefetch(tf.data.AUTOTUNE)
15)

This is a good pattern when you want readable parsing logic without loading the entire file at once.

Use TFRecord for larger TensorFlow workflows

TFRecord is a common TensorFlow-native format for large datasets because it works well with tf.data and avoids some of the overhead of ad hoc text parsing.

python
1import tensorflow as tf
2
3raw_dataset = tf.data.TFRecordDataset("train.tfrecord")
4
5feature_spec = {
6    "x1": tf.io.FixedLenFeature([], tf.float32),
7    "x2": tf.io.FixedLenFeature([], tf.float32),
8    "label": tf.io.FixedLenFeature([], tf.int64),
9}
10
11def parse_example(serialized):
12    example = tf.io.parse_single_example(serialized, feature_spec)
13    features = tf.stack([example["x1"], example["x2"]])
14    label = example["label"]
15    return features, label
16
17dataset = (
18    raw_dataset
19    .map(parse_example, num_parallel_calls=tf.data.AUTOTUNE)
20    .batch(32)
21    .prefetch(tf.data.AUTOTUNE)
22)

If you are training repeatedly on large datasets, TFRecord is often a strong format choice.

The core tf.data transformations

A useful TensorFlow input pipeline is usually built from a few standard steps:

  • 'map for parsing or preprocessing'
  • 'shuffle for randomization'
  • 'batch for grouped examples'
  • 'cache when repeated reads are expensive and data size allows it'
  • 'prefetch to overlap input work with model execution'

Typical structure:

python
1dataset = (
2    dataset
3    .shuffle(10000)
4    .batch(64)
5    .prefetch(tf.data.AUTOTUNE)
6)

For many training jobs, these steps matter more than the exact file format because they determine whether the model waits around for data.

Feed the dataset into Keras

Once the pipeline is ready, pass it straight to model.fit.

python
1model = tf.keras.Sequential([
2    tf.keras.layers.Dense(8, activation="relu"),
3    tf.keras.layers.Dense(2)
4])
5
6model.compile(
7    optimizer="adam",
8    loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
9    metrics=["accuracy"]
10)
11
12model.fit(dataset, epochs=3)

This is the normal bridge from TensorFlow input pipelines to model training.

Arrays are still fine for simple cases

Not every experiment needs a full pipeline. If the dataset is small and already in arrays, this is perfectly valid:

python
model.fit(features, labels, batch_size=32, epochs=3)

The point is not to force tf.data everywhere. Use it when you need streaming, preprocessing composition, scalable file input, or better input performance.

Common Pitfalls

The biggest mistake is treating file reading as the whole job. Reading bytes or lines is only the first step; the model still needs tensors with the right shapes and dtypes.

Another issue is building a dataset pipeline and then batching again somewhere else in a conflicting way. Be clear about where batching lives.

Developers also underestimate prefetch and parallel map. On larger jobs, these can have a big effect because input stalls waste accelerator time.

Finally, use the simplest source that fits the problem. Arrays, CSV pipelines, and TFRecord pipelines all have legitimate use cases; there is no single universal input format.

Summary

  • Use tf.data for most nontrivial TensorFlow input pipelines.
  • 'from_tensor_slices is the easiest option when data already fits in memory.'
  • 'TextLineDataset works well for text and CSV inputs.'
  • TFRecord is a common scalable format for larger TensorFlow workflows.
  • Add parsing, shuffling, batching, and prefetching based on the real needs of the training job.

Course illustration
Course illustration

All Rights Reserved.