1D noncausal convolution w/ 2-sides padding.
| 19 | |
| 20 | |
| 21 | class NonCausalConv1d(nn.Module): |
| 22 | """1D noncausal convolution w/ 2-sides padding.""" |
| 23 | |
| 24 | def __init__( |
| 25 | self, |
| 26 | in_channels, |
| 27 | out_channels, |
| 28 | kernel_size, |
| 29 | stride=1, |
| 30 | padding=-1, |
| 31 | dilation=1, |
| 32 | groups=1, |
| 33 | bias=True): |
| 34 | super().__init__() |
| 35 | self.in_channels = in_channels |
| 36 | self.out_channels = out_channels |
| 37 | self.kernel_size = kernel_size |
| 38 | if padding < 0: |
| 39 | padding = (kernel_size - 1) // 2 * dilation |
| 40 | self.dilation = dilation |
| 41 | self.conv = nn.Conv1d( |
| 42 | in_channels=in_channels, |
| 43 | out_channels=out_channels, |
| 44 | kernel_size=kernel_size, |
| 45 | stride=stride, |
| 46 | padding=padding, |
| 47 | dilation=dilation, |
| 48 | groups=groups, |
| 49 | bias=bias, |
| 50 | ) |
| 51 | |
| 52 | def forward(self, x): |
| 53 | """ |
| 54 | Args: |
| 55 | x (Tensor): Float tensor variable with the shape (B, C, T). |
| 56 | Returns: |
| 57 | Tensor: Float tensor variable with the shape (B, C, T). |
| 58 | """ |
| 59 | x = self.conv(x) |
| 60 | return x |
| 61 | |
| 62 | |
| 63 | class NonCausalConvTranspose1d(nn.Module): |