TensorFlow
Dataset API
Oversampling
Machine Learning
Deep Learning

Oversampling functionality in Tensorflow dataset API

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

The TensorFlow Dataset API does not give you a one-line "make this dataset balanced" switch in the way some tabular preprocessing libraries do. But it does give you the building blocks to oversample minority examples by repeating, filtering, mixing, and resampling datasets.

That means the question is less "does tf.data support oversampling?" and more "which oversampling pattern fits my training pipeline?" For most deep learning pipelines, repeated sampling or weighted dataset mixing is the practical answer.

Why Oversampling Is Used

Oversampling increases the frequency of minority examples so the model sees them more often during training. This is useful when one class is heavily underrepresented and the model would otherwise learn to predict the majority class too easily.

In tf.data, the simplest idea is to create separate datasets for each class and then sample from them with balanced probabilities.

Split The Dataset By Class

Suppose each dataset element is a pair (features, label) and labels are binary. You can filter the dataset into class-specific streams:

python
1import tensorflow as tf
2
3features = tf.constant([[1.0], [2.0], [3.0], [4.0], [5.0]])
4labels = tf.constant([0, 0, 0, 0, 1])
5
6dataset = tf.data.Dataset.from_tensor_slices((features, labels))
7
8majority_ds = dataset.filter(lambda x, y: tf.equal(y, 0))
9minority_ds = dataset.filter(lambda x, y: tf.equal(y, 1))

From there, you can repeat the minority dataset so it never runs out during mixing:

python
minority_ds = minority_ds.repeat()
majority_ds = majority_ds.repeat()

Repeating is important because oversampling means the minority examples will be drawn multiple times.

Mix Datasets With Sampling Weights

One straightforward oversampling pattern is sample_from_datasets. Give both datasets the same sampling weight to create a roughly balanced stream.

python
1balanced_ds = tf.data.Dataset.sample_from_datasets(
2    [majority_ds, minority_ds],
3    weights=[0.5, 0.5]
4).batch(4)
5
6for batch_x, batch_y in balanced_ds.take(2):
7    print(batch_y.numpy())

Even though the original source was imbalanced, the sampled stream now surfaces the minority class much more often.

This is usually the cleanest answer when you want stochastic oversampling in the input pipeline itself.

Repeat And Concatenate For Simpler Cases

If you want a deterministic oversampling pattern rather than probabilistic mixing, you can repeat the minority dataset and concatenate it back:

python
1oversampled_minority = minority_ds.take(4)
2combined = majority_ds.take(4).concatenate(oversampled_minority).shuffle(20)
3
4for _, y in combined.batch(8).take(1):
5    print(y.numpy())

This is conceptually simple, but for ongoing training pipelines the sampled-mix approach is usually more flexible and easier to scale.

Know The Tradeoff With Duplicated Examples

Basic oversampling in tf.data usually means showing the same minority examples multiple times. That can help the optimizer pay attention to the minority class, but it can also increase overfitting if the rare examples are too few.

That is why oversampling is often combined with:

  • augmentation for image or signal data
  • shuffling
  • stronger regularization
  • careful validation metrics

For deep learning, duplicated sampling plus augmentation is often more practical than trying to reproduce classical synthetic methods such as SMOTE directly inside the TensorFlow input pipeline.

Compare With Class Weights

Oversampling is not the only fix for imbalance. Sometimes class weights are simpler. Instead of changing how often examples appear, class weighting changes how strongly the loss reacts to each class.

For Keras training:

python
model.fit(train_ds, epochs=5, class_weight={0: 1.0, 1: 4.0})

Class weights avoid duplicating samples, while oversampling changes the actual batch composition. Which one works better depends on the dataset and model behavior.

Common Pitfalls

One common mistake is oversampling only in the training pipeline but then judging the model with plain accuracy, which may still hide minority-class failure. Another is repeating the minority dataset without enough shuffling, which can produce highly repetitive batches. Developers also often forget that duplicated examples are not new information, so oversampling alone can increase memorization. Finally, tf.data oversampling patterns are usually about repeated sampling, not about generating synthetic new examples in the style of classical tabular oversampling methods.

Summary

  • 'tf.data supports oversampling through composition, even though it does not present it as one magic switch.'
  • A practical pattern is to split by class, repeat each subset, and mix them with sample_from_datasets.
  • Simple repetition is often enough, especially when paired with shuffling and augmentation.
  • Oversampling changes batch composition, while class weighting changes the loss contribution.
  • Evaluate with class-sensitive metrics so you can tell whether the oversampling actually improved minority performance.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the 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.