5 Best Ways to Shuffle Preprocessed Data Using TensorFlow and Python

Rate this post

πŸ’‘ Problem Formulation: When working with machine learning models, it’s crucial to randomize the order of training data to avoid biases and improve generalization. This article addresses the challenge of shuffling preprocessed data using TensorFlow and Python. For instance, you might start with a dataset in a predictable sequence (e.g., sorted by labels) and want to shuffle it to a random order before training a model.

Method 1: Using tf.data.Dataset.shuffle()

This method shuffles records in the dataset using TensorFlow’s tf.data.Dataset.shuffle() function. It maintains a fixed-size buffer and randomly selects the next element from this buffer, replacing it with the next input element, providing a uniform random shuffle.

Here’s an example:

import tensorflow as tf

# Assume 'preprocessed_data' is your dataset.
dataset = tf.data.Dataset.from_tensor_slices(preprocessed_data)
shuffled_dataset = dataset.shuffle(buffer_size=10000)

for element in shuffled_dataset:

The output will be the elements of your dataset printed out in a random order.

This code snippet initializes a TensorFlow Dataset from preprocessed data, then applies the shuffle() transformation with a specified buffer size. The buffer size dictates how many elements to shuffle at a time, which should be greater than or equal to the full dataset size for optimal shuffling.

Method 2: Shuffling with a Seed

Shuffling with a seed ensures reproducible shuffling by setting a random sequence that’s repeatable in each run, using the seed parameter of the shuffle() function. This is critical for debugging and comparisons.

Here’s an example:

shuffled_dataset = dataset.shuffle(buffer_size=10000, seed=42)

for element in shuffled_dataset:

The output will resemble the previous method, but running the code again will generate the same shuffled order.

When setting the same seed value, the shuffle() function will produce the same shuffling outcome each time the code is executed, which aids in the reproducibility of experiments.

Method 3: Shuffling at Each Epoch

Shuffling at each epoch prevents the model from learning the order of the data by refreshing the shuffle after every epoch using the reshuffle_each_iteration argument.

Here’s an example:

shuffled_dataset = dataset.shuffle(

for element in shuffled_dataset:

The output will be different each epoch, providing a new shuffle order for every iteration over the dataset.

This snippet shows how to set up the shuffling to occur not just once but before each iteration/epoch, guaranteeing that the model never sees the data in the same order twice, thus enhancing the generalization ability.

Method 4: Shuffling with NumPy

Shuffling outside of TensorFlow’s pipeline using NumPy’s numpy.random.shuffle() can also be an effective method. This is particularly useful for smaller datasets that can fit into memory.

Here’s an example:

import numpy as np

numpy_data = preprocessed_data.numpy()

dataset = tf.data.Dataset.from_tensor_slices(numpy_data)

for element in dataset:

The output will show your data shuffled randomly.

After converting the data to a NumPy array, we use np.random.shuffle() to randomly shuffle the elements. The shuffled data is then converted back to a TensorFlow dataset. This operation is done in place and modifies the original data array.

Bonus One-Liner Method 5: Shuffle with tf.random.shuffle()

TensorFlow also provides a one-liner tf.random.shuffle() function to shuffle tensors along their first dimension in a simple and straightforward manner.

Here’s an example:

shuffled_data = tf.random.shuffle(preprocessed_data)


The output will be the entire tensor, shuffled along its first dimension.

Using tf.random.shuffle() is a quick and easy way to shuffle data without setting up a Dataset, suitable for tensors that are directly accessible and fits use cases with less complexity.


  • Method 1: TensorFlow Dataset’s Shuffle. Strong shuffling with a large buffer. May consume more memory.
  • Method 2: Shuffling with a Seed. Reproducible results for debugging. Less randomness across different runs.
  • Method 3: Shuffling at Each Epoch. Helps prevent model overfitting to the order of data. Might increase training time slightly.
  • Method 4: Shuffling with NumPy. Good for in-memory data. Breaks TensorFlow’s data pipeline optimizations.
  • Method 5: One-Liner Shuffle. Quick and easy but less flexible compared to Dataset.shuffle().