BatchSampler
SamplerBatchSampler(sampler: Sampler, batch_size: int, drop_last: bool)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
samplerSamplerbatch_sizeintdrop_lastboolTrue, 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)
__init__
→None__init__(sampler: Sampler, batch_size: int, drop_last: bool)Store the inner sampler and batching configuration.
Parameters
samplerSamplerbatch_sizeintdrop_lastbool__iter__
→Iterator[list[int]]__iter__()Yield successive batches of indices.
__len__
→int__len__()Return the number of batches produced per epoch.
Computed as floor(n / batch_size) when drop_last is
True, else ceil(n / batch_size).