TensorFlow
sample_from_datasets
DirectedInterleave
exhausted input warning
Dataset sampling

Does TensorFlow's sample_from_datasets still sample from a Dataset when getting a DirectedInterleave selected an exhausted input warning?

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 exhausted-input warning from sample_from_datasets often sounds fatal, but in many cases sampling continues. The real behavior depends on source lengths and the stop_on_empty_dataset setting. If you do not control cardinality explicitly, epochs can drift and class balance can change mid-training.

What the Warning Means

tf.data.Dataset.sample_from_datasets chooses an input dataset for each element request. When one source runs out, TensorFlow warns that a selected input was exhausted. This warning indicates a state transition, not always a complete pipeline stop.

Default behavior often continues sampling from non-exhausted sources. If strict stop-on-empty mode is enabled, the combined stream can end as soon as the relevant condition is met.

python
1import tensorflow as tf
2
3left = tf.data.Dataset.from_tensor_slices([1, 2, 3])
4right = tf.data.Dataset.from_tensor_slices([10, 20, 30, 40, 50])
5
6mixed = tf.data.Dataset.sample_from_datasets(
7    [left, right],
8    weights=[0.5, 0.5],
9    stop_on_empty_dataset=False
10)
11
12for x in mixed.take(12):
13    print(int(x.numpy()))

In this setup, once left depletes, output can continue from right.

Control Epoch Size Explicitly

When dataset lengths differ, rely on repeat and take to define stable training windows.

python
1balanced = tf.data.Dataset.sample_from_datasets(
2    [left.repeat(), right.repeat()],
3    weights=[0.7, 0.3],
4    stop_on_empty_dataset=False
5).take(1000)

This avoids unpredictable step counts and keeps experiment comparisons fair.

A good rule is to make source streams effectively unbounded with repeat, then enforce deterministic epoch size with take.

Verify Effective Sampling Ratios

Configured weights are probabilistic, not exact for short streams. Always measure actual output mix.

python
1import collections
2import tensorflow as tf
3
4source_a = tf.data.Dataset.from_tensor_slices([0] * 10)
5source_b = tf.data.Dataset.from_tensor_slices([1] * 100)
6
7stream = tf.data.Dataset.sample_from_datasets([source_a, source_b], weights=[0.5, 0.5])
8counts = collections.Counter(int(v.numpy()) for v in stream.take(200))
9print(counts)

Run this check in debug mode after each major input-pipeline change.

Interaction with tf.data Transformations

Downstream transformations can alter effective class balance:

  • filter may drop more records from one source.
  • batch can mask uneven tail behavior.
  • shuffle can hide source transitions in small inspections.

So validate distribution after the full pipeline, not only right after sampling.

Troubleshooting Strategy

If warning frequency suddenly rises:

  1. Print source cardinality estimates.
  2. Confirm whether sources are repeated.
  3. Check if new filters shrink one source.
  4. Re-run a small deterministic sample test.

Cardinality inspection example:

python
print(tf.data.experimental.cardinality(left).numpy())
print(tf.data.experimental.cardinality(right).numpy())

A tiny reproducible script is usually faster than debugging inside a full training job.

When to Treat Warning as Serious

Treat the warning as expected if source depletion is intentional and downstream behavior stays stable. Treat it as a bug signal when:

  • Sampling was intended to remain balanced.
  • Epoch step count changes unexpectedly.
  • Minority class disappears after a pipeline update.

In these cases, rework cardinality control before trusting model metrics.

For class-balanced training, many teams use repeated class datasets, fixed take, and deterministic shuffling after sampling. This creates stable step counts and easier experiment comparison.

python
1train_ds = tf.data.Dataset.sample_from_datasets(
2    [class0.repeat(), class1.repeat()],
3    weights=[0.5, 0.5],
4    stop_on_empty_dataset=False
5).shuffle(2048).batch(64).prefetch(tf.data.AUTOTUNE).take(1000)

When warnings still appear in this pattern, check upstream filters or source loading failures before changing sampling weights. Keep this small validation script near training code so behavior stays visible during framework upgrades.

Common Pitfalls

  • Assuming exhausted-input warning always means total pipeline failure.
  • Relying on weights without controlling dataset lengths.
  • Forgetting to define deterministic epoch size with take.
  • Validating class ratios before filter and batch steps only.
  • Ignoring warning changes after data-ingestion code updates.

Summary

  • Exhausted-input warnings often indicate source depletion, not immediate stop.
  • Sampling continuation depends on stop_on_empty_dataset and stream design.
  • Use repeat plus take for consistent epoch behavior.
  • Measure actual sampled distribution instead of assuming configured weights.
  • Revalidate the full pipeline whenever source lengths or filters change.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
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