torchoutil.nn.functional.padding module¶
- cat_padded_batch( ) 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.
- pad_and_stack_rec(
- sequence: Tensor | int | float | tuple | list,
- pad_value: int | float | bool = 0,
- *,
- align: Literal['left', 'right', 'center', 'random'] = 'left',
- device: device | None | Literal['default', 'cuda_if_available'] | str | int = None,
- dtype: dtype | None | Literal['default'] | str | DTypeEnum = None,
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. 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.
- pad_dim(
- x: Tensor,
- target_length: int,
- *,
- dim: int = -1,
- align: Literal['left', 'right', 'center', 'random'] = 'left',
- pad_value: int | float | bool | Callable[[Tensor], int | float | bool] = 0.0,
- mode: Literal['constant', 'reflect', 'replicate', 'circular'] = 'constant',
- generator: Generator | None | Literal['default'] | int = None,
Generic function for pad a single dimension.
- pad_dims(
- x: Tensor,
- target_lengths: Iterable[int],
- *,
- dims: Iterable[int] = (-1,),
- aligns: Literal['left', 'right', 'center', 'random'] | Iterable[Literal['left', 'right', 'center', 'random']] = ('left',),
- pad_value: int | float | bool | Callable[[Tensor], int | float | bool] = 0.0,
- mode: Literal['constant', 'reflect', 'replicate', 'circular'] = 'constant',
- generator: Generator | None | Literal['default'] | int = None,
Generic function to pad multiple dimensions.