The recommended pipeline order
The power of tf.data comes from chaining transformations. Each one returns a new Dataset (lazy, not yet executed). The order you apply them matters — getting it wrong silently destroys randomization or wastes memory.
| Step | Transformation | Why this order |
|---|---|---|
| 1 | .map(parse, num_parallel_calls=AUTOTUNE) | Decode/parse raw data |
| 2 | .cache() | Cache after expensive parse, before randomization |
| 3 | .shuffle(buffer) | Per-epoch reshuffling, before batching |
| 4 | .batch(size, drop_remainder=True) | Group into batches |
| 5 | .map(augment, num_parallel_calls=AUTOTUNE) | Augment AFTER batching (faster batched ops) |
| 6 | .prefetch(AUTOTUNE) | Always last — overlaps CPU/GPU |
Critical rule: shuffle BEFORE batch. Shuffling after batch only randomizes batches, not individual elements — samples within each batch stay clumped together.