TensorFlow
next_batch
data processing
machine learning
data pipeline

how to implement tensorflow's next_batch for own data

Master System Design with Codemia

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

Introduction

Older TensorFlow examples often use a next_batch helper that returns small slices of training data. For your own dataset, the essential job is to batch samples, optionally shuffle between epochs, and keep labels aligned with features. You can implement this manually for simple cases, but tf.data is the better long-term approach.

Implement a Minimal Python Batch Iterator

If you want the old-style behavior directly, create a small iterator object.

python
1import numpy as np
2
3
4class BatchLoader:
5    def __init__(self, features, labels, shuffle=True):
6        self.features = np.asarray(features)
7        self.labels = np.asarray(labels)
8        self.shuffle = shuffle
9        self.index = 0
10        self.order = np.arange(len(self.features))
11        if self.shuffle:
12            np.random.shuffle(self.order)
13
14    def next_batch(self, batch_size):
15        if self.index + batch_size > len(self.features):
16            self.index = 0
17            if self.shuffle:
18                np.random.shuffle(self.order)
19
20        idx = self.order[self.index:self.index + batch_size]
21        self.index += batch_size
22        return self.features[idx], self.labels[idx]
23
24
25x = np.arange(20).reshape(10, 2)
26y = np.arange(10)
27loader = BatchLoader(x, y)
28
29xb, yb = loader.next_batch(3)
30print(xb)
31print(yb)

This matches the spirit of next_batch and is easy to reason about.

Handle Epoch Boundaries Correctly

The tricky part is what happens when you hit the end of the dataset. Common choices are:

  • wrap and reshuffle immediately
  • return the remaining items and start a new epoch on the next call
  • drop incomplete final batch

The right choice depends on the training loop, but you should define it explicitly rather than relying on accidental slicing behavior.

Keep Features and Labels Together

Batching bugs often come from shuffling features and labels separately. Always shuffle by index so they stay aligned.

Wrong idea:

  • shuffle x
  • shuffle y

Correct idea:

  • build one permutation
  • apply it to both arrays

The iterator above does exactly that.

Prefer tf.data for Real Training Pipelines

For production TensorFlow code, tf.data.Dataset is cleaner and faster.

python
1import tensorflow as tf
2import numpy as np
3
4x = np.arange(20).reshape(10, 2).astype("float32")
5y = np.arange(10).astype("int32")
6
7dataset = tf.data.Dataset.from_tensor_slices((x, y))
8dataset = dataset.shuffle(buffer_size=len(x)).repeat().batch(3)
9
10for xb, yb in dataset.take(2):
11    print(xb.numpy())
12    print(yb.numpy())

This gives the same conceptual result as next_batch, but with better integration into TensorFlow training loops.

Add Prefetch for Throughput

Once batching works, prefetch is usually the next improvement.

python
dataset = dataset.prefetch(tf.data.AUTOTUNE)

That allows input preparation to overlap with model execution, which is especially helpful for GPU workloads.

Separate Train and Evaluation Behavior

Training batches usually shuffle and repeat forever. Evaluation batches usually do not shuffle and often stop after one pass.

Example:

python
train_ds = tf.data.Dataset.from_tensor_slices((x, y)).shuffle(len(x)).repeat().batch(4)
eval_ds = tf.data.Dataset.from_tensor_slices((x, y)).batch(4)

Keeping those behaviors separate prevents evaluation instability and confusing metrics.

Debugging Batch Problems

If batches look wrong, inspect:

  1. shapes of features and labels
  2. whether shuffling keeps alignment
  3. what happens at end of epoch
  4. whether the final batch size is fixed or variable

Many training bugs blamed on the model are really data-loader bugs.

Also log one sample batch early in development so shape and alignment mistakes are obvious before long training runs start.

Common Pitfalls

  • Shuffling features and labels independently.
  • Forgetting to define behavior at epoch boundaries.
  • Rebuilding batches with Python loops when tf.data would be simpler.
  • Using the same batching rules for training and evaluation.
  • Ignoring partial-batch behavior and getting inconsistent shapes.

Summary

  • A next_batch helper only needs indexing, alignment, and epoch management.
  • Manual iterators are fine for small experiments and debugging.
  • 'tf.data is the preferred implementation for modern TensorFlow pipelines.'
  • Keep training and evaluation batching behavior separate.
  • Debug data shapes and label alignment before blaming model code.

Course illustration
Course illustration

All Rights Reserved.