quantlab.model.torch_data.IndexBatches#
- class quantlab.model.torch_data.IndexBatches(size, batch_size, *, shuffle=False, drop_last=False, replacement=False, num_samples=None, generator=None)[source]#
Bases:
SamplerBatches of dataset positions, each an int64 tensor, for a dataset that gathers a batch at once.
Each pass draws a fresh order: positions in order, shuffled (
torch.randperm), ornum_samplesdrawn with replacement (torch.randint), fromgenerator; consecutivebatch_sizepositions form a batch, anddrop_lastdrops a last batch shorter thanbatch_size.- Parameters:
- sizeint
Number of items in the dataset.
- batch_sizeint
Positions per batch; at least 1.
- shufflebool, default False
Draw a new random order every pass.
- drop_lastbool, default False
Drop a last incomplete batch.
- replacementbool, default False
Draw
num_samplespositions with replacement (implies shuffling).- num_samplesint, optional
Positions drawn per pass with replacement;
sizewhen unset.- generatortorch.Generator, optional
Source of the random order.
- Raises:
- ValueError
If
batch_sizeis below 1.
Examples
>>> [b.tolist() for b in IndexBatches(5, 2)] [[0, 1], [2, 3], [4]] >>> len(IndexBatches(5, 2, drop_last=True)) 2