Repeat finite training data
Training for a fixed number of epochs over an in-memory or file-backed input pipeline.
TensorFlow · Module
tf.data.Dataset.repeat repeats a dataset’s elements for a specified number of traversals. With no count or -1, it produces an indefinitely repeating dataset.
repeat returns a dataset that traverses the original dataset multiple times. Pass a nonnegative count for a finite result, or omit it (equivalent to -1) to continue indefinitely; the optional name labels the operation.
repeat(count=None, name=None) -> 'DatasetV2'| Name | Type | Description |
|---|---|---|
count | tf.int64 scalar Tensor | Optional scalar tf.int64 tensor specifying how many times to repeat the dataset; None or -1 means indefinitely. |
name | string | Optional name for the TensorFlow data operation. |
A DatasetV2 containing the repeated elements.
count may be supplied as a scalar tf.int64 tensor rather than only as a Python integer.from_tensor_slices.count unset or using -1 creates an infinite dataset, so use a terminating consumer or a finite count.shuffle, can produce different elements on different repetitions.import tensorflow as tf
dataset = tf.data.Dataset.from_tensor_slices([1, 2, 3])
repeated = dataset.repeat(2)
print(list(repeated.as_numpy_iterator()))Creates a dataset from three values, repeats the complete sequence twice, and materializes the result.
Training for a fixed number of epochs over an in-memory or file-backed input pipeline.
Feeding a step-based training loop that does not naturally end at the source boundary.
Testing ordering and repetition behavior with a small synthetic dataset.
Dataset.repeat do, and what does its default count mean?repeat(count) appends repeated traversals of the dataset, so each original value is encountered count times. With count=None or -1, traversal is unbounded.
Follow-up: What happens if the source is shuffled? Each repetition can differ because the documentation explicitly allows nondeterministic upstream output.
repeat?The count argument is optional and represents the number of repetitions as a scalar tf.int64 tensor. The optional name supplies a name for the TensorFlow data operation.
Follow-up: Why might a finite count be preferable in evaluation? It gives the consumer a naturally terminating input instead of requiring an explicit stop condition.
For Dataset.from_tensor_slices([1, 2, 3]).repeat(3), iteration yields the sequence three times in order: 1, 2, 3, then 1, 2, 3, then 1, 2, 3. The method returns another DatasetV2.
Follow-up: How should an infinite repeated dataset be consumed? Pair it with a bounded training step count or another operation that limits iteration.
tf.data.Dataset.from_tensor_slicestf.data.Dataset.shuffletf.data.Dataset.batchtf.data.Dataset.taketf.data.Dataset.as_numpy_iterator