class

StackDataset

extendsDataset
StackDataset(args: Dataset = (), kwargs: Dataset = {})
source

Bundle several map-style datasets so each index returns a stacked tuple.

The bundled datasets must agree in length. Item i is the tuple (d_1[i], d_2[i], \dots, d_K[i]) for each child dataset — handy when paired modalities (image, caption, label) live in separate sources but share a common index.

Accepts either positional children (StackDataset(d1, d2)) or keyword children (StackDataset(image=d1, label=d2)); the two forms are mutually exclusive. Positional construction returns tuples; keyword construction returns dicts keyed by the supplied names.

Parameters

*argsDataset= ()
Positional child datasets. Cannot be combined with **kwargs.
**kwargsDataset= {}
Keyword child datasets. Cannot be combined with *args.

Notes

Equivalent in spirit to a per-index zip across child datasets, but exposed as a Dataset so it can drive a sampler-based DataLoader. All children must satisfy len(d_k) == n for some shared n; otherwise construction raises ValueError.

Examples

>>> images = TensorDataset(X_img)
>>> labels = TensorDataset(y_lbl)
>>> ds = StackDataset(image=images, label=labels)
>>> sample = ds[0]
>>> sorted(sample.keys())
['image', 'label']

Methods (3)

dunder

__init__

None
__init__(args: Dataset = (), kwargs: Dataset = {})
source

Bundle child datasets either positionally or by keyword.

Parameters

*argsDataset= ()
Positional child datasets. The resulting samples are tuples.
**kwargsDataset= {}
Keyword child datasets. The resulting samples are dicts keyed by the keyword names.

Raises

ValueError
If both positional and keyword children are given, if no children are given, or if the children disagree in length.
dunder

__len__

int
__len__()
source

Return the common length shared by all bundled child datasets.

dunder

__getitem__

tuple or dict
__getitem__(idx: int)
source

Return one sample drawn from each child at index idx.

Parameters

idxint
Index into the bundled datasets.

Returns

tuple or dict

A tuple of child samples if the dataset was built positionally, otherwise a dict keyed by the keyword names supplied at construction time.