Implement the PE function.
| 230 | |
| 231 | |
| 232 | class PositionalEncoding(nn.Module): |
| 233 | "Implement the PE function." |
| 234 | |
| 235 | def __init__(self, d_model, max_len=10000): |
| 236 | super(PositionalEncoding, self).__init__() |
| 237 | # Compute the positional encodings once in log space. |
| 238 | pe = torch.zeros(max_len, d_model) |
| 239 | position = torch.arange(0, max_len).unsqueeze(1) |
| 240 | div_term = torch.exp(torch.arange(0, d_model, 2) * |
| 241 | -(math.log(10000.0) / d_model)) |
| 242 | pe[:, 0::2] = torch.sin(position * div_term) |
| 243 | pe[:, 1::2] = torch.cos(position * div_term) |
| 244 | pe = pe.unsqueeze(0).permute(0,2,1) # of shape (1, d_model, l) |
| 245 | self.pe = nn.Parameter(pe, requires_grad=True) |
| 246 | # self.register_buffer('pe', pe) |
| 247 | |
| 248 | def forward(self, x): |
| 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): |
nothing calls this directly
no outgoing calls
no test coverage detected