Residual block module in WaveNet.
| 37 | |
| 38 | |
| 39 | class ResidualBlock(torch.nn.Module): |
| 40 | """Residual block module in WaveNet.""" |
| 41 | |
| 42 | def __init__(self, |
| 43 | kernel_size=3, |
| 44 | residual_channels=64, |
| 45 | gate_channels=128, |
| 46 | skip_channels=64, |
| 47 | aux_channels=80, |
| 48 | dropout=0.0, |
| 49 | dilation=1, |
| 50 | bias=True, |
| 51 | use_causal_conv=False |
| 52 | ): |
| 53 | """Initialize ResidualBlock module. |
| 54 | |
| 55 | Args: |
| 56 | kernel_size (int): Kernel size of dilation convolution layer. |
| 57 | residual_channels (int): Number of channels for residual connection. |
| 58 | skip_channels (int): Number of channels for skip connection. |
| 59 | aux_channels (int): Local conditioning channels i.e. auxiliary input dimension. |
| 60 | dropout (float): Dropout probability. |
| 61 | dilation (int): Dilation factor. |
| 62 | bias (bool): Whether to add bias parameter in convolution layers. |
| 63 | use_causal_conv (bool): Whether to use use_causal_conv or non-use_causal_conv convolution. |
| 64 | |
| 65 | """ |
| 66 | super(ResidualBlock, self).__init__() |
| 67 | self.dropout = dropout |
| 68 | # no future time stamps available |
| 69 | if use_causal_conv: |
| 70 | padding = (kernel_size - 1) * dilation |
| 71 | else: |
| 72 | assert (kernel_size - 1) % 2 == 0, "Not support even number kernel size." |
| 73 | padding = (kernel_size - 1) // 2 * dilation |
| 74 | self.use_causal_conv = use_causal_conv |
| 75 | |
| 76 | # dilation conv |
| 77 | self.conv = Conv1d(residual_channels, gate_channels, kernel_size, |
| 78 | padding=padding, dilation=dilation, bias=bias) |
| 79 | |
| 80 | # local conditioning |
| 81 | if aux_channels > 0: |
| 82 | self.conv1x1_aux = Conv1d1x1(aux_channels, gate_channels, bias=False) |
| 83 | else: |
| 84 | self.conv1x1_aux = None |
| 85 | |
| 86 | # conv output is split into two groups |
| 87 | gate_out_channels = gate_channels // 2 |
| 88 | self.conv1x1_out = Conv1d1x1(gate_out_channels, residual_channels, bias=bias) |
| 89 | self.conv1x1_skip = Conv1d1x1(gate_out_channels, skip_channels, bias=bias) |
| 90 | |
| 91 | def forward(self, x, c): |
| 92 | """Calculate forward propagation. |
| 93 | |
| 94 | Args: |
| 95 | x (Tensor): Input tensor (B, residual_channels, T). |
| 96 | c (Tensor): Local conditioning auxiliary tensor (B, aux_channels, T). |