RandomSampler¶
- 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, ifreplacement=True
,num_samples
can set to specify the sample number to draw.- Parameters
-
data_source (Dataset) – dataset to sample, this could be an instance of
paddle.io.Dataset
other Python object which implemented__len__
.replacement (bool) – If False, sample the whole dataset, If False, set
num_samples
for how many sample to draw. Default False.num_samples (int) – set sample number to draw if
replacement
is True. Default None.generator (Generator) – specify a generator to sample the data source. Default None
- Returns
-
a Sampler yield sample index randomly
- Return type
-
Sampler
Examples
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: print(index)
see paddle.io.Sampler