Randomly sample from multiple tf.data.Datasets in Tensorflow
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
When you have several tf.data.Dataset objects and want to draw examples from them in random order, the right tool is usually tf.data.Dataset.sample_from_datasets. It mixes multiple input streams probabilistically, which is much better than simple concatenation when you need balanced sampling or controlled proportions.
Use sample_from_datasets for Probabilistic Mixing
The core pattern is straightforward:
This creates a new dataset that draws from dataset_a about 70 percent of the time and from dataset_b about 30 percent of the time.
That is the usual answer when the goal is "randomly sample from multiple datasets."
Why Not Just Concatenate and Shuffle?
Concatenating datasets and then shuffling is fine when all examples can be materialized together and you only care about random order. It is not the same as weighted sampling, especially when:
- the datasets have different sizes
- you want oversampling of rare classes
- the datasets are infinite or repeated streams
- you want a stable probability mix over time
sample_from_datasets operates at the dataset level, not only at the final merged element list.
A Common Class-Balancing Use Case
Suppose one dataset contains positive examples and another contains negative examples. If the raw data is imbalanced, you can use equal sampling weights to present a more balanced stream to the model.
Even though the negative dataset is larger, the resulting training stream can stay balanced because the sampling probabilities are controlled explicitly.
Repeat Behavior Matters
If one dataset is finite and the others repeat forever, the mixed pipeline may stop earlier than you expect unless you manage repetition carefully. In training pipelines, it is common to call .repeat() on all input datasets before sampling so that the combined stream does not end after one source is exhausted.
You should also think about whether exhaustion should stop sampling or whether other datasets should continue. The desired behavior depends on whether the inputs represent:
- training sources with replacement
- a fixed evaluation corpus
- class-specific streams that must stay aligned
Add Local Shuffle Before Sampling
Sampling chooses which dataset to draw from, but it does not automatically randomize the order inside each input stream. If each source has meaningful ordering, shuffle the sources too:
That gives you randomness at both levels:
- random source selection
- random element order inside each source
interleave Solves a Different Problem
interleave is often mentioned in the same discussions, but it serves a different purpose. It is good for reading from many files or input streams concurrently with structured cycling behavior. If you want probabilistic source selection, sample_from_datasets is usually the clearer API.
Common Pitfalls
- Concatenating and shuffling when you actually need weighted per-dataset sampling.
- Forgetting to
repeat()datasets in training and then wondering why the mixed pipeline ends early. - Assuming dataset-level sampling also shuffles item order inside each dataset.
- Using weights that do not reflect the actual training objective, which can create accidental bias.
- Reaching for
interleavewhen the requirement is random source choice rather than parallel stream reading.
Summary
- Use
tf.data.Dataset.sample_from_datasetsto randomly draw from multiple datasets. - Weights let you control the long-run sampling proportion from each source.
- Shuffle individual datasets if their internal order matters.
- Repeat datasets deliberately in training pipelines so the mixed stream behaves as intended.
- '
interleaveis useful for concurrent reading, but it is not the same as probabilistic dataset sampling.'
Related reading
- Rank error in tf.nn.dynamic_rnn
- RBM implementation with tensorflow
- Re-implementing TF 1.0 sampled_softmax_loss funtion for seq2seq model in to TF 2 Keras model
- Re-initialize variables in Tensorflow
- Randomness in Artificial Intelligence Machine Learning
- Rank mismatch Rank of labels received 2 should equal rank of logits minus 1 received 2
- Re-train a frozen .pb model in TensorFlow
- Read big train/validation/test datasets in tensorflow
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free 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.