mindspore.dataset.dataloader.BatchSampler

View Source On Gitee
class mindspore.dataset.dataloader.BatchSampler(sampler, batch_size, drop_last)[source]

Sampler that yields a mini-batch of indices each time.

Parameters
  • sampler (Union[Sampler, Iterable]) – Sampler used to generate individual indices.

  • batch_size (int) – Size of the mini-batch.

  • drop_last (bool) – Whether to drop the last batch if its size is less than batch_size.

Examples

>>> from mindspore.dataset.dataloader import BatchSampler, SequentialSampler
>>>
>>> dataset = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> sequential_sampler = SequentialSampler(dataset)
>>>
>>> batch_sampler = BatchSampler(sequential_sampler, 4, False)
>>> print(list(batch_sampler))
[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9]]
>>>
>>> batch_sampler = BatchSampler(sequential_sampler, 4, True)
>>> print(list(batch_sampler))
[[0, 1, 2, 3], [4, 5, 6, 7]]