class paddle.io. RandomSampler ( data_source, replacement=False, num_samples=None, generator=None ) [source]

Iterate samples randomly, yield shuffled indices, if replacement=False, yield shuffled indices of the whole data souce, if replacement=True, num_samples can set to specify the sample number to draw.

  • data_source (Dataset) – dataset to sample, this could be an instance of Dataset or IterableDataset or other Python object which implemented __len__ to get indices as the range of dataset length. Default None.

  • replacement (bool, optional) – If False, sample the whole dataset, If True, set num_samples for how many samples to draw. Default False.

  • num_samples (int, optional) – set sample number to draw if replacement is True, then it will take samples according to the number you set. Default None, disabled.

  • generator (Generator, optional) – specify a generator to sample the data_source. Default None, disabled.


a Sampler yield sample index randomly.

Return type



from paddle.io import Dataset, RandomSampler

class RandomDataset(Dataset):
    def __init__(self, num_samples):
        self.num_samples = num_samples

    def __getitem__(self, idx):
        image = np.random.random([784]).astype('float32')
        label = np.random.randint(0, 9, (1, )).astype('int64')
        return image, label

    def __len__(self):
        return self.num_samples

sampler = RandomSampler(data_source=RandomDataset(100))

for index in sampler: