nemos.batching.LazyArrayDataLoader#
- class nemos.batching.LazyArrayDataLoader(*arrays, batch_size, shuffle='chunk_order', seed=None)[source]#
Bases:
_BaseArrayDataLoaderDataLoader for lazy/out-of-core arrays (e.g. dask, zarr, HDF5).
Unlike
ArrayDataLoader, this loader does not eagerly convert arrays to JAX arrays. Instead, it reads slices from the source arrays and converts each batch to JAX on the fly. This keeps memory usage proportional to batch size rather than dataset size.The default shuffle strategy is
"chunk_order"(approximate): chunk order is randomized each pass, but samples within the same chunk always stay together in the same batch and keep their original order. Use"chunk_and_samples"to also permute samples within each batch. Both require only contiguous reads. Passingshuffle="full"shuffles the whole dataset likeArrayDataLoader. This requires the arrays to support fancy indexing and may be slower than reading contiguous segments. Indices within each batch are sorted to support HDF5 arrays via h5py.This loader is re-iterable: each call to
__iter__()returns a fresh iterator.Examples
>>> import numpy as np >>> from nemos.batching import LazyArrayDataLoader >>> X = np.ones((100, 5)) >>> y = np.ones((100,)) >>> loader = LazyArrayDataLoader(X, y, batch_size=32, shuffle="chunk_order") >>> for X_batch, y_batch in loader: ... pass # Train on batch
Attributes
Number of samples in each batch.
Total number of samples in the dataset.
- __init__(*arrays, batch_size, shuffle='chunk_order', seed=None)[source]#
Initialize a lazy array data loader.
- Parameters:
*arrays (
ArrayLike) – Input and output arrays (any number). Each must support.shapeand sequential slicing (arr[start:end]). The"full"strategy additionally requires support for fancy indexing.batch_size (
int) – Number of samples per batch.shuffle (
str) – Shuffle strategy: one of"none","chunk_order","chunk_and_samples", or"full". Default is"chunk_order".seed (
int|None) – Random seed for shuffling. Default is None.
Methods
__init__(*arrays, batch_size[, shuffle, seed])Initialize a lazy array data loader.
Return the first batch containing at least one valid sample.
- __iter__()#
Return a fresh iterator over batches, dispatching on the shuffle strategy.
- sample_batch()#
Return the first batch containing at least one valid sample.
Scans contiguous batches in sequential order (ignoring shuffle) and returns the first one with a sample that is finite (no NaN/Inf) across every array. This skips leading all-invalid regions, such as the NaN warmup of convolutional-basis features, while loading only as much data as needed.
Validity uses the same
get_valid_multitreenotion asvalidate_inputsand_preprocess_inputs, so an accepted batch is exactly one that passes validation and stays non-empty after NaN-dropping.