generated from gursi26/paper-implementation-template
/
other_modules.py
49 lines (38 loc) · 1.4 KB
/
other_modules.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from torch import nn
import torch, math
import matplotlib.pyplot as plt
class PositionalEncoder(nn.Module):
def __init__(self, d_model, max_seq_len, p=0.1):
super(PositionalEncoder, self).__init__()
position = torch.arange(max_seq_len).unsqueeze(1)
div_term = torch.exp(torch.arange(0, d_model, 2) * (-math.log(10000.0) / d_model))
pe = torch.zeros(1, max_seq_len, d_model)
pe[0, :, 0::2] = torch.sin(position * div_term)
pe[0, :, 1::2] = torch.cos(position * div_term)
self.register_buffer('pe', pe)
self.dropout = nn.Dropout(p=p)
# x has shape [batch, seq_len, embed_dim]
def forward(self, x):
_, seq_len, _ = x.shape
out = self.dropout(x + self.pe[:, :seq_len, :])
return out
class FeedForward(nn.Module):
def __init__(self, input_dim):
super(FeedForward, self).__init__()
self.layer = nn.Sequential(
nn.Linear(input_dim, input_dim * 4),
nn.ReLU(),
nn.Linear(input_dim * 4, input_dim)
)
def forward(self, x):
return self.layer(x)
def test_positional_encoder():
pe = PositionalEncoder(512, 2048)
out = pe(torch.randn(32, 100, 512))
print(out.shape)
plt.figure(figsize=(20, 10))
plt.title("Positional encoding")
plt.imshow(pe.pe[0].T, cmap="Greys")
plt.show()
if __name__ == "__main__":
test_positional_encoder()