| 46 | |
| 47 | |
| 48 | class PositionalEncoding(nn.Module): |
| 49 | |
| 50 | def __init__(self, d_model, max_len=300): |
| 51 | super(PositionalEncoding, self).__init__() |
| 52 | |
| 53 | pe = torch.zeros(max_len, d_model) |
| 54 | position = torch.arange(0, max_len, dtype=torch.float).unsqueeze(1) |
| 55 | div_term = torch.exp(torch.arange(0, d_model, 2).float() * (-math.log(10000.0) / d_model)) |
| 56 | pe[:, 0::2] = torch.sin(position * div_term) |
| 57 | pe[:, 1::2] = torch.cos(position * div_term) |
| 58 | # pe = pe.unsqueeze(0).transpose(0, 1) |
| 59 | self.register_buffer('pe', pe) |
| 60 | |
| 61 | def forward(self, pos): |
| 62 | return self.pe[pos] |
| 63 | |
| 64 | |
| 65 | @SUBMODULES.register_module() |
nothing calls this directly
no outgoing calls
no test coverage detected