espnet2.samplers.abs_sampler.AbsSampler
espnet2.samplers.abs_sampler.AbsSampler
class espnet2.samplers.abs_sampler.AbsSampler(data_source: Sized | None = None)
Bases: Sampler
, ABC
Abstract base class for defining custom samplers in PyTorch.
The AbsSampler class serves as a template for creating samplers that define how data should be sampled from a dataset. This class inherits from PyTorch’s Sampler class and mandates the implementation of the __len__ and __iter__ methods. Custom samplers must extend this class and provide specific sampling logic.
None
- Parameters:None
- Returns: None
- Yields: None
- Raises:
- NotImplementedError – If __len__ or __iter__ methods are not
- implemented in a subclass. –
####### Examples
To create a custom sampler, subclass AbsSampler and implement the required methods:
``
`
python class CustomSampler(AbsSampler):
def __len__(self) -> int: : return 100 # Example length
def __iter__(self) -> Iterator[Tuple[str, …]]: : for i in range(self._len_()): : yield (str(i),) # Example yield
sampler = CustomSampler() print(len(sampler)) # Output: 100 for item in sampler:
print(item) # Output: (‘0’,), (‘1’,), …, (‘99’,)
``
`
NOTE
The generate method returns a list of items generated by iterating over the sampler. It can be used for obtaining a deterministic sample based on a specific seed.
generate(seed)
Generates a list of samples from the sampler.
This method returns a list of samples generated by the sampler instance. The output will depend on the specific implementation of the sampler. The method takes a seed for random number generation to ensure reproducibility of the sampling process.
- Parameters:
- seed (int) – A seed for the random number generator to ensure reproducible
- results.
- Returns: A list of samples generated by the sampler.
- Return type: List[Tuple[str, …]]
####### Examples
>>> sampler = SomeConcreteSampler()
>>> samples = sampler.generate(seed=42)
>>> print(samples)
[('sample1',), ('sample2',), ('sample3',)]
NOTE
The behavior of this method is dependent on the specific implementation of the AbsSampler subclass.