DataLoader#

class braintools.trainer.DataLoader(dataset, batch_size=32, shuffle=False, sampler=None, batch_sampler=None, num_workers=0, collate_fn=None, drop_last=False, prefetch=2, seed=None)#

JAX-compatible data loader.

Parameters:
  • dataset (Dataset | Any) – Dataset or array(s) to load from.

  • batch_size (int) – Number of samples per batch.

  • shuffle (bool) – Whether to shuffle the data each epoch.

  • sampler (Sampler | None) – Custom sampler. Mutually exclusive with shuffle.

  • batch_sampler (BatchSampler | None) – Custom batch sampler. Mutually exclusive with batch_size, shuffle, sampler.

  • num_workers (int) – Number of worker processes for data loading (not implemented in JAX).

  • collate_fn (Callable | None) – Function to collate samples into batches.

  • drop_last (bool) – Whether to drop the last incomplete batch.

  • prefetch (int) – Number of batches to prefetch (for device transfer).

  • seed (int | None) – Random seed for shuffling.

Examples

Basic usage with a tuple of arrays:

>>> import jax.numpy as jnp
>>> import braintools
>>> X = jnp.ones((100, 8))
>>> y = jnp.zeros((100,))
>>> loader = braintools.trainer.DataLoader((X, y), batch_size=32, shuffle=True, seed=0)
>>> len(loader)
4
>>> for x_batch, y_batch in loader:
...     print(x_batch.shape, y_batch.shape)
(32, 8) (32,)
(32, 8) (32,)
(32, 8) (32,)
(4, 8) (4,)

With a dictionary dataset, dropping the incomplete last batch:

>>> dataset = braintools.trainer.DictDataset({'x': X, 'y': y})
>>> loader = braintools.trainer.DataLoader(dataset, batch_size=32, drop_last=True)
>>> [batch['x'].shape for batch in loader]
[(32, 8), (32, 8), (32, 8)]
property num_samples: int#

Return the total number of samples.

set_epoch(epoch)[source]#

Set the epoch (for distributed samplers).