espnet2.gan_codec.shared.encoder.seanet.SConv1d
espnet2.gan_codec.shared.encoder.seanet.SConv1d
class espnet2.gan_codec.shared.encoder.seanet.SConv1d(in_channels: int, out_channels: int, kernel_size: int, stride: int = 1, dilation: int = 1, groups: int = 1, bias: bool = True, causal: bool = False, norm: str = 'none', norm_kwargs: Dict[str, Any] = {}, pad_mode: str = 'reflect')
Bases: Module
Conv1d with built-in handling of asymmetric or causal padding and normalization.
This module is designed to facilitate convolution operations with options for different padding strategies and normalization methods. It automatically computes necessary padding based on the specified convolution parameters, ensuring that the output shape aligns with the expected dimensions, particularly in causal scenarios.
conv
A normalized 1D convolution layer.
- Type:NormConv1d
causal
Indicates if causal padding should be used.
- Type: bool
pad_mode
Specifies the padding mode (‘reflect’, ‘zero’, etc.).
Type: str
Parameters:
- in_channels (int) – Number of input channels.
- out_channels (int) – Number of output channels.
- kernel_size (int) – Size of the convolution kernel.
- stride (int , optional) – Stride of the convolution. Default is 1.
- dilation (int , optional) – Dilation of the convolution. Default is 1.
- groups (int , optional) – Number of blocked connections. Default is 1.
- bias (bool , optional) – If True, adds a learnable bias to the output. Default is True.
- causal (bool , optional) – If True, applies causal padding. Default is False.
- norm (str , optional) – Normalization method. Default is ‘none’.
- norm_kwargs (Dict *[*str , Any ] , optional) – Additional arguments for normalization.
- pad_mode (str , optional) – Padding mode for the convolutions. Default is ‘reflect’.
Raises:ValueError – If both stride and dilation are greater than 1.
####### Examples
>>> conv = SConv1d(in_channels=16, out_channels=33, kernel_size=3)
>>> input_tensor = torch.randn(20, 16, 50) # (batch_size, channels, time)
>>> output_tensor = conv(input_tensor)
>>> output_tensor.shape
torch.Size([20, 33, 48]) # Output shape with default parameters
NOTE
The effective kernel size is calculated by taking dilation into account. For causal convolutions, left padding is applied to maintain causality.
Initialize internal Module state, shared by both nn.Module and ScriptModule.
forward(x)
Forward pass for the SConv1d module.
This method processes the input tensor through a 1D convolutional layer, applies necessary padding, and normalizes the output if specified. It handles both causal and non-causal convolutions and manages the padding appropriately based on the convolution parameters.
- Parameters:x (torch.Tensor) – Input tensor of shape (B, C, T), where B is the batch size, C is the number of channels, and T is the sequence length.
- Returns: The output tensor after applying the convolution and : normalization, with the same shape as the input tensor if padding is correctly applied.
- Return type: torch.Tensor
####### Examples
>>> model = SConv1d(in_channels=1, out_channels=2, kernel_size=3)
>>> input_tensor = torch.randn(5, 1, 10) # Batch size of 5, 1 channel, length 10
>>> output_tensor = model(input_tensor)
>>> output_tensor.shape
torch.Size([5, 2, 10]) # Output shape after convolution and normalization