MCPcopy Create free account
hub / github.com/NeuSpeech/EEG-To-Text / PositionalEncoding

Class PositionalEncoding

model_decoding.py:203–222  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

201
202# from https://pytorch.org/tutorials/beginner/transformer_tutorial.html
203class PositionalEncoding(nn.Module):
204
205 def __init__(self, d_model, dropout=0.1, max_len=5000):
206 super(PositionalEncoding, self).__init__()
207 self.dropout = nn.Dropout(p=dropout)
208
209 pe = torch.zeros(max_len, d_model)
210 position = torch.arange(0, max_len, dtype=torch.float).unsqueeze(1)
211 div_term = torch.exp(torch.arange(0, d_model, 2).float() * (-math.log(10000.0) / d_model))
212 pe[:, 0::2] = torch.sin(position * div_term)
213 pe[:, 1::2] = torch.cos(position * div_term)
214 pe = pe.unsqueeze(0).transpose(0, 1)
215 self.register_buffer('pe', pe)
216
217 def forward(self, x):
218 # print('[DEBUG] input size:', x.size())
219 # print('[DEBUG] positional embedding size:', self.pe.size())
220 x = x + self.pe[:x.size(0), :]
221 # print('[DEBUG] output x with pe size:', x.size())
222 return self.dropout(x)
223
224
225""" Miscellaneous (not working well) """

Callers 1

__init__Method · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected