Autoformer decoder layer with the progressive decomposition architecture
| 129 | |
| 130 | |
| 131 | class DecoderLayer(nn.Module): |
| 132 | """ |
| 133 | Autoformer decoder layer with the progressive decomposition architecture |
| 134 | """ |
| 135 | def __init__(self, self_attention, cross_attention, d_model, c_out, d_ff=None, |
| 136 | moving_avg=25, dropout=0.1, activation="relu"): |
| 137 | super(DecoderLayer, self).__init__() |
| 138 | d_ff = d_ff or 4 * d_model |
| 139 | self.self_attention = self_attention |
| 140 | self.cross_attention = cross_attention |
| 141 | self.conv1 = nn.Conv1d(in_channels=d_model, out_channels=d_ff, kernel_size=1, bias=False) |
| 142 | self.conv2 = nn.Conv1d(in_channels=d_ff, out_channels=d_model, kernel_size=1, bias=False) |
| 143 | self.decomp1 = series_decomp(moving_avg) |
| 144 | self.decomp2 = series_decomp(moving_avg) |
| 145 | self.decomp3 = series_decomp(moving_avg) |
| 146 | self.dropout = nn.Dropout(dropout) |
| 147 | self.projection = nn.Conv1d(in_channels=d_model, out_channels=c_out, kernel_size=3, stride=1, padding=1, |
| 148 | padding_mode='circular', bias=False) |
| 149 | self.activation = F.relu if activation == "relu" else F.gelu |
| 150 | |
| 151 | def forward(self, x, cross, x_mask=None, cross_mask=None): |
| 152 | x = x + self.dropout(self.self_attention( |
| 153 | x, x, x, |
| 154 | attn_mask=x_mask |
| 155 | )[0]) |
| 156 | x, trend1 = self.decomp1(x) |
| 157 | x = x + self.dropout(self.cross_attention( |
| 158 | x, cross, cross, |
| 159 | attn_mask=cross_mask |
| 160 | )[0]) |
| 161 | x, trend2 = self.decomp2(x) |
| 162 | y = x |
| 163 | y = self.dropout(self.activation(self.conv1(y.transpose(-1, 1)))) |
| 164 | y = self.dropout(self.conv2(y).transpose(-1, 1)) |
| 165 | x, trend3 = self.decomp3(x + y) |
| 166 | |
| 167 | residual_trend = trend1 + trend2 + trend3 |
| 168 | residual_trend = self.projection(residual_trend.permute(0, 2, 1)).transpose(1, 2) |
| 169 | return x, residual_trend |
| 170 | |
| 171 | |
| 172 | class Decoder(nn.Module): |