MCPcopy Create free account
hub / github.com/Xuehao-Gao/GUESS / PositionalEncoding

Class PositionalEncoding

mld/models/operator/position_encoding_layer.py:6–30  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4
5
6class PositionalEncoding(nn.Module):
7
8 def __init__(self, d_model, dropout=0.1, max_len=5000, batch_first=False):
9 super().__init__()
10 self.batch_first = batch_first
11
12 self.dropout = nn.Dropout(p=dropout)
13
14 pe = torch.zeros(max_len, d_model)
15 position = torch.arange(0, max_len, dtype=torch.float).unsqueeze(1)
16 div_term = torch.exp(torch.arange(
17 0, d_model, 2).float() * (-np.log(10000.0) / d_model))
18 pe[:, 0::2] = torch.sin(position * div_term)
19 pe[:, 1::2] = torch.cos(position * div_term)
20 pe = pe.unsqueeze(0).transpose(0, 1)
21
22 self.register_buffer("pe", pe)
23
24 def forward(self, x):
25 # not used in the final model
26 if self.batch_first:
27 x = x + self.pe.permute(1, 0, 2)[:, : x.shape[1], :]
28 else:
29 x = x + self.pe[: x.shape[0], :]
30 return self.dropout(x)

Callers 5

__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected