TensorPlay

Latest development documentation · Updated 2026-09-08. A documentation snapshot for package 1.0.0.dev20260909 is not available.

On this page

Functions 3

#

default_collate

functionFull reference ↗
tensorplay.utils.data.default_collate(batch)[source]

Take in a batch of data and put the elements within the batch into a tensor with an additional outer dimension - batch size.

The exact output type can be a tensorplay.Tensor, a Sequence of tensorplay.Tensor, a Collection of tensorplay.Tensor, or left unchanged, depending on the input type. This is used as the default function for collation when batch_size or batch_sampler is defined in DataLoader.

Parameters:

batch – a single batch to be collated

#

default_convert

functionFull reference ↗
tensorplay.utils.data.default_convert(data)[source]

Convert each NumPy array element into a tensorplay.Tensor.

If the input is a Sequence, Collection, or Mapping, it tries to convert each element inside to a tensorplay.Tensor. If the input is not a NumPy array, it is left unchanged. This is used as the default function for collation when both batch_sampler and batch_size are NOT defined in DataLoader.

Parameters:

data – a single data point to be converted

#

get_worker_info

functionFull reference ↗
tensorplay.utils.data.get_worker_info() WorkerInfo | None[source]

Returns the information about the current DataLoader iterator worker process. When called in a worker process, returns a WorkerInfo object with information about that worker process; otherwise returns None.

Classes 14

#

BatchSampler

classFull reference ↗
class tensorplay.utils.data.BatchSampler(sampler: Sampler[int] | Iterable[int], batch_size: int, drop_last: bool)[source]

Wraps another sampler to yield a mini-batch of indices.

Parameters:
  • sampler (Sampler or Iterable) – Base sampler. Can be any iterable object

  • batch_size (int) – Size of mini-batch.

  • drop_last (bool) – If True, the sampler will drop the last batch if its size would be less than batch_size

#

ChainDataset

classFull reference ↗
class tensorplay.utils.data.ChainDataset(datasets: Iterable[Dataset])[source]

Dataset for chaining multiple IterableDataset s.

This class is useful to assemble different existing dataset streams. The chaining operation is done on-the-fly, so concatenating large-scale datasets with this class will be efficient.

Parameters:

datasets (iterable of IterableDataset) – datasets to be chained together

#

ConcatDataset

classFull reference ↗
class tensorplay.utils.data.ConcatDataset(datasets: Iterable[Dataset])[source]

Dataset as a concatenation of multiple datasets.

This class is useful to assemble different existing datasets.

Parameters:

datasets (sequence) – List of datasets to be concatenated

#

DataLoader

classFull reference ↗
class tensorplay.utils.data.DataLoader(dataset: Dataset[_T_co], batch_size: int | None = 1, shuffle: bool | None = None, sampler: Sampler | Iterable | None = None, batch_sampler: Sampler | Iterable | None = None, num_workers: int = 0, collate_fn: Callable[[List[Any]], Any] | None = None, pin_memory: bool = False, drop_last: bool = False, timeout: float = 0, worker_init_fn: Callable[[int], None] | None = None, multiprocessing_context=None, generator: Generator | None = None, *, prefetch_factor: int | None = None, persistent_workers: bool = False, pin_memory_device: str = '', in_order: bool = True, device: str | None = None)[source]

Data loader combines a dataset and a sampler, and provides an iterable over the given dataset.

The DataLoader supports both map-style and iterable-style datasets with single- or multi-process loading, customizing loading order and optional automatic batching (collation).

Parameters:
  • dataset (Dataset) – dataset from which to load the data.

  • batch_size (int, optional) – how many samples per batch to load (default: 1).

  • shuffle (bool, optional) – set to True to have the data reshuffled at every epoch (default: False).

  • sampler (Sampler or Iterable, optional) – defines the strategy to draw samples from the dataset. Can be any Iterable with __len__ implemented. If specified, shuffle must not be specified.

  • batch_sampler (Sampler or Iterable, optional) – like sampler, but returns a batch of indices at a time. Mutually exclusive with batch_size, shuffle, sampler, and drop_last.

  • num_workers (int, optional) – how many subprocesses to use for data loading. 0 means that the data will be loaded in the main process. (default: 0)

  • collate_fn (Callable, optional) – merges a list of samples to form a mini-batch of Tensor(s). Used when using batched loading from a map-style dataset.

  • pin_memory (bool, optional) – If True, the data loader will copy Tensors into CUDA page-locked host memory before returning them.

  • drop_last (bool, optional) – set to True to drop the last incomplete batch, if the dataset size is not divisible by the batch size. (default: False)

  • timeout (numeric, optional) – if positive, the timeout value for collecting a batch from workers. Should always be non-negative. (default: 0)

  • worker_init_fn (Callable, optional) – If not None, this will be called on each worker subprocess with the worker id (an int in [0, num_workers - 1]) as input, before data loading. (default: None)

  • multiprocessing_context (str or context, optional) – start method or multiprocessing context used to spawn the workers, e.g., "fork" or "spawn". If None, the default context of the platform is used. (default: None)

  • generator (Generator, optional) – If not None, this RNG will be used by RandomSampler to generate random indexes. (default: None)

  • prefetch_factor (int, optional) – Number of batches loaded in advance by each worker. 2 means there will be a total of 2 * num_workers batches prefetched across all workers. (default: 2 when num_workers > 0; otherwise must be None)

  • persistent_workers (bool, optional) – If True, the data loader will not shut down the worker processes after a dataset has been consumed once. This allows to maintain the workers Dataset instances alive. (default: False)

  • pin_memory_device (str, optional) – Deprecated device spelling kept for accelerator for pinned host allocations. (default: "")

  • in_order (bool, optional) – If False, the data loader will not enforce that batches returned from multiprocessing workers are provided in the order the sampler produced them. This enables faster delivery of batches that complete early, at the cost of batch order no longer being deterministic. (default: True)

  • device (str, optional) – device to move batches to after collation.

