BatchLoader

BatchLoader: iterator-based batching over any Dataset.

class src.batch_loader.BatchLoader(dataset: Dataset, batch_size: int, shuffle: bool = False, drop_last: bool = False)[source]

Bases: object

Wraps a dataset and yields batches of data via iteration.

Batches are built from indices only; the actual data is loaded from the dataset (which may itself be lazy) only when each batch is consumed.

Parameters:
  • dataset – Any Dataset instance (labeled or unlabeled).

  • batch_size – Number of samples per batch.

  • shuffle – If True, indices are randomly shuffled before batching at the start of every iteration. If False, data is returned in its original order.

  • drop_last – If True, the last batch is discarded when it contains fewer than batch_size samples. If False (default), the last batch is kept even if it is smaller.

Raises:
  • TypeError – If any argument has an unexpected type.

  • ValueError – If batch_size is less than 1.

Example:

loader = BatchLoader(dataset, batch_size=32, shuffle=True)
for batch in loader:
    # batch is a list of (data, label) tuples or data items
    ...
property dataset: Dataset

The underlying dataset.

property batch_size: int

Number of samples per batch.

property shuffle: bool

Whether indices are shuffled at the start of each iteration.

property drop_last: bool

Whether the last incomplete batch is dropped.