espnet2.gan_codec.shared.encoder.seanet.NormConv1d
espnet2.gan_codec.shared.encoder.seanet.NormConv1d
class espnet2.gan_codec.shared.encoder.seanet.NormConv1d(*args, causal: bool = False, norm: str = 'none', norm_kwargs: Dict[str, Any] = {}, **kwargs)
Bases: Module
Wrapper around Conv1d and normalization applied to this conv to provide a uniform interface across normalization approaches.
This class applies a 1D convolution followed by a specified normalization method, allowing flexibility in the choice of normalization technique while maintaining a consistent interface.
conv
The convolutional layer with optional parameterization normalization.
- Type: nn.Module
norm
The normalization layer applied after the convolution.
- Type: nn.Module
norm
The type of normalization used.
Type: str
Parameters:
- *args – Variable length argument list for the Conv1d layer.
- causal (bool , optional) – Whether to use causal convolution. Defaults to False.
- norm (str , optional) – The normalization method to apply. Options include “none”, “weight_norm”, “spectral_norm”, “time_layer_norm”, “layer_norm”, “time_group_norm”. Defaults to “none”.
- norm_kwargs (Dict *[*str , Any ] , optional) – Additional keyword arguments for the normalization layer. Defaults to an empty dictionary.
####### Examples
>>> norm_conv = NormConv1d(in_channels=16, out_channels=33,
... kernel_size=3, causal=True,
... norm="layer_norm")
>>> input_tensor = torch.randn(10, 16, 50) # (batch_size, channels, length)
>>> output_tensor = norm_conv(input_tensor)
>>> output_tensor.shape
torch.Size([10, 33, 48]) # Output shape after convolution
Initialize internal Module state, shared by both nn.Module and ScriptModule.
forward(x)
Applies the convolution followed by the normalization to the input.
This method takes an input tensor x, applies a 1D convolution using the defined Conv1d layer, and subsequently normalizes the result using the specified normalization technique. It provides a unified interface for various normalization approaches.
- Parameters:x (torch.Tensor) – The input tensor of shape (batch_size, in_channels, sequence_length) to be processed.
- Returns: The output tensor after applying the convolution and : normalization, with the same shape as the input tensor.
- Return type: torch.Tensor
####### Examples
>>> conv_layer = NormConv1d(in_channels=1, out_channels=2, kernel_size=3)
>>> input_tensor = torch.randn(4, 1, 10) # (batch_size=4, channels=1, length=10)
>>> output_tensor = conv_layer(input_tensor)
>>> output_tensor.shape
torch.Size([4, 2, 8]) # The output shape will vary based on kernel size and stride
NOTE
Ensure that the input tensor is of the correct shape and type. The method expects a 3D tensor and will raise an error if the input does not conform to this requirement.