builder
data.sampler.builder
SAMPLER_IMPLEMENTATION
module-attribute
SAMPLER_IMPLEMENTATION = {'InfiniteRandomSampler': InfiniteRandomSampler}
InfiniteRandomSampler
Bases: RandomSampler
Infinitely samples elements randomly. If without replacement, then sample from a shuffled dataset.
If with replacement, then user can specify :attr:num_samples to draw.
Parameters:
-
data_source(Dataset) –dataset to sample from
-
replacement(bool) –samples are drawn on-demand with replacement if
True, default=False -
num_samples(int) –number of samples to draw, default=
len(dataset). -
generator(Generator) –Generator used in sampling.
Assume that this DataLoader is used for training with shuffle=True.
We use InfiniteRandomSampler to make the DataLoader to infinitely sample the dataset.
This prevents the "drop_last" and the "prefeching" problem across epochs.
Be careful that you should stop the training loop by counting the number of steps manually.
The DataLoader will never stop by itself.
NOTE: Note that this sampler has infinite length and thus you should be careful when calculating the current epoch. We recommend you to use step//steps_per_epoch to calculate the current epoch.
__iter__
Source code in SaigeToolkit/data/sampler/infinite_random_sampler.py
build_sampler
build_sampler(_target_: Optional[str], data_source: Optional[Sized], **cfg_sampler: dict) -> Optional[Sampler]
Parameters:
-
cfg_sampler(dict, default:{}) –config dict for building sampler
Returns:
-
Optional[Sampler]–Optional[Sampler]: sampler object instance. if cfg_sampler is None, return None
Notes
Follows the interface of torch.utils.data.Sampler.