Autoformer encoder layer with the progressive decomposition architecture
| 51 | |
| 52 | |
| 53 | class EncoderLayer(nn.Module): |
| 54 | """ |
| 55 | Autoformer encoder layer with the progressive decomposition architecture |
| 56 | """ |
| 57 | def __init__(self, attention, d_model, d_ff=None, moving_avg=25, dropout=0.1, activation="relu"): |
| 58 | super(EncoderLayer, self).__init__() |
| 59 | d_ff = d_ff or 4 * d_model |
| 60 | self.attention = attention |
| 61 | self.conv1 = nn.Conv1d(in_channels=d_model, out_channels=d_ff, kernel_size=1, bias=False) |
| 62 | self.conv2 = nn.Conv1d(in_channels=d_ff, out_channels=d_model, kernel_size=1, bias=False) |
| 63 | self.decomp1 = series_decomp(moving_avg) |
| 64 | self.decomp2 = series_decomp(moving_avg) |
| 65 | self.dropout = nn.Dropout(dropout) |
| 66 | self.activation = F.relu if activation == "relu" else F.gelu |
| 67 | |
| 68 | def forward(self, x, attn_mask=None): |
| 69 | new_x, attn = self.attention( |
| 70 | x, x, x, |
| 71 | attn_mask=attn_mask |
| 72 | ) |
| 73 | x = x + self.dropout(new_x) |
| 74 | x, _ = self.decomp1(x) |
| 75 | y = x |
| 76 | y = self.dropout(self.activation(self.conv1(y.transpose(-1, 1)))) |
| 77 | y = self.dropout(self.conv2(y).transpose(-1, 1)) |
| 78 | res, _ = self.decomp2(x + y) |
| 79 | return res, attn |
| 80 | |
| 81 | |
| 82 | class Encoder(nn.Module): |