-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
220 lines (163 loc) · 7.02 KB
/
model.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import copy
import math
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
'''
num_epochs = 1 # Number of training epochs
d_model = 128 # dimension in encoder
heads = 4 # number of heads in multi-head attention
N = 2 # number of encoder layers
m = 14 # number of features
'''
class Transformer(nn.Module):
def __init__(self, m, d_model, N, heads, dropout):
super().__init__()
self.gating = Gating(d_model, m)
self.encoder = Encoder(d_model, N, heads, m, dropout)
self.out = nn.Linear(d_model, 1)
def forward(self, src, t):
e_i = self.gating(src)
e_outputs = self.encoder(e_i, t)
output = self.out(e_outputs)
return output.reshape(1)
class Gating(nn.Module):
def __init__(self, d_model, m): # 128,14
super().__init__()
self.m = m
# the reset gate r_i
self.W_r = nn.Parameter(torch.Tensor(m, m))
self.V_r = nn.Parameter(torch.Tensor(m, m))
self.b_r = nn.Parameter(torch.Tensor(m))
# the update gate u_i
self.W_u = nn.Parameter(torch.Tensor(m, m))
self.V_u = nn.Parameter(torch.Tensor(m, m))
self.b_u = nn.Parameter(torch.Tensor(m))
# the output
self.W_e = nn.Parameter(torch.Tensor(m, d_model))
self.b_e = nn.Parameter(torch.Tensor(d_model))
self.init_weights()
self.cnn_layers = nn.Sequential(
nn.Conv2d(1, 1, kernel_size=(3, 1), stride=1),
)
def init_weights(self):
stdv = 1.0 / math.sqrt(self.m)
for weight in self.parameters():
weight.data.uniform_(-stdv, stdv)
def forward(self, x):
x_i = x[:, :, 1:2, :] #only applying the gating on the current row even with the stack of 3 rows cames as input (1,1,3,14)
h_i = self.cnn_layers(x) # shape becomes 1,1,1,14 as the nn.conv2d has output channel as 1 but the convolution is applied on whole past input (stack of three)
r_i = torch.sigmoid(torch.matmul(h_i, self.W_r) + torch.matmul(x_i, self.V_r) + self.b_r)
u_i = torch.sigmoid(torch.matmul(h_i, self.W_u) + torch.matmul(x_i, self.V_u) + self.b_u)
# the output of the gating mechanism
hh_i = torch.mul(h_i, u_i) + torch.mul(x_i, r_i)
return torch.matmul(hh_i, self.W_e) + self.b_e # (the final output is 1,1,1,128 as the encoder has size of 128.)
class Encoder(nn.Module):
def __init__(self, d_model, N, heads, m, dropout): #d_model = 128 # dimension in encoder, heads = 4 #number of heads in multi-head attention, N = 2 #encoder layers, m = 14 #number of features
super().__init__()
self.N = N
# self.embed = Embedder(vocab_size, d_model)
self.pe = PositionalEncoder(d_model)
self.layers = get_clones(EncoderLayer(d_model, heads, dropout), N)
self.norm = Norm(d_model)
self.d_model = d_model
def forward(self, src, t):
src = src.reshape(1, self.d_model) # this 128 is changed according to d_model
x = self.pe(src, t)
for i in range(self.N):
x = self.layers[i](x, None)
return self.norm(x)
class PositionalEncoder(nn.Module):
def __init__(self, d_model):
super().__init__()
self.d_model = d_model
def forward(self, x, t):
# make embeddings relatively larger
x = x * math.sqrt(self.d_model)
pe = np.zeros(self.d_model)
for i in range(0, self.d_model, 2):
pe[i] = math.sin(t / (10000 ** ((2 * i) / self.d_model)))
pe[i + 1] = math.cos(t / (10000 ** ((2 * (i + 1)) / self.d_model)))
x = x + Variable(torch.Tensor(pe))
return x
# We can then build a convenient cloning function that can generate multiple layers:
def get_clones(module, N):
return nn.ModuleList([copy.deepcopy(module) for i in range(N)])
# build an encoder layer with one multi-head attention layer and one # feed-forward layer
class EncoderLayer(nn.Module):
def __init__(self, d_model, heads, dropout=0.5):
super().__init__()
self.norm_1 = Norm(d_model)
self.norm_2 = Norm(d_model)
self.attn = MultiHeadAttention(heads, d_model, dropout)
self.ff = FeedForward(d_model)
self.dropout_1 = nn.Dropout(dropout)
self.dropout_2 = nn.Dropout(dropout)
def forward(self, x, mask):
x2 = self.norm_1(x)
x = x + self.dropout_1(self.attn(x2, x2, x2, mask))
x2 = self.norm_2(x)
x = x + self.dropout_2(self.ff(x2))
return x
class Norm(nn.Module):
def __init__(self, d_model, eps=1e-6):
super().__init__()
self.size = d_model
# create two learnable parameters to calibrate normalisation
self.alpha = nn.Parameter(torch.ones(self.size))
self.bias = nn.Parameter(torch.zeros(self.size))
self.eps = eps
def forward(self, x):
norm = self.alpha * (x - x.mean(dim=-1, keepdim=True)) / (x.std(dim=-1, keepdim=True) + self.eps) + self.bias
return norm
class MultiHeadAttention(nn.Module):
def __init__(self, heads, d_model, dropout=0.5):
super().__init__()
self.d_model = d_model
self.d_k = d_model // heads
self.h = heads
self.q_linear = nn.Linear(d_model, d_model)
self.v_linear = nn.Linear(d_model, d_model)
self.k_linear = nn.Linear(d_model, d_model)
self.dropout = nn.Dropout(dropout)
self.out = nn.Linear(d_model, d_model)
def forward(self, q, k, v, mask=None):
bs = q.size(0)
# perform linear operation and split into h heads
k = self.k_linear(k).view(bs, -1, self.h, self.d_k)
q = self.q_linear(q).view(bs, -1, self.h, self.d_k)
v = self.v_linear(v).view(bs, -1, self.h, self.d_k)
# transpose to get dimensions bs * h * sl * d_model
k = k.transpose(1, 2)
q = q.transpose(1, 2)
v = v.transpose(1, 2)
# calculate attention using function we will define next
scores = attention(q, k, v, self.d_k, mask, self.dropout)
# concatenate heads and put through final linear layer
concat = scores.transpose(1, 2).contiguous() \
.view(bs, -1, self.d_model)
output = self.out(concat)
return output
def attention(q, k, v, d_k, mask=None, dropout=None):
scores = torch.matmul(q, k.transpose(-2, -1)) / math.sqrt(d_k)
if mask is not None:
mask = mask.unsqueeze(1)
# scores = scores.masked_fill(mask == 0, -1e9)
scores = F.softmax(scores, dim=-1)
if dropout is not None:
scores = dropout(scores)
output = torch.matmul(scores, v)
return output
class FeedForward(nn.Module):
def __init__(self, d_model, d_ff=512, dropout=0.5):
super().__init__()
# set d_ff as a default to 512
self.linear_1 = nn.Linear(d_model, d_ff)
self.dropout = nn.Dropout(dropout)
self.linear_2 = nn.Linear(d_ff, d_model)
def forward(self, x):
x = self.dropout(F.relu(self.linear_1(x)))
x = self.linear_2(x)
return x