espnet2.gan_codec.shared.encoder.seanet.pad1d
Less than 1 minute
espnet2.gan_codec.shared.encoder.seanet.pad1d
espnet2.gan_codec.shared.encoder.seanet.pad1d(x: Tensor, paddings: Tuple[int, int], mode: str = 'zero', value: float = 0.0)
Apply padding to a 1D tensor with support for reflection and other modes.
This function serves as a lightweight wrapper around torch.nn.functional.pad to facilitate the handling of reflection padding when the input tensor is small. If the input length is less than or equal to the maximum padding size, extra zero padding is added to ensure the reflection operation can be performed without errors.
- Parameters:
- x (torch.Tensor) – The input tensor of shape (…, length).
- paddings (Tuple *[*int , int ]) – A tuple specifying the padding sizes for the left and right sides, respectively.
- mode (str , optional) – The padding mode to use. Supported modes are: ‘zero’, ‘reflect’, ‘replicate’, and ‘circular’. Default is ‘zero’.
- value (float , optional) – The value to use for padding when mode is ‘zero’. Default is 0.0.
- Returns: The padded tensor.
- Return type: torch.Tensor
- Raises:AssertionError – If the padding values are negative.
Examples
>>> import torch
>>> x = torch.tensor([1, 2, 3, 4])
>>> padded = pad1d(x, (2, 2), mode='zero')
>>> print(padded)
tensor([0, 0, 1, 2, 3, 4, 0, 0])
>>> padded_reflect = pad1d(x, (2, 2), mode='reflect')
>>> print(padded_reflect)
tensor([3, 4, 3, 2, 1, 2, 3, 4])