• >
  • torchdata.samplers
Shortcuts

torchdata.samplers

This module implements samplers to be used in conjunction with torch.utils.data.DataLoader instances.

Those can be used just like PyTorch’s torch.utils.data.Sampler instances.

See PyTorch tutorial for more examples and information.

class torchdata.samplers.Distribution(distribution: torch.distributions.distribution.Distribution, num_samples: int)[source]

Sample num_samples indices from distribution object.

Parameters
  • distribution (torch.distributions.distribution.Distribution) – Distribution-like object implementing sample() method.

  • num_samples (int) – Number of samples to be yielded

class torchdata.samplers.RandomOverSampler(labels)[source]

Sample elements randomly with underrepresented classes upsampled.

Length is equal to max_samples_per_class * classes.

Parameters

labels (torch.Tensor) – Tensor containing labels for respective samples.

class torchdata.samplers.RandomSubsetSampler(indices, replacement=False, num_samples=None)[source]

Sample elements randomly from a given list of indices.

If without replacement, then sample from a shuffled dataset. If with replacement, then user can specify num_samples to draw.

Similar to PyTorch’s SubsetRandomSampler, but this one allows you to specify indices which will be sampled in random order, not range subsampled.

Parameters
  • indices (typing.Iterable) – A sequence of indices

  • replacement (bool, optional) – Samples are drawn with replacement if True. Default: False

  • num_samples (int, optional) – Number of samples to draw, default=`len(dataset)`. This argument is supposed to be specified only when replacement is True. Default: None

class torchdata.samplers.RandomUnderSampler(labels: None._VariableFunctions.tensor)[source]

Sample elements randomly with overrepresnted classes downsampled.

Length is equal to min_samples_per_class * classes.

Parameters

labels (torch.Tensor) – Tensor containing labels for respective samples.