mindspore.dataset.RandomSampler

View Source On AtomGit
class mindspore.dataset.RandomSampler(replacement=False, num_samples=None, shuffle=Shuffle.GLOBAL)[source]

Samples the elements randomly.

Note

The shuffling modes supported for different datasets are as follows:

List of support for shuffling mode

Shuffling Mode

MindDataset

TFRecordDataset

Others

Shuffle.ADAPTIVE

Supported

Not Supported

Not Supported

Shuffle.GLOBAL

Supported

Supported

Supported

Shuffle.PARTIAL

Supported

Not Supported

Not Supported

Shuffle.FILES

Supported

Supported

Not Supported

Shuffle.INFILE

Supported

Not Supported

Not Supported

Parameters:
  • replacement (bool, optional) – If True, put the sample ID back for the next draw. Default: False.

  • num_samples (int, optional) – Number of elements to sample. Default: None , which means sample all elements.

  • shuffle (Shuffle, optional) –

    Specify the shuffle mode. Default: Shuffle.GLOBAL, Global shuffle of all rows of data in dataset. There are several levels of shuffling, desired shuffle enum defined by mindspore.dataset.Shuffle .

    • Shuffle.ADAPTIVE : When the number of dataset samples is less than or equal to 100 million, Shuffle.GLOBAL is used. When the number of dataset samples is greater than 100 million, Shuffle.PARTIAL is used. The shuffle is performed once every 1 million samples.

    • Shuffle.GLOBAL : Global shuffle of all rows of data in dataset. The memory usage is large.

    • Shuffle.PARTIAL : Partial shuffle of data in dataset for every 1 million samples. The memory usage is less than Shuffle.GLOBAL .

    • Shuffle.FILES : Shuffle the file sequence but keep the order of data within each file.

    • Shuffle.INFILE : Keep the file sequence the same but shuffle the data within each file.

Raises:
  • TypeError – If replacement is not of type bool.

  • TypeError – If num_samples is not of type int.

  • ValueError – If num_samples is a negative value.

  • TypeError – If shuffle is not of type Shuffle.

Examples

>>> import mindspore.dataset as ds
>>> # creates a RandomSampler
>>> sampler = ds.RandomSampler()
>>> dataset = ds.ImageFolderDataset(image_folder_dataset_dir,
...                                 num_parallel_workers=8,
...                                 sampler=sampler)