| 249 | return x + self.pe[:, :, 0:x.shape[2]] |
| 250 | |
| 251 | class Encoder(nn.Module): |
| 252 | def __init__(self, num_layers, r1, r2, num_f_maps, input_dim, num_classes, channel_masking_rate, att_type, alpha): |
| 253 | super(Encoder, self).__init__() |
| 254 | self.conv_1x1 = nn.Conv1d(input_dim, num_f_maps, 1) # fc layer |
| 255 | self.layers = nn.ModuleList( |
| 256 | [AttModule(2 ** i, num_f_maps, num_f_maps, r1, r2, att_type, 'encoder', alpha) for i in # 2**i |
| 257 | range(num_layers)]) |
| 258 | |
| 259 | self.conv_out = nn.Conv1d(num_f_maps, num_classes, 1) |
| 260 | self.dropout = nn.Dropout2d(p=channel_masking_rate) |
| 261 | self.channel_masking_rate = channel_masking_rate |
| 262 | |
| 263 | def forward(self, x, mask): |
| 264 | ''' |
| 265 | :param x: (N, C, L) |
| 266 | :param mask: |
| 267 | :return: |
| 268 | ''' |
| 269 | |
| 270 | if self.channel_masking_rate > 0: |
| 271 | x = x.unsqueeze(2) |
| 272 | x = self.dropout(x) |
| 273 | x = x.squeeze(2) |
| 274 | |
| 275 | feature = self.conv_1x1(x) |
| 276 | for layer in self.layers: |
| 277 | feature = layer(feature, None, mask) |
| 278 | |
| 279 | out = self.conv_out(feature) * mask[:, 0:1, :] |
| 280 | |
| 281 | return out, feature |
| 282 | |
| 283 | |
| 284 | class Decoder(nn.Module): |