-
Notifications
You must be signed in to change notification settings - Fork 2
/
lgg_model.py
301 lines (244 loc) · 10.7 KB
/
lgg_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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import torch
from torch import dropout, nn
from torch.nn import functional as F
import numpy as np
import datetime as dt
from torch.autograd import Variable
import math
import copy
# from torch.utils.tensorboard import SummaryWriter
device=torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
def softmax(x):
return np.exp(x) / np.sum(np.exp(x))
# Layer Normalization
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
# 'vanilla' baseline model
class vanilla_LSTM(nn.Module):
def __init__(self, words_num, embedding_dim, hidden_size, num_layers):
super().__init__()
self.hidden_size = hidden_size
self.num_layers = num_layers
self.Embedding = nn.Embedding(num_embeddings=words_num, embedding_dim=embedding_dim)
self.LSTM = nn.LSTM(embedding_dim, hidden_size, num_layers, batch_first=True)
self.Linear = nn.Linear(hidden_size, words_num)
def forward(self, data):
data = self.Embedding(data)
h0 = torch.zeros(self.num_layers, data.shape[0], self.hidden_size, device=device)
c0 = torch.zeros(self.num_layers, data.shape[0], self.hidden_size, device=device)
data, (_, _) = self.LSTM(data, (h0, c0))
out = self.Linear(data)
return out
# the enhanced version is based on the vanilla one above (add something)
# dropout & layernorm
class LSTM_enhanced(nn.Module):
def __init__(self, words_num, embedding_dim, hidden_size, num_layers, dropout=0.5):
super().__init__()
self.hidden_size = hidden_size
self.num_layers = num_layers
self.Embedding = nn.Embedding(num_embeddings=words_num, embedding_dim=embedding_dim)
# add dropout to LSTM module
self.LSTM = nn.LSTM(embedding_dim, hidden_size, num_layers, batch_first=True, dropout=dropout)
# add layer normalization
# self.Norm = Norm(hidden_size)
self.Linear = nn.Linear(hidden_size, words_num)
# self.attn = nn.MultiheadAttention(hidden_size, num_heads=8)
def forward(self, data):
data = self.Embedding(data)
# data, _ = self.attn(data, data, data) # attention
h0 = torch.zeros(self.num_layers, data.shape[0], self.hidden_size, device=device)
c0 = torch.zeros(self.num_layers, data.shape[0], self.hidden_size, device=device)
data, (_, _) = self.LSTM(data, (h0, c0))
# data = self.Norm(data)
out = self.Linear(data)
return out
# 2022/2/28 reduce the power of the vanilla_LSTM model, hoping to reduce the overfitting
# too much dropout makes it hard to converge!!!
class vanilla_GRU(nn.Module):
def __init__(self, words_num, embedding_dim, hidden_size, num_layers):
super().__init__()
self.hidden_size = hidden_size
self.num_layers = num_layers
self.Embedding = nn.Embedding(num_embeddings=words_num, embedding_dim=embedding_dim)
self.GRU = nn.GRU(embedding_dim, hidden_size, num_layers, batch_first=True, dropout=0.5)
self.Linear = nn.Linear(hidden_size, words_num)
def forward(self, data):
data = self.Embedding(data)
a0 = torch.zeros(self.num_layers, data.shape[0], self.hidden_size, device=device)
data, _ = self.GRU(data, a0)
out = self.Linear(data)
return out
######
# Tune the dropout rate by yourself, and add layer norm
class GRU_enhanced(nn.Module):
def __init__(self, words_num, embedding_dim, hidden_size, num_layers, dropout=0.5):
super().__init__()
self.hidden_size = hidden_size
self.num_layers = num_layers
self.Embedding = nn.Embedding(num_embeddings=words_num, embedding_dim=embedding_dim)
self.GRU = nn.GRU(embedding_dim, hidden_size, num_layers, batch_first=True, dropout=dropout)
self.Linear = nn.Linear(hidden_size, words_num)
self.norm = Norm(hidden_size)
self.attn = nn.MultiheadAttention(hidden_size, num_heads=8)
def forward(self, data):
data = self.Embedding(data)
data, _ = self.attn(data, data, data)
a0 = torch.zeros(self.num_layers, data.shape[0], self.hidden_size, device=device)
data, _ = self.GRU(data, a0)
# add layer norm
data = self.norm(data)
out = self.Linear(data)
return out
################ Transformer Decoder copied online ################
################ Haven't tried this part yet ######################
################ Reference: https://medium.com/towards-data-science/how-to-code-the-transformer-in-pytorch-24db27c8f9ec
class Embedder(nn.Module):
def __init__(self, vocab_size, d_model):
super().__init__()
self.embed = nn.Embedding(vocab_size, d_model)
def forward(self, src):
return self.embed(src)
class PositionalEncoder(nn.Module):
def __init__(self, d_model, max_seq_len = 80):
super().__init__()
self.d_model = d_model
# create constant 'pe' matrix with values dependant on
# pos and i
pe = torch.zeros(max_seq_len, d_model)
for pos in range(max_seq_len):
for i in range(0, d_model, 2):
pe[pos, i] = \
math.sin(pos / (10000 ** ((2 * i)/d_model)))
pe[pos, i + 1] = \
math.cos(pos / (10000 ** ((2 * (i + 1))/d_model)))
pe = pe.unsqueeze(0)
self.register_buffer('pe', pe)
def forward(self, x):
# make embeddings relatively larger
x = x * math.sqrt(self.d_model)
#add constant to embedding
seq_len = x.size(1)
x = x + Variable(self.pe[:,:seq_len], \
requires_grad=False).to(device)
return x
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 MultiHeadAttention(nn.Module):
def __init__(self, heads, d_model, dropout = 0.1):
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
class FeedForward(nn.Module):
def __init__(self, d_model, d_ff=2048, dropout = 0.1):
super().__init__()
# We set d_ff as a default to 2048
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
# layernorm
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
# build a decoder layer with two multi-head attention layers and
# one feed-forward layer
class DecoderLayer(nn.Module):
def __init__(self, d_model, heads, dropout=0.1):
super().__init__()
self.norm_1 = Norm(d_model)
self.norm_2 = Norm(d_model)
self.norm_3 = Norm(d_model)
self.dropout_1 = nn.Dropout(dropout)
self.dropout_2 = nn.Dropout(dropout)
self.dropout_3 = nn.Dropout(dropout)
self.attn_1 = MultiHeadAttention(heads, d_model)
self.attn_2 = MultiHeadAttention(heads, d_model)
self.ff = FeedForward(d_model).to(device)
def forward(self, x, e_outputs, src_mask, trg_mask):
x2 = self.norm_1(x)
x = x + self.dropout_1(self.attn_1(x2, x2, x2, trg_mask))
x2 = self.norm_2(x)
x = x + self.dropout_2(self.attn_2(x2, e_outputs, e_outputs,
src_mask))
x2 = self.norm_3(x)
x = x + self.dropout_3(self.ff(x2))
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)])
## The final decoder
class Decoder(nn.Module):
def __init__(self, vocab_size, d_model, N, heads):
super().__init__()
self.N = N
self.embed = Embedder(vocab_size, d_model)
self.pe = PositionalEncoder(d_model)
self.layers = get_clones(DecoderLayer(d_model, heads), N)
self.norm = Norm(d_model)
def forward(self, trg, e_outputs, src_mask, trg_mask):
x = self.embed(trg)
x = self.pe(x)
for i in range(self.N):
x = self.layers[i](x, e_outputs, src_mask, trg_mask)
return self.norm(x)
########################## END ###############################
if __name__ == "__main__":
NET = LSTM_enhanced(3061, 256, 256, 3, 0.1)
input = torch.ones((32, 10), dtype=torch.long)
output = NET(input)
# writer = SummaryWriter("graph")
# writer.add_graph(NET, input)
# writer.close()