espnet2.gan_codec.shared.encoder.seanet_2d.NormConv2d
espnet2.gan_codec.shared.encoder.seanet_2d.NormConv2d
class espnet2.gan_codec.shared.encoder.seanet_2d.NormConv2d(*args, causal: bool = False, norm: str = 'none', norm_kwargs: Dict[str, Any] = {}, **kwargs)
Bases: Module
Wrapper around Conv2d with normalization to provide a uniform interface.
This class encapsulates a 2D convolutional layer along with a normalization layer, allowing for various normalization techniques to be applied seamlessly. The user can specify the type of normalization to be used and provide any additional parameters required for that normalization.
conv
The convolutional layer with optional weight normalization.
- Type: nn.Module
norm
The normalization layer applied to the output of the convolution.
- Type: nn.Module
norm
The type of normalization applied.
Type: str
Parameters:
- *args – Variable length argument list to be passed to nn.Conv2d.
- causal (bool , optional) – If True, applies causal convolution. Defaults to False.
- norm (str , optional) – The type of normalization to apply. Defaults to “none”.
- norm_kwargs (Dict *[*str , Any ] , optional) – Additional keyword arguments for the normalization layer. Defaults to an empty dictionary.
####### Examples
>>> layer = NormConv2d(1, 32, kernel_size=(3, 3), norm='batch_norm')
>>> input_tensor = torch.randn(1, 1, 64, 64)
>>> output_tensor = layer(input_tensor)
>>> output_tensor.shape
torch.Size([1, 32, 62, 62])
Initialize internal Module state, shared by both nn.Module and ScriptModule.
forward(x)
Applies the convolution and normalization to the input tensor.
This method takes an input tensor x, applies the convolutional layer, followed by the normalization layer, and returns the output tensor.
- 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, T is the time dimension.
- Returns: The output tensor after applying the convolution and normalization, with the same shape as the input tensor.
- Return type: torch.Tensor
- Raises:AssertionError – If the input tensor does not have 4 dimensions.
####### Examples
>>> norm_conv = NormConv2d(in_channels=1, out_channels=2, kernel_size=3)
>>> input_tensor = torch.randn(4, 1, 16, 16) # Batch size of 4
>>> output_tensor = norm_conv(input_tensor)
>>> output_tensor.shape
torch.Size([4, 2, 14, 14]) # Output shape after conv and norm