Autoformer decoder layer with the progressive decomposition architecture
| 168 | |
| 169 | |
| 170 | class DecoderLayer(nn.Module): |
| 171 | """ |
| 172 | Autoformer decoder layer with the progressive decomposition architecture |
| 173 | """ |
| 174 | def __init__(self, self_attention, cross_attention, d_model, c_out, d_ff=None, |
| 175 | moving_avg=25, dropout=0.1, activation="relu"): |
| 176 | super(DecoderLayer, self).__init__() |
| 177 | d_ff = d_ff or 4 * d_model |
| 178 | self.self_attention = self_attention |
| 179 | self.cross_attention = cross_attention |
| 180 | self.conv1 = nn.Conv1d(in_channels=d_model, out_channels=d_ff, kernel_size=1, bias=False) |
| 181 | self.conv2 = nn.Conv1d(in_channels=d_ff, out_channels=d_model, kernel_size=1, bias=False) |
| 182 | |
| 183 | if isinstance(moving_avg, list): |
| 184 | self.decomp1 = series_decomp_multi(moving_avg) |
| 185 | self.decomp2 = series_decomp_multi(moving_avg) |
| 186 | self.decomp3 = series_decomp_multi(moving_avg) |
| 187 | else: |
| 188 | self.decomp1 = series_decomp(moving_avg) |
| 189 | self.decomp2 = series_decomp(moving_avg) |
| 190 | self.decomp3 = series_decomp(moving_avg) |
| 191 | |
| 192 | self.dropout = nn.Dropout(dropout) |
| 193 | self.projection = nn.Conv1d(in_channels=d_model, out_channels=c_out, kernel_size=3, stride=1, padding=1, |
| 194 | padding_mode='circular', bias=False) |
| 195 | self.activation = F.relu if activation == "relu" else F.gelu |
| 196 | |
| 197 | def forward(self, x, cross, x_mask=None, cross_mask=None): |
| 198 | x = x + self.dropout(self.self_attention( |
| 199 | x, x, x, |
| 200 | attn_mask=x_mask |
| 201 | )[0]) |
| 202 | |
| 203 | x, trend1 = self.decomp1(x) |
| 204 | x = x + self.dropout(self.cross_attention( |
| 205 | x, cross, cross, |
| 206 | attn_mask=cross_mask |
| 207 | )[0]) |
| 208 | |
| 209 | x, trend2 = self.decomp2(x) |
| 210 | y = x |
| 211 | y = self.dropout(self.activation(self.conv1(y.transpose(-1, 1)))) |
| 212 | y = self.dropout(self.conv2(y).transpose(-1, 1)) |
| 213 | x, trend3 = self.decomp3(x + y) |
| 214 | |
| 215 | residual_trend = trend1 + trend2 + trend3 |
| 216 | residual_trend = self.projection(residual_trend.permute(0, 2, 1)).transpose(1, 2) |
| 217 | return x, residual_trend |
| 218 | |
| 219 | |
| 220 | class Decoder(nn.Module): |