#

Dataset

classFull reference ↗
class tensorplay.utils.data.Dataset[source]

An abstract class representing a Dataset.

All datasets that represent a map from keys to data samples should subclass it. All subclasses should overwrite __getitem__(), supporting fetching a data sample for a given key. Subclasses could also optionally overwrite __len__(), which is expected to return the size of the dataset by many Sampler implementations and the default options of DataLoader.

Note

DataLoader by default constructs an index sampler that yields integral indices. To make it work with a map-style dataset with non-integral indices/keys, a custom sampler must be provided.

#

IterableDataset

classFull reference ↗
class tensorplay.utils.data.IterableDataset[source]

An iterable Dataset.

All datasets that represent an iterable of data samples should subclass it. Such form of datasets is particularly useful when data come from a stream.

All subclasses should overwrite __iter__(), which would return an iterator of samples in this dataset.

When a subclass is used with DataLoader, each item in the dataset will be yielded from the DataLoader iterator.

#

RandomSampler

classFull reference ↗
class tensorplay.utils.data.RandomSampler(data_source: Sized, replacement: bool = False, num_samples: int | None = None, generator: Generator | None = None)[source]

Samples elements randomly. If without replacement, then sample from a shuffled dataset. If with replacement, then user can specify num_samples to draw.

Parameters:
  • data_source (Sized) – data source to sample from. Must implement __len__.

  • replacement (bool) – samples are drawn on-demand with replacement if True, default=``False``.

  • num_samples (int) – number of samples to draw, default=`len(dataset)`.

  • generator (Generator) – Generator used in sampling.

#

Sampler

classFull reference ↗
class tensorplay.utils.data.Sampler[source]

Base class for all Samplers.

Every Sampler subclass has to provide an __iter__() method, providing a way to iterate over indices or lists of indices (batches) of dataset elements, and may provide a __len__() method that returns the length of the returned iterators.

Note

The __len__() method isn’t strictly required by DataLoader, but is expected in any calculation involving the length of a DataLoader.

#

SequentialSampler

classFull reference ↗
class tensorplay.utils.data.SequentialSampler(data_source: Sized)[source]

Samples elements sequentially, always in the same order.

Parameters:

data_source (Sized) – data source to sample from. Must implement __len__.

#

StackDataset

classFull reference ↗
class tensorplay.utils.data.StackDataset(*args: Dataset[_T_co], **kwargs: Dataset[_T_co])[source]

Dataset as a stacking of multiple datasets.

This class is useful to assemble different parts of complex input data, given as datasets.

Example

>>> images = ImageDataset()
>>> texts = TextDataset()
>>> tuple_stack = StackDataset(images, texts)
>>> tuple_stack[0] == (images[0], texts[0])
>>> dict_stack = StackDataset(image=images, text=texts)
>>> dict_stack[0] == {"image": images[0], "text": texts[0]}
Parameters:
  • *args (Dataset) – Datasets for stacking returned as tuple.

  • **kwargs (Dataset) – Datasets for stacking returned as dict.

#

Subset

classFull reference ↗
class tensorplay.utils.data.Subset(dataset: Dataset[_T_co], indices: Sequence[int])[source]

Subset of a dataset at specified indices.

Note

When subclassing Subset and overriding __getitem__, you must also override __getitems__ to ensure DataLoader works correctly with your custom logic. If you override only __getitem__, a NotImplementedError will be raised when using DataLoader.

Parameters:
  • dataset (Dataset) – The whole Dataset

  • indices (sequence) – Indices in the whole set selected for subset

#

SubsetRandomSampler

classFull reference ↗
class tensorplay.utils.data.SubsetRandomSampler(indices: Sequence[int], generator: Generator | None = None)[source]

Samples elements randomly from a given list of indices, without replacement.

Parameters:
  • indices (sequence) – a sequence of indices

  • generator (Generator) – Generator used in sampling.

#

TensorDataset

classFull reference ↗
class tensorplay.utils.data.TensorDataset(*tensors: TensorBase)[source]

Dataset wrapping tensors.

Each sample will be retrieved by indexing tensors along the first dimension.

Parameters:

*tensors (Tensor) – tensors that have the same size of the first dimension.

#

WeightedRandomSampler

classFull reference ↗
class tensorplay.utils.data.WeightedRandomSampler(weights: Sequence[float], num_samples: int, replacement: bool = True, generator: Generator | None = None)[source]

Samples elements from [0,..,len(weights)-1] with given probabilities (weights).

Parameters:
  • weights (sequence) – a sequence of weights, not necessary summing up to one

  • num_samples (int) – number of samples to draw

  • replacement (bool) – if True, samples are drawn with replacement. If not, they are drawn without replacement, which means that when a sample index is drawn for a row, it cannot be drawn again for that row.

  • generator (Generator) – Generator used in sampling.

Search documentation

Search all 1,743 documentation pages.

Keyboard shortcuts

Global

  • /Focus search
  • ?This dialog
  • ,Open settings
  • jAI assistant

Search

  • Navigate results
  • Open result
  • escClose

Package

  • mMain information
  • dDocs
  • .Code
  • -Changelog
  • tTimeline
  • sStats
  • vVersions