MCPcopy Create free account
hub / github.com/ChinaYi/ASFormer / PositionalEncoding

Class PositionalEncoding

model.py:232–249  ·  view source on GitHub ↗

Implement the PE function.

Source from the content-addressed store, hash-verified

230
231
232class 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
251class Encoder(nn.Module):
252 def __init__(self, num_layers, r1, r2, num_f_maps, input_dim, num_classes, channel_masking_rate, att_type, alpha):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected