torchwrench.nn.functional.padding module

class torchwrench.nn.functional.padding.PaddedValue(padded, lengths)[source]

Bases: NamedTuple

lengths : Tensor

Alias for field number 1

padded : Tensor

Alias for field number 0

torchwrench.nn.functional.padding.cat_padded_batch(x1: Tensor, x1_lens: Tensor, x2: Tensor, x2_lens: Tensor, seq_dim: int = -1, batch_dim: int = 0) tuple[Tensor, Tensor][source]

Concatenate padded batched of sequences.

Args:

x1: First batch with D dims of shape (batch_size, …, N1, …) x1_lens: First lengths of each element in sequence dim of shape (batch_size,). x2: Second batch with D dims of shape (batch_size, …, N2, …)

The shape must be the same than x1 unless for the dimension N2.

x2_lens: Second lengths of each element in sequence dim of shape (batch_size,). seq_dim: Dimension index of sequence. defaults to -1. batch_dim: Batch dimension index. defaults to 0.

torchwrench.nn.functional.padding.collate_tensors(tensors: Iterable[Tensor], pad_value: float = 0.0, dim: int = -1) PaddedValue[source]
torchwrench.nn.functional.padding.pad_and_stack_rec(sequence: Tensor | int | float | tuple | list, pad_value: int | float | bool = 0, *, align: 'left' | 'right' | 'center' | 'random' = 'left', device: device | None | 'default' | 'cuda_if_available' | str | int = None, dtype: dtype | None | 'default' | str | DTypeEnum = None) Tensor[source]

Recursive version of torch.nn.utils.rnn.pad_sequence, with padding of Tensors.

Args:

sequence: The sequence to pad. Must be convertable to tensor by having the correct number of dims in all sublists. pad_value: The pad value used. defaults to 0.

Note: Complex values is currently not supported.

align: Alignement used for padding. defaults to “left”. device: The device of the output Tensor. defaults to None. dtype: The dtype of the output Tensor. defaults to None.

Example 1::

>>> sequence = [[1, 2], [3], [], [4, 5]]
>>> output = pad_sequence_rec(sequence, 0)
tensor([[1, 2], [3, 0], [0, 0], [4, 5]])

Example 2::

>>> invalid_sequence = [[1, 2, 3], 3]
>>> output = pad_sequence_rec(invalid_sequence, 0)
ValueError : Cannot pad sequence of tensors of differents number of dims.
torchwrench.nn.functional.padding.pad_dim(x: Tensor, target_length: int, *, dim: int = -1, align: 'left' | 'right' | 'center' | 'random' = 'left', pad_value: int | float | bool | Tensor0D | Callable[[Tensor], int | float | bool] = 0.0, mode: 'constant' | 'reflect' | 'replicate' | 'circular' = 'constant', generator: Generator | None | 'default' | int = None) Tensor[source]

Generic function to pad a tensor along a single dimension.

Args:

x: Tensor to pad of with N dims of shape (…, D, …), where D is the size of the dim-th dimension. target_length: Target length for dim. dims: Axis/dim to pad. defaults to -1. align: Alignement for pad. pad_value: Pad value used to fill tensor. defaults to 0.0.

Note: Complex values is currently not supported.

mode: Pad mode used. defaults to “constant”. generator: Random generator when align is “random”.

Returns:

Padded tensor of N dims of (…, target_length, …).

torchwrench.nn.functional.padding.pad_dims(x: Tensor, target_lengths: Iterable[int], *, dims: Iterable[int] | 'auto' | None = None, aligns: 'left' | 'right' | 'center' | 'random' | Iterable['left' | 'right' | 'center' | 'random'] = 'left', pad_value: int | float | bool | Tensor0D | Callable[[Tensor], int | float | bool] = 0.0, mode: 'constant' | 'reflect' | 'replicate' | 'circular' = 'constant', generator: Generator | None | 'default' | int = None) Tensor[source]

Generic function to pad a tensor along multiple dimensions.

Args:

x: Tensor to pad of with N dims. target_lengths: List of target lengths for each dimension. The list has size M <= N. dims: Dimensions for each length. Must be of size M. If “auto”, creates a list of the M last dimensions. aligns: Alignement or list of alignements for each dimension of size M. pad_value: Pad value used to fill tensor. defaults to 0.0.

Note: Complex values is currently not supported.

mode: Pad mode used. defaults to “constant”. generator: Random generator when aligns is “random”.

Returns:

Padded tensor of N dims.