torch_utils.py
import goldener.torch_utils
Utilities to work with PyTorch tensors and datasets.
This module gathers helpers to collate batches, convert tensors from/to numpy vectors and access the samples of
datasets, as well as ResetableTorchIterableDataset, an iterable dataset that can be re-initialized.
Collate dictionary samples while preserving sequence-valued features.
Arguments:
- batch: A list of samples, where each sample is a dictionary.
Returns:
A dictionary with sequences kept per sample and all other values collated with PyTorch's default collate function.
Raises:
- KeyError: If a sample is missing a key present in the first sample.
Convert a torch tensor to a 2D tensor.
If the input tensor is 0D, it is converted to a (1, 1) tensor. If the input tensor is 1D, it is converted to a (N, 1) tensor. If the input tensor is 2D, it is kept as (N, M) tensor. If the input tensor is 3D or higher, the second dimension is moved to the last dimension, and the first dimensions are flattened to form a 2D tensor.
Arguments:
- x: Input tensor convert to 2D tensor.
Returns:
A 2D tensor of shape (N, M)
Convert a torch tensor to a numpy array of vectors.
If the input tensor is 0D, it is converted to a (1, 1) array. If the input tensor is 1D, it is converted to a (N, 1) array. If the input tensor is 2D, it is kept as (N, M) array. If the input tensor is 3D or higher, the second dimension is moved to the last dimension, and the first dimensions are flattened to form a 2D array of vectors.
Arguments:
- x: Input tensor convert to numpy vectors.
Returns:
A 2D numpy array of shape (N, M)
Convert a numpy array to a torch tensor.
If the input array is 0D, it is converted to a (1, 1) tensor. If the input array is 1D, it is converted to a (N, 1) tensor. If the input array is 2D, it is kept as (N, M) tensor. If the input array is 3D or higher, the last dimension is moved to the second dimension, and the first dimensions are reshaped to form the desired shape.
Arguments:
- x: Input numpy array to convert to torch tensor.
- shape: Desired shape of the output tensor.
- dtype: Desired data type of the output tensor.
- device: Desired device of the output tensor.
Returns:
A torch tensor of the desired shape, dtype, and device.
Raises:
- ValueError: If the desired shape is less than 2D.
Apply a numpy transformation to a torch tensor.
Arguments:
- x: Input tensor to transform.
- transform_np: A callable that takes a numpy array and returns a transformed numpy array.
Returns:
A transformed torch tensor with the same dtype and device as the input tensor.
A resetable iterable dataset that can be re-initialized.
It is useful for accessing the first element (to set up a table for instance) of the dataset before looping over the full dataset.
Attributes:
- data_iterable: The underlying iterable dataset.
- _data_iterator: The current iterator over the dataset, or None once it
has been exhausted (see
__next__) and not yet re-created.
Get a sample from a dataset as a dictionary and validate some of its keys.
Arguments:
- dataset: The dataset to get a sample from.
- collate_fn: An optional collate function to apply to the sample.
- expected: An optional list of keys that must be present in the sample.
- excluded: An optional list of keys that must not be present in the sample.
Returns:
A sample from the dataset as a dictionary.
Raises:
- ValueError: If the sample is not a dictionary after applying the
collate_fn. - ValueError: If the sample is missing any of the
expectedkeys. - ValueError: If the sample contains any of the
excludedkeys.
Get the unique values/vectors of a given dimension in a tensor.
Arguments:
- tensor: The input tensor to get unique values from.
- dim: The dimension along which to get unique values. Default is 1.
Returns:
A tensor with all the unique values of the input tensor for teh specified dimension.
Shuffle the given items in a list.
Arguments:
- items: The list of items to shuffle.
- generator: A torch generator to manage the random shuffling.
Returns: A list of shuffled items.