flambe.sampler

Package Contents

class flambe.sampler.Sampler[source]

Bases: flambe.compile.Component

Base Sampler interface.

Objects implementing this interface should implement two methods:

  • sample: takes a set of data and returns an iterator
  • lenght: takes a set of data and return the length of the
    iterator that would be given by the sample method

Sampler objects are used inside the Trainer to provide the data to the models. Note that pushing the data to the appropriate device is usually done inside the Trainer.

sample(self, data: Sequence[Sequence[torch.Tensor]], n_epochs: int = 1)

Sample from the list of features and yields batches.

Parameters:
  • data (Sequence[Sequence[torch.Tensor, ..]]) – The input data to sample from
  • n_epochs (int, optional) – The number of epochs to run in the output iterator.
Yields:

Iterator[Tuple[Tensor]] – A batch of data, as a tuple of Tensors

length(self, data: Sequence[Sequence[torch.Tensor]])

Return the number of batches in the sampler.

Parameters:data (Sequence[Sequence[torch.Tensor, ..]]) – The input data to sample from
Returns:The number of batches that would be created per epoch
Return type:int
class flambe.sampler.BaseSampler(batch_size: int = 64, shuffle: bool = True, pad_index: Union[int, Sequence[int]] = 0, n_workers: int = 0, pin_memory: bool = False, seed: Optional[int] = None, downsample: Optional[float] = None, downsample_max_samples: Optional[int] = None, downsample_seed: Optional[int] = None, drop_last: bool = False)[source]

Bases: flambe.sampler.sampler.Sampler

Implements a BaseSampler object.

This is the most basic implementation of a sampler. It uses Pytorch’s DataLoader object internally, and offers the possiblity to override the sampling of the examples and how to from a batch from them.

sample(self, data: Sequence[Sequence[torch.Tensor]], n_epochs: int = 1)

Sample from the list of features and yields batches.

Parameters:
  • data (Sequence[Sequence[torch.Tensor, ..]]) – The input data to sample from
  • n_epochs (int, optional) – The number of epochs to run in the output iterator. Use -1 to run infinitely.
Yields:

Iterator[Tuple[Tensor]] – A batch of data, as a tuple of Tensors

length(self, data: Sequence[Sequence[torch.Tensor]])

Return the number of batches in the sampler.

Parameters:data (Sequence[Sequence[torch.Tensor, ..]]) – The input data to sample from
Returns:The number of batches that would be created per epoch
Return type:int
class flambe.sampler.EpisodicSampler(n_support: int, n_query: int, n_episodes: int, n_classes: int = None, pad_index: int = 0, balance_query: bool = False)[source]

Bases: flambe.sampler.Sampler

Implement an EpisodicSample object.

Currently only supports sequence inputs.

sample(self, data: Sequence[Sequence[torch.Tensor]], n_epochs: int = 1)

Sample from the list of features and yields batches.

Parameters:
  • data (Sequence[Sequence[torch.Tensor, torch.Tensor]]) – The input data as a list of (source, target) pairs
  • n_epochs (int, optional) – The number of epochs to run in the output iterator. For this object, the total number of batches will be (n_episodes * n_epochs)
Yields:

Iterator[Tuple[Tensor, Tensor, Tensor, Tensor]] – In order: the query_source, the query_target the support_source, and the support_target tensors. For sequences, the batch is used as first dimension.

length(self, data: Sequence[Sequence[torch.Tensor]])

Return the number of batches in the sampler.

Parameters:data (Sequence[Sequence[torch.Tensor, ..]]) – The input data to sample from
Returns:The number of batches that would be created per epoch
Return type:int