-
Notifications
You must be signed in to change notification settings - Fork 9
/
transformer.py
495 lines (352 loc) · 16.7 KB
/
transformer.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
import torch
from torch import nn
import torch.functional as F
import numpy as np
import matplotlib.pyplot as plt
import os
from torch.utils.data import Dataset, DataLoader
from fra_eng_dataset import FraEngDataset, fra_eng_dataset_collate
from torch.nn.utils.rnn import pad_sequence, pack_padded_sequence, pad_packed_sequence
device = 'cpu'
if torch.cuda.is_available():
device = 'cuda'
class SelfAttentionHead(nn.Module):
def __init__(self, d_model):
super().__init__()
self.d_model = d_model
self.K = nn.Linear(d_model, d_model)
self.V = nn.Linear(d_model, d_model)
self.Q = nn.Linear(d_model, d_model)
def forward(self, x, padding_mask = None, subsq_mask = None):
# src shape: [N, SEQ, D_MODEL]
#SelfAttention:
keys = self.K.forward(x)
values = self.V.forward(x)
queries = self.Q.forward(x)
sqrt_d = self.d_model ** 0.5
att = torch.matmul(queries, keys.transpose(1,2)) / sqrt_d
# att shape: [N, SEQ, SEQ]
# Broadcast padding mask to word attentions so that word attention does not attend to positions outside the sentence
if padding_mask is not None:
att = att + padding_mask.transpose(1,2)
# Add subsequent mask so that each position can attend only itself and the previous elements
if subsq_mask is not None:
att = att + subsq_mask.unsqueeze(0)
att_softmax = torch.softmax(att, dim=2)
# shape: [N, SEQ, SEQ]
att_out = torch.matmul(att_softmax, values)
# shape: [N, SEQ, D_MODEL]
return att_out, keys, values
class MemAttentionHead(nn.Module):
def __init__(self, d_model):
super().__init__()
self.d_model = d_model
self.Q = nn.Linear(d_model, d_model)
def forward(self, x, mem_padding_mask, keys = None, values = None):
# X shape: [N, SEQ, D_MODEL]
queries = self.Q.forward(x)
sqrt_d = self.d_model ** 0.5
att = torch.matmul(queries, keys.transpose(1,2)) / sqrt_d
# att shape: [N, SEQ_TGT, SEQ_SRC]
# Broadcast padding mask to word attentions so that word attention does not attend to positions outside the source sentence
if mem_padding_mask is not None:
att = att + mem_padding_mask.transpose(1,2)
att_softmax = torch.softmax(att, dim=2)
# shape: [N, SEQ_TGT, SEQ_SRC]
att_out = torch.matmul(att_softmax, values)
# shape: [N, SEQ_TGT, D_MODEL]
return att_out
class MultiHeadSelfAttention(nn.Module):
def __init__(self, d_model, num_heads):
super().__init__()
self.num_heads = num_heads
self.heads = nn.ModuleList([SelfAttentionHead(d_model) for i in range(num_heads)])
self.linear = nn.Linear(num_heads * d_model, d_model)
def forward(self, src, src_padding_mask, src_subsq_mask):
out_cat = None
keys = None
values = None
for i in range(self.num_heads):
head_outp, keys, values = self.heads[i].forward(src, src_padding_mask, src_subsq_mask)
if i == 0:
out_cat = head_outp
else:
out_cat = torch.cat([out_cat, head_outp], dim=2)
ret = self.linear.forward(out_cat)
return ret, keys, values
class MultiHeadMemAttention(nn.Module):
def __init__(self, d_model, num_heads):
super().__init__()
self.num_heads = num_heads
self.heads = nn.ModuleList([MemAttentionHead(d_model) for i in range(num_heads)])
self.linear = nn.Linear(num_heads * d_model, d_model)
def forward(self, src, src_padding_mask, keys, values):
out_cat = None
for i in range(self.num_heads):
head_outp = self.heads[i].forward(src, src_padding_mask, keys = keys, values = values)
if i == 0:
out_cat = head_outp
else:
out_cat = torch.cat([out_cat, head_outp], dim=2)
ret = self.linear.forward(out_cat)
return ret
class EncoderLayer(nn.Module):
def __init__(self, d_model, num_att_heads, ff_dim = 2048, dropout = 0.1):
super().__init__()
self.multihead_attention = MultiHeadSelfAttention(d_model, num_att_heads)
self.dropout1 = nn.Dropout(dropout)
self.att_sublayer_norm = torch.nn.LayerNorm(d_model)
self.linear1 = nn.Linear(d_model, ff_dim)
self.relu = nn.ReLU()
self.dropout_lin = nn.Dropout(dropout)
self.linear2 = nn.Linear(ff_dim, d_model)
self.dropout2 = nn.Dropout(dropout)
self.lin_sublayer_norm = torch.nn.LayerNorm(d_model)
def forward(self, src, src_padding_mask, src_subsq_mask):
residual_1 = src
x, keys, values = self.multihead_attention.forward(src, src_padding_mask, src_subsq_mask)
x = self.att_sublayer_norm.forward(residual_1 + self.dropout1(x))
residual_2 = x
x = self.linear2(self.dropout_lin(self.relu(self.linear1.forward(x))))
x = self.lin_sublayer_norm(residual_2 + self.dropout2(x))
return x, keys, values
class DecoderLayer(nn.Module):
def __init__(self, d_model, num_att_heads, ff_dim = 2048, dropout = 0.1):
super().__init__()
self.multihead_self_attention = MultiHeadSelfAttention(d_model, num_att_heads)
self.self_att_sublayer_norm = torch.nn.LayerNorm(d_model)
self.dropout1 = nn.Dropout(dropout)
self.multihead_mem_attention = MultiHeadMemAttention(d_model, num_att_heads)
self.mem_att_sublayer_norm = torch.nn.LayerNorm(d_model)
self.dropout2 = nn.Dropout(dropout)
self.linear1 = nn.Linear(d_model, ff_dim)
self.dropout_lin = nn.Dropout(dropout)
self.relu = nn.ReLU()
self.linear2 = nn.Linear(ff_dim, d_model)
self.lin_sublayer_norm = torch.nn.LayerNorm(d_model)
self.dropout3 = nn.Dropout(dropout)
def forward(self, x, src_padding_mask, tgt_padding_mask, tgt_subsq_mask, mem_keys, mem_values):
residual_1 = x
x, keys, values = self.multihead_self_attention.forward(x, tgt_padding_mask, tgt_subsq_mask)
x = self.self_att_sublayer_norm.forward(residual_1 + self.dropout1(x))
residual_2 = x
x = self.multihead_mem_attention.forward(x, src_padding_mask, keys = mem_keys, values = mem_values)
x = self.mem_att_sublayer_norm.forward(residual_2 + self.dropout2(x))
residual_3 = x
x = self.linear2(self.dropout_lin(self.relu(self.linear1.forward(x))))
x = self.lin_sublayer_norm(residual_3 + self.dropout3(x))
return x
class Encoder(nn.Module):
def __init__(self, num_layers, d_model, num_att_heads):
super().__init__()
self.layers = nn.ModuleList([EncoderLayer(d_model, num_att_heads) for i in range(num_layers)])
self.norm = nn.LayerNorm(d_model)
def forward(self,src, src_padding_mask, src_subsq_mask):
x = src
keys = None
values = None
for layer in self.layers:
x, keys, values = layer.forward(x, src_padding_mask, src_subsq_mask)
x = self.norm.forward(x)
return keys, values
class Decoder(nn.Module):
def __init__(self, num_layers, d_model, num_att_heads):
super().__init__()
self.layers = nn.ModuleList([DecoderLayer(d_model, num_att_heads) for i in range(num_layers)])
self.norm = nn.LayerNorm(d_model)
def forward(self, tgt, src_padding_mask, tgt_padding_mask, tgt_subsq_mask, mem_keys, mem_values):
x = tgt
for layer in self.layers:
x = layer.forward(x, src_padding_mask, tgt_padding_mask, tgt_subsq_mask, mem_keys, mem_values)
x = self.norm.forward(x)
return x
class PositionalEncoding(nn.Module):
def __init__(self, d_model):
super().__init__()
self.d_model = d_model
self.sin_args = torch.zeros(1, self.d_model).to(device)
self.cos_args = torch.zeros(1, self.d_model).to(device)
for i in range(self.d_model//2):
self.sin_args[0,i * 2] = 10000**(2.*i/self.d_model)
self.cos_args[0,i * 2 + 1] = 10000**(2.*i/self.d_model)
self.sin_args_filled = (self.sin_args > 1e-10).float()
self.sin_args = self.sin_args + (self.sin_args < 1e-10).float()
self.cos_args_filled = (self.cos_args > 1e-10).float()
self.cos_args = self.cos_args + (self.cos_args < 1e-10).float()
def forward(self, x):
for pos in range(x.size()[-2]):
x[:,pos,:] = x[:,pos,:] + \
torch.sin(pos / self.sin_args) * self.sin_args_filled + \
torch.cos(pos / self.cos_args) * self.cos_args_filled
return x
# positional_enc = PositionalEncoding(256)
# data = torch.zeros(1, 50, 256)
# data_pos_enc = positional_enc.forward(data)
#
# enc_np = data_pos_enc.squeeze(dim=0).numpy()
# plt.imshow(enc_np)
# plt.show()
class Transformer(nn.Module):
def __init__(self, num_layers, d_model, num_att_heads, input_dict_size, output_dict_size):
super().__init__()
self.max_sent_len = 50
self.input_emb = nn.Embedding(input_dict_size, d_model)
self.outp_emb = nn.Embedding(output_dict_size, d_model)
self.positional_encoder = PositionalEncoding(d_model)
self.encoder = Encoder(num_layers, d_model, num_att_heads)
self.decoder = Decoder(num_layers, d_model, num_att_heads)
self.outp_logits = nn.Linear(d_model, output_dict_size)
self.softmax = nn.Softmax(dim=2)
def translate(self, src, tgt_start_code, tgt_eos_code, src_padding_mask, src_subsq_mask):
# TODO: Beam search
enc_x = self.input_emb.forward(src.squeeze(dim=2))
enc_x = self.positional_encoder.forward(enc_x)
enc_keys, enc_values = self.encoder.forward(enc_x, src_padding_mask, src_subsq_mask)
snt = torch.ones((1,1,1)) * tgt_start_code
snt = snt.long()
snt = snt.to(device)
translation_idxes = []
for idx in range(self.max_sent_len):
dec_x = self.outp_emb.forward(snt.squeeze(dim=2))
dec_x = self.positional_encoder.forward(dec_x)
dec_x = self.decoder.forward(
dec_x,
src_padding_mask = src_padding_mask,
tgt_padding_mask = torch.zeros_like(snt).float().to(device),
tgt_subsq_mask = get_square_subsequent_mask(snt.size()[1]),
mem_keys = enc_keys,
mem_values = enc_values
)
dec_x = self.outp_logits.forward(dec_x)
dec_x = self.softmax(dec_x)
next_word_softmax = dec_x[0,idx,:].to('cpu').detach()
next_word_idx = torch.argmax(next_word_softmax)
snt = torch.cat([snt, torch.ones((1,1,1)).long().to(device) * next_word_idx], dim=1)
translation_idxes.append(next_word_idx)
if next_word_idx == tgt_eos_code:
break
return translation_idxes
def forward(self, src, tgt, src_padding_mask, src_subsq_mask, tgt_padding_mask, tgt_subsq_mask):
enc_x = self.input_emb.forward(src.squeeze(dim=2))
enc_x = self.positional_encoder.forward(enc_x)
enc_keys, enc_values = self.encoder.forward(enc_x, src_padding_mask, src_subsq_mask)
dec_x = self.outp_emb.forward(tgt.squeeze(dim=2))
dec_x = self.positional_encoder.forward(dec_x)
dec_x = self.decoder.forward(dec_x, src_padding_mask, tgt_padding_mask, tgt_subsq_mask, enc_keys, enc_values)
dec_x = self.outp_logits.forward(dec_x)
dec_x = self.softmax(dec_x)
return dec_x
BATCH_SIZE = 32
LEARNING_RATE = 1e-4
EPOCHS = 100
STORE_MODELS = True
models_path = 'models'
if not os.path.exists(models_path):
os.mkdir(models_path)
dataset = FraEngDataset()
sentences_loader = DataLoader(dataset, batch_size=BATCH_SIZE, shuffle=True, drop_last=True, collate_fn=fra_eng_dataset_collate)
in_dict_size = dataset.get_fra_dict_size()
out_dict_size = dataset.get_eng_dict_size()
transformer_model = Transformer(
num_layers=6,
d_model=256,
num_att_heads=8,
input_dict_size=in_dict_size,
output_dict_size=out_dict_size
).to(device)
def get_square_subsequent_mask(seq_len):
mask = (torch.triu(torch.ones(seq_len, seq_len).to(device)) == 1).transpose(0, 1)
mask = mask.float().masked_fill(mask == 0, float('-inf')).masked_fill(mask == 1, float(0.0))
return mask
def get_padding_mask(input, val1 = float('-inf'), val2 = float(0.0)):
mask = torch.ones(input.size()).to(device)
mask = mask.float().masked_fill(input == 0, val1).masked_fill(input > 0, val2)
return mask
def get_one_hot(x, out_dim, mask):
tens = x.view(-1)
tens_one_hot = torch.zeros(list(tens.size()) + [out_dim]).to(device)
for i in range(len(tens)):
tens_one_hot[i,tens[i]] = 1
tens_one_hot = tens_one_hot.view(list(x.size()) + [out_dim])
tens_one_hot = tens_one_hot * mask
return tens_one_hot.to(device)
def translate_sentences(src_sentences, tgt_sentences, max_sent_num = 15):
transformer_model.eval()
with torch.no_grad():
for snt_idx in range(len(src_sentences)):
if snt_idx > max_sent_num:
break
src = src_sentences[snt_idx:snt_idx+1]
padded_src = pad_sequence(src, padding_value=0, batch_first=True).to(device)
src_padding_mask = get_padding_mask(padded_src)
src_subsq_mask = get_square_subsequent_mask(padded_src.size()[1])
snt_translation = transformer_model.translate(
src = padded_src,
tgt_start_code = dataset.get_eng_start_code(),
tgt_eos_code = dataset.get_eng_eos_code(),
src_padding_mask = src_padding_mask,
src_subsq_mask = src_subsq_mask
)
src_sent = ''
for word_idx in src_sentences[snt_idx]:
src_sent = f"{src_sent} {dataset.fra_token_to_text[word_idx]}"
tgt_sent = ''
for word_idx in tgt_sentences[snt_idx]:
tgt_sent = f"{tgt_sent} {dataset.eng_token_to_text[word_idx]}"
translated_sent = ''
for word_idx in snt_translation:
translated_sent = f"{translated_sent} {dataset.eng_token_to_text[word_idx]}"
print(f"Source sentence is: {src_sent}")
print(f"Target sentence is: {tgt_sent}")
print(f"Model translation is: {translated_sent}")
transformer_model.train()
optimizer = torch.optim.Adam(transformer_model.parameters(), lr = 1e-4)
batch_count = 0
for epoch in range(EPOCHS):
train_loss_sum = 0.0
total_word_count = 0.0
for sentences in sentences_loader:
src_sentences = sentences['fra_sentences']
tgt_sentences = sentences['eng_sentences']
batch_count+=1
if batch_count == 10:
batch_count = 0
translate_sentences(src_sentences,tgt_sentences)
continue
tgt_sentences_out = []
for idx in range(len(tgt_sentences)):
tgt_sentences_out.append(tgt_sentences[idx][1:])
tgt_sentences[idx] = tgt_sentences[idx][:-1]
# Create tensors from token lists
padded_src = pad_sequence(src_sentences, padding_value=0, batch_first=True).to(device)
padded_tgt = pad_sequence(tgt_sentences, padding_value=0, batch_first=True).to(device)
padded_tgt_out = pad_sequence(tgt_sentences_out, padding_value=0, batch_first=True).to(device)
src_padding_mask = get_padding_mask(padded_src)
src_subsq_mask = get_square_subsequent_mask(padded_src.size()[1])
tgt_padding_mask = get_padding_mask(padded_tgt)
tgt_subsq_mask = get_square_subsequent_mask(padded_tgt.size()[1])
pred = transformer_model.forward(
src=padded_src,
tgt=padded_tgt,
src_padding_mask=src_padding_mask,
src_subsq_mask=src_subsq_mask,
tgt_padding_mask=tgt_padding_mask,
tgt_subsq_mask=tgt_subsq_mask
)
# Mask to zero one hot vectors corresponding to padded elements
one_hot_mask = get_padding_mask(padded_tgt_out, val1=float(0.0), val2=float(1.0))
y_one_hot = get_one_hot(padded_tgt_out.squeeze(dim=2), out_dict_size, mask=one_hot_mask)
loss = - torch.sum(torch.log(pred) * y_one_hot)
print(loss)
loss.backward()
optimizer.step()
optimizer.zero_grad()
train_loss_sum += loss.detach().to('cpu').data
total_word_count += torch.sum(y_one_hot).to('cpu').data
print(f"Epoch {epoch} " + '=' * 60)
print(f"Total loss per word: {train_loss_sum / total_word_count}")
print(f"Some generated sentences:")
if STORE_MODELS == True:
model_path = os.path.join(models_path, f'Epoch_{epoch}_model.pt')
torch.save(transformer_model, model_path)
print("Traing done! Generating some more sentences.")