Skip to content

Data API

MNIST

The loader downloads the four standard MNIST archives on first use, caches them, normalizes images to float32, and returns flattened 784-value samples.

load_mnist

load_mnist(cache_dir: str | None = None)

Load MNIST, downloading if not already cached.

Returns

(x_train, y_train), (x_test, y_test) x : float32 ndarray of shape (N, 784), values in [0, 1] y : int32 ndarray of shape (N,), values in 0–9

Source code in MiniTorch/data/mnist.py
def load_mnist(cache_dir: str | None = None):
    """
    Load MNIST, downloading if not already cached.

    Returns
    -------
    (x_train, y_train), (x_test, y_test)
        x : float32 ndarray of shape (N, 784), values in [0, 1]
        y : int32   ndarray of shape (N,),     values in 0–9
    """
    if cache_dir is None:
        cache_dir = _CACHE_DIR

    print("Loading MNIST …")
    x_train = _parse_images(_download("train_images", cache_dir))
    y_train = _parse_labels(_download("train_labels", cache_dir))
    x_test  = _parse_images(_download("test_images",  cache_dir))
    y_test  = _parse_labels(_download("test_labels",  cache_dir))
    print(f"  train: {x_train.shape}  test: {x_test.shape}")
    return (x_train, y_train), (x_test, y_test)

DataLoader

DataLoader yields NumPy feature and label arrays. Wrap features in a Variable when using eager autograd.

DataLoader

DataLoader(x: ndarray, y: ndarray, batch_size: int = 32, shuffle: bool = True)

Simple mini-batch iterator over (x, y) numpy arrays.

Parameters

x : feature array, shape (N, ...) y : label array, shape (N, ...) batch_size : number of samples per batch shuffle : whether to shuffle before each epoch

Source code in MiniTorch/data/dataloader.py
def __init__(
    self,
    x: np.ndarray,
    y: np.ndarray,
    batch_size: int = 32,
    shuffle: bool = True,
):
    assert len(x) == len(y), "x and y must have the same length"
    self.x = x
    self.y = y
    self.batch_size = batch_size
    self.shuffle = shuffle
    self.n = len(x)

__iter__

__iter__()
Source code in MiniTorch/data/dataloader.py
def __iter__(self):
    indices = np.arange(self.n)
    if self.shuffle:
        np.random.shuffle(indices)
    for start in range(0, self.n, self.batch_size):
        idx = indices[start : start + self.batch_size]
        yield self.x[idx], self.y[idx]

__len__

__len__() -> int

Number of batches per epoch.

Source code in MiniTorch/data/dataloader.py
def __len__(self) -> int:
    """Number of batches per epoch."""
    return (self.n + self.batch_size - 1) // self.batch_size