Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

TensorFlow · Module

Dataset.repeat

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.

How it works

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.

Syntax

repeat(count=None, name=None) -> 'DatasetV2'

Parameters

NameTypeDescription
counttf.int64 scalar TensorOptional scalar tf.int64 tensor specifying how many times to repeat the dataset; None or -1 means indefinitely.
namestringOptional name for the TensorFlow data operation.

Returns

A DatasetV2 containing the repeated elements.

Key points

  • A finite count makes every source value appear once per dataset traversal, repeated the requested number of traversals.
  • count may be supplied as a scalar tf.int64 tensor rather than only as a Python integer.
  • It operates as a transformation on an existing dataset, commonly after constructing one with from_tensor_slices.
  • The transformation is invoked as an instance method and returns the transformed dataset for further pipeline composition.
  • The resulting dataset can be inspected by converting iteration to NumPy-compatible values.

Common mistakes

  • Leaving count unset or using -1 creates an infinite dataset, so use a terminating consumer or a finite count.
  • Repeated passes are not necessarily identical when the input depends on global state.
  • Upstream nondeterminism, such as shuffle, can produce different elements on different repetitions.

Example: Repeat a dataset a fixed number of times

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.

When to use it

Repeat finite training data

Training for a fixed number of epochs over an in-memory or file-backed input pipeline.

Create an endless input stream

Feeding a step-based training loop that does not naturally end at the source boundary.

Build reproducible pipeline checks

Testing ordering and repetition behavior with a small synthetic dataset.

Interview questions

QWhat does 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.

QWhat are the arguments accepted by 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.

QWhat output order does a finite repeated dataset produce?

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_slices
  • tf.data.Dataset.shuffle
  • tf.data.Dataset.batch
  • tf.data.Dataset.take
  • tf.data.Dataset.as_numpy_iterator

Source: tf.data.TextLineDataset. Checked 2026-10-15.