class

BatchSampler

extendsSampler
BatchSampler(sampler: Sampler, batch_size: int, drop_last: bool)
source

Group an inner sampler's indices into mini-batches.

Wraps an existing Sampler and packs its emitted indices into fixed-size lists. This is what DataLoader uses internally to convert a per-sample index stream into per-batch index lists.

Parameters

samplerSampler
Underlying per-sample index sampler.
batch_sizeint
Number of indices per yielded batch.
drop_lastbool
If True, drop the trailing batch when the inner sampler's length is not divisible by batch_size. If False, yield the short final batch.

Notes

The inner sampler is consumed lazily — BatchSampler simply accumulates batch_size indices then yields the list and starts a new batch. drop_last=True produces floor(n / batch_size) batches of uniform size (the usual choice for training, where short batches mess with BatchNorm statistics); drop_last=False produces ceil(n / batch_size) batches with the final one possibly short (the usual choice for evaluation, where every sample must be visited).

Examples

>>> inner = SequentialSampler(my_dataset)   # 10 items
>>> bs = BatchSampler(inner, batch_size=4, drop_last=False)
>>> [b for b in bs]
[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9]]

Methods (3)

dunder

__init__

None
__init__(sampler: Sampler, batch_size: int, drop_last: bool)
source

Store the inner sampler and batching configuration.

Parameters

samplerSampler
Inner per-sample sampler.
batch_sizeint
Target batch size.
drop_lastbool
Whether to discard the final short batch.
dunder

__iter__

Iterator[list[int]]
__iter__()
source

Yield successive batches of indices.

dunder

__len__

int
__len__()
source

Return the number of batches produced per epoch.

Computed as floor(n / batch_size) when drop_last is True, else ceil(n / batch_size).