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.
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:
From there, you can repeat the minority dataset so it never runs out during mixing:
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.
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:
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:
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.datasupports 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
- Padding time-series subsequences for LSTM-RNN training
- padding'same' conversion to PyTorch padding
- Parallel fitting of multiple Keras Models on single GPU
- Parallel processes in distributed tensorflow
- Parallel threads with TensorFlow Dataset API and flat_map
- parallelising tf.data.Dataset.from_generator
- Package ‘neuralnet’ in R, rectified linear unit ReLU activation function?
- PacMan what kinds of heuristics are mainly used?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.