mxnet.gluon.data.sampler

Dataset sampler.

Classes

BatchSampler(sampler, batch_size[, last_batch])

Wraps over another Sampler and return mini-batches of samples.

FilterSampler(fn, dataset)

Samples elements from a Dataset for which fn returns True.

IntervalSampler(length, interval[, rollover])

Samples elements from [0, length) at fixed intervals.

RandomSampler(length)

Samples elements from [0, length) randomly without replacement.

Sampler()

Base class for samplers.

SequentialSampler(length[, start])

Samples elements from [start, start+length) sequentially.

class mxnet.gluon.data.sampler.BatchSampler(sampler, batch_size, last_batch='keep')[source]

Bases: Sampler

Wraps over another Sampler and return mini-batches of samples.

Parameters:
  • sampler (Sampler) – The source Sampler.

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

  • last_batch ({'keep', 'discard', 'rollover'}) –

    Specifies how the last batch is handled if batch_size does not evenly divide sequence length.

    If ‘keep’, the last batch will be returned directly, but will contain less element than batch_size requires.

    If ‘discard’, the last batch will be discarded.

    If ‘rollover’, the remaining elements will be rolled over to the next iteration.

Examples

>>> sampler = gluon.data.SequentialSampler(10)
>>> batch_sampler = gluon.data.BatchSampler(sampler, 3, 'keep')
>>> list(batch_sampler)
[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]
class mxnet.gluon.data.sampler.FilterSampler(fn, dataset)[source]

Bases: Sampler

Samples elements from a Dataset for which fn returns True.

Parameters:
  • fn (callable) – A callable function that takes a sample and returns a boolean

  • dataset (Dataset) – The dataset to filter.

class mxnet.gluon.data.sampler.IntervalSampler(length, interval, rollover=True)[source]

Bases: Sampler

Samples elements from [0, length) at fixed intervals.

Parameters:
  • length (int) – Length of the sequence.

  • interval (int) – The number of items to skip between two samples.

  • rollover (bool, default True) – Whether to start again from the first skipped item after reaching the end. If true, this sampler would start again from the first skipped item until all items are visited. Otherwise, iteration stops when end is reached and skipped items are ignored.

Examples

>>> sampler = contrib.data.IntervalSampler(13, interval=3)
>>> list(sampler)
[0, 3, 6, 9, 12, 1, 4, 7, 10, 2, 5, 8, 11]
>>> sampler = contrib.data.IntervalSampler(13, interval=3, rollover=False)
>>> list(sampler)
[0, 3, 6, 9, 12]
class mxnet.gluon.data.sampler.RandomSampler(length)[source]

Bases: Sampler

Samples elements from [0, length) randomly without replacement.

Parameters:

length (int) – Length of the sequence.

class mxnet.gluon.data.sampler.Sampler[source]

Bases: object

Base class for samplers.

All samplers should subclass Sampler and define __iter__ and __len__ methods.

class mxnet.gluon.data.sampler.SequentialSampler(length, start=0)[source]

Bases: Sampler

Samples elements from [start, start+length) sequentially.

Parameters:
  • length (int) – Length of the sequence.

  • start (int, default is 0) – The start of the sequence index.