/
layers.py
164 lines (123 loc) · 5.75 KB
/
layers.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
from torch import nn
from constants import *
import torch
import math
class EncoderLayer(nn.Module):
def __init__(self):
super().__init__()
self.layer_norm_1 = LayerNormalization()
self.multihead_attention = MultiheadAttention()
self.drop_out_1 = nn.Dropout(drop_out_rate)
self.layer_norm_2 = LayerNormalization()
self.feed_forward = FeedFowardLayer()
self.drop_out_2 = nn.Dropout(drop_out_rate)
def forward(self, x, e_mask):
x_1 = self.layer_norm_1(x) # (B, L, d_model)
x = x + self.drop_out_1(
self.multihead_attention(x_1, x_1, x_1, mask=e_mask)
) # (B, L, d_model)
x_2 = self.layer_norm_2(x) # (B, L, d_model)
x = x + self.drop_out_2(self.feed_forward(x_2)) # (B, L, d_model)
return x # (B, L, d_model)
class DecoderLayer(nn.Module):
def __init__(self):
super().__init__()
self.layer_norm_1 = LayerNormalization()
self.masked_multihead_attention = MultiheadAttention()
self.drop_out_1 = nn.Dropout(drop_out_rate)
self.layer_norm_2 = LayerNormalization()
self.multihead_attention = MultiheadAttention()
self.drop_out_2 = nn.Dropout(drop_out_rate)
self.layer_norm_3 = LayerNormalization()
self.feed_forward = FeedFowardLayer()
self.drop_out_3 = nn.Dropout(drop_out_rate)
def forward(self, x, e_output, e_mask, d_mask):
x_1 = self.layer_norm_1(x) # (B, L, d_model)
x = x + self.drop_out_1(
self.masked_multihead_attention(x_1, x_1, x_1, mask=d_mask)
) # (B, L, d_model)
x_2 = self.layer_norm_2(x) # (B, L, d_model)
x = x + self.drop_out_2(
self.multihead_attention(x_2, e_output, e_output, mask=e_mask)
) # (B, L, d_model)
x_3 = self.layer_norm_3(x) # (B, L, d_model)
x = x + self.drop_out_3(self.feed_forward(x_3)) # (B, L, d_model)
return x # (B, L, d_model)
class MultiheadAttention(nn.Module):
def __init__(self):
super().__init__()
self.inf = 1e9
# W^Q, W^K, W^V in the paper
self.w_q = nn.Linear(d_model, d_model)
self.w_k = nn.Linear(d_model, d_model)
self.w_v = nn.Linear(d_model, d_model)
self.dropout = nn.Dropout(drop_out_rate)
self.attn_softmax = nn.Softmax(dim=-1)
# Final output linear transformation
self.w_0 = nn.Linear(d_model, d_model)
def forward(self, q, k, v, mask=None):
input_shape = q.shape
# Linear calculation + split into num_heads
q = self.w_q(q).view(input_shape[0], -1, num_heads, d_k) # (B, L, num_heads, d_k)
k = self.w_k(k).view(input_shape[0], -1, num_heads, d_k) # (B, L, num_heads, d_k)
v = self.w_v(v).view(input_shape[0], -1, num_heads, d_k) # (B, L, num_heads, d_k)
# For convenience, convert all tensors in size (B, num_heads, L, d_k)
q = q.transpose(1, 2)
k = k.transpose(1, 2)
v = v.transpose(1, 2)
# Conduct self-attention
attn_values = self.self_attention(q, k, v, mask=mask) # (B, num_heads, L, d_k)
concat_output = attn_values.transpose(1, 2)\
.contiguous().view(input_shape[0], -1, d_model) # (B, L, d_model)
return self.w_0(concat_output)
def self_attention(self, q, k, v, mask=None):
# Calculate attention scores with scaled dot-product attention
attn_scores = torch.matmul(q, k.transpose(-2, -1)) # (B, num_heads, L, L)
attn_scores = attn_scores / math.sqrt(d_k)
# If there is a mask, make masked spots -INF
if mask is not None:
mask = mask.unsqueeze(1) # (B, 1, L) => (B, 1, 1, L) or (B, L, L) => (B, 1, L, L)
attn_scores = attn_scores.masked_fill_(mask == 0, -1 * self.inf)
# Softmax and multiplying K to calculate attention value
attn_distribs = self.attn_softmax(attn_scores)
attn_distribs = self.dropout(attn_distribs)
attn_values = torch.matmul(attn_distribs, v) # (B, num_heads, L, d_k)
return attn_values
class FeedFowardLayer(nn.Module):
def __init__(self):
super().__init__()
self.linear_1 = nn.Linear(d_model, d_ff, bias=True)
self.relu = nn.ReLU()
self.linear_2 = nn.Linear(d_ff, d_model, bias=True)
self.dropout = nn.Dropout(drop_out_rate)
def forward(self, x):
x = self.relu(self.linear_1(x)) # (B, L, d_ff)
x = self.dropout(x)
x = self.linear_2(x) # (B, L, d_model)
return x
class LayerNormalization(nn.Module):
def __init__(self, eps=1e-6):
super().__init__()
self.eps = eps
self.layer = nn.LayerNorm([d_model], elementwise_affine=True, eps=self.eps)
def forward(self, x):
x = self.layer(x)
return x
class PositionalEncoder(nn.Module):
def __init__(self):
super().__init__()
# Make initial positional encoding matrix with 0
pe_matrix= torch.zeros(seq_len, d_model) # (L, d_model)
# Calculating position encoding values
for pos in range(seq_len):
for i in range(d_model):
if i % 2 == 0:
pe_matrix[pos, i] = math.sin(pos / (10000 ** (2 * i / d_model)))
elif i % 2 == 1:
pe_matrix[pos, i] = math.cos(pos / (10000 ** (2 * i / d_model)))
pe_matrix = pe_matrix.unsqueeze(0) # (1, L, d_model)
self.positional_encoding = pe_matrix.to(device=device).requires_grad_(False)
def forward(self, x):
x = x * math.sqrt(d_model) # (B, L, d_model)
x = x + self.positional_encoding # (B, L, d_model)
return x