pytensor_ml.util.DataLoader#

class pytensor_ml.util.DataLoader(X, y, batch_size=64, dtype=None, random_state=None)#

Draw shuffled, fixed-size batches from a dataset, cycling indefinitely.

Calling the loader returns the next (X_batch, y_batch). Batches are always batch_size rows: a pass that runs off the end of the data is topped up from a freshly shuffled order rather than coming up short, so a batch may straddle two epochs.

Parameters:
Xndarray

Features, indexed along the first axis.

yndarray

Targets, indexed along the first axis and aligned row-wise with X.

batch_sizeint

Rows per batch. Default 64.

dtypestr, optional

Dtype to cast X and y to. Defaults to floatX.

random_stateint or numpy Generator, optional

Seed for the shuffling generator, for reproducible batch sequences.

Examples

Call the loader once per training step; it reshuffles and wraps around on its own, so the loop never has to track epoch boundaries:

import numpy as np

from pytensor_ml.util import DataLoader

X = np.random.default_rng(0).normal(size=(500, 8))
y = np.random.default_rng(1).normal(size=(500, 1))

loader = DataLoader(X, y, batch_size=32, random_state=0)
X_batch, y_batch = loader()

Methods