espnet2.gan_codec.shared.decoder.seanet_2d.unpad2d
Less than 1 minute
espnet2.gan_codec.shared.decoder.seanet_2d.unpad2d
espnet2.gan_codec.shared.decoder.seanet_2d.unpad2d(x: Tensor, paddings: Tuple[Tuple[int, int], Tuple[int, int]])
Remove padding from a 2D tensor, ensuring proper handling of zero padding.
This function takes a 2D tensor x and a tuple of padding values, and returns the tensor with the specified paddings removed. It ensures that the padding values are valid and that the resulting dimensions are appropriate after unpadding.
- Parameters:
- x (torch.Tensor) – The input tensor of shape (…, height, width).
- paddings (Tuple *[*Tuple *[*int , int ] , Tuple *[*int , int ] ]) – A tuple specifying the paddings to be removed. The first element corresponds to the (top, bottom) padding for the height dimension, and the second element corresponds to the (left, right) padding for the width dimension.
- Returns: The tensor with the specified padding removed.
- Return type: torch.Tensor
- Raises:
- AssertionError – If any padding values are negative or exceed the dimensions
- of the tensor. –
Examples
>>> import torch
>>> x = torch.randn(1, 3, 10, 10) # A random tensor of shape (1, 3, 10, 10)
>>> paddings = ((2, 2), (3, 3)) # Remove 2 rows from top and bottom,
... # and 3 columns from left and right
>>> unpadded_x = unpad2d(x, paddings)
>>> unpadded_x.shape
torch.Size([1, 3, 6, 4]) # The resulting shape after unpadding