Autoformer encoder layer with the progressive decomposition architecture
| 103 | |
| 104 | |
| 105 | class EncoderLayer(nn.Module): |
| 106 | """ |
| 107 | Autoformer encoder layer with the progressive decomposition architecture |
| 108 | """ |
| 109 | def __init__(self, attention, d_model, d_ff=None, moving_avg=25, dropout=0.1, activation="relu"): |
| 110 | super(EncoderLayer, self).__init__() |
| 111 | d_ff = d_ff or 4 * d_model |
| 112 | self.attention = attention |
| 113 | self.conv1 = nn.Conv1d(in_channels=d_model, out_channels=d_ff, kernel_size=1, bias=False) |
| 114 | self.conv2 = nn.Conv1d(in_channels=d_ff, out_channels=d_model, kernel_size=1, bias=False) |
| 115 | |
| 116 | if isinstance(moving_avg, list): |
| 117 | self.decomp1 = series_decomp_multi(moving_avg) |
| 118 | self.decomp2 = series_decomp_multi(moving_avg) |
| 119 | else: |
| 120 | self.decomp1 = series_decomp(moving_avg) |
| 121 | self.decomp2 = series_decomp(moving_avg) |
| 122 | |
| 123 | self.dropout = nn.Dropout(dropout) |
| 124 | self.activation = F.relu if activation == "relu" else F.gelu |
| 125 | |
| 126 | def forward(self, x, attn_mask=None): |
| 127 | new_x, attn = self.attention( |
| 128 | x, x, x, |
| 129 | attn_mask=attn_mask |
| 130 | ) |
| 131 | x = x + self.dropout(new_x) |
| 132 | x, _ = self.decomp1(x) |
| 133 | y = x |
| 134 | y = self.dropout(self.activation(self.conv1(y.transpose(-1, 1)))) |
| 135 | y = self.dropout(self.conv2(y).transpose(-1, 1)) |
| 136 | res, _ = self.decomp2(x + y) |
| 137 | return res, attn |
| 138 | |
| 139 | |
| 140 | class Encoder(nn.Module): |