espnet2.gan_codec.shared.encoder.seanet_2d.pad2d
Less than 1 minute
espnet2.gan_codec.shared.encoder.seanet_2d.pad2d
espnet2.gan_codec.shared.encoder.seanet_2d.pad2d(x: Tensor, paddings: Tuple[Tuple[int, int], Tuple[int, int]], mode: str = 'zero', value: float = 0.0)
Applies padding to a 2D tensor with an option for reflective padding.
This function is a wrapper around torch.nn.functional.pad. It allows for both zero and reflective padding. When reflective padding is requested, additional zero padding is added if the input dimensions are smaller than the specified padding sizes.
- Parameters:
- x (torch.Tensor) – The input tensor of shape (B, C, F, T), where B is the batch size, C is the number of channels, F is the frequency dimension, and T is the time dimension.
- paddings (Tuple *[*Tuple *[*int , int ] , Tuple *[*int , int ] ]) – A tuple of two tuples specifying the padding for the time and frequency dimensions respectively. Each inner tuple contains two integers, the amount of padding before and after the dimension.
- mode (str , optional) – The mode of padding. Can be “zero” for zero padding or “reflect” for reflective padding. Defaults to “zero”.
- value (float , optional) – The value to use for zero padding when mode is “zero”. Defaults to 0.0.
- Returns: The padded tensor.
- Return type: torch.Tensor
- Raises:AssertionError – If any of the padding values are negative.
Examples
>>> x = torch.randn(1, 1, 4, 4)
>>> paddings = ((1, 1), (2, 2))
>>> padded_tensor = pad2d(x, paddings, mode="zero")
>>> print(padded_tensor.shape)
torch.Size([1, 1, 6, 8])
>>> padded_tensor_reflect = pad2d(x, paddings, mode="reflect")
>>> print(padded_tensor_reflect.shape)
torch.Size([1, 1, 6, 8])