-
Notifications
You must be signed in to change notification settings - Fork 11
/
Annotated_Transformer_English_to_Chinese_Translator.py
821 lines (673 loc) · 28 KB
/
Annotated_Transformer_English_to_Chinese_Translator.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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
#!/usr/bin/env python
# coding: utf-8
'''
# # Annotated_Transformer_English_to_Chinese_Translator
Created on :2020/09/20 20:58:18
@author :Caihao (Chris) Cui
@file :Annotated_Transformer_English_to_Chinese_Translator.py
@content :
@version :0.0
'''
# In this notebook, I will build a Transformer model (neural network model) to translate English sentence to Chinese sentences.
#
# The reference paper is [Attention Is All You Need](https://arxiv.org/pdf/1706.03762.pdf)
# %% 1. Understand the Transformer Model
#
# The Whole Transformer encoder-decoder model architecture services for the following purposes.
# - Encoder(s): the encoding process transforms the input sentence (list of English words) into numeric matrix format (embedding ), consider this step is to extract useful and necessary information for the decoder. In Fig 06, the embedding is represented by the green matrix.
#
# - Decoder(s): then the decoding process mapping these embeddings back to another language sequence as Fig 06 shown, which helps us to solve all kinds of supervised NLP tasks, like machine translation (in this blog), sentiment classification, entity recognition, summary generation, semantic relation extraction and so on.
#
# ![Understand_How_Transformer_Work](./document/images/Understand_How_Transformer_Work.png)
#
#
# %% 2. Encoder
#
# We will focus on the structure of the encoder in this section, because after understanding the structure of the encoder, understanding the decoder will be very simple. Moreover we can just use the encoder to complete some of the mainstream tasks in NLP, such as sentiment classification, semantic relationship analysis, named entity recognition and so on.
#
# Recall that the Encoder denotes the process of mapping natural language sequences to mathematical expressions to hidden layers outputs.
# **Here is a Transformer Encoder Block structure**
# > Notification: the following sections will refer to the 1,2,3,4 blocks.
#
# ![Transformer Encoder Stacks](./document/images/encoder.png)
# %% 2.0 Data Preparation: English-to-Chinese Translator Data
# import seaborn as sns
import time
import math
import copy
from collections import Counter
import numpy as np
from nltk import word_tokenize
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
# init parameters
UNK = 0 # unknown word-id
PAD = 1 # padding word-id
BATCH_SIZE = 64
DEBUG = True # Debug / Learning Purposes.
# DEBUG = False # Build the model, better with GPU CUDA enabled.
if DEBUG:
EPOCHS = 2
LAYERS = 3
H_NUM = 8
D_MODEL = 128
D_FF = 256
DROPOUT = 0.1
MAX_LENGTH = 60
TRAIN_FILE = 'data/nmt/en-cn/train_mini.txt'
DEV_FILE = 'data/nmt/en-cn/dev_mini.txt'
SAVE_FILE = 'save/models/model.pt'
else:
EPOCHS = 20
LAYERS = 6
H_NUM = 8
D_MODEL = 256
D_FF = 1024
DROPOUT = 0.1
MAX_LENGTH = 60
TRAIN_FILE = 'data/nmt/en-cn/train.txt'
DEV_FILE = 'data/nmt/en-cn/dev.txt'
SAVE_FILE = 'save/models/large_model.pt'
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
def seq_padding(X, padding=0):
"""
add padding to a batch data
"""
L = [len(x) for x in X]
ML = max(L)
return np.array([
np.concatenate([x, [padding] * (ML - len(x))]) if len(x) < ML else x for x in X
])
class PrepareData:
def __init__(self, train_file, dev_file):
# 01. Read the data and tokenize
self.train_en, self.train_cn = self.load_data(train_file)
self.dev_en, self.dev_cn = self.load_data(dev_file)
# 02. build dictionary: English and Chinese
self.en_word_dict, self.en_total_words, self.en_index_dict = self.build_dict(
self.train_en)
self.cn_word_dict, self.cn_total_words, self.cn_index_dict = self.build_dict(
self.train_cn)
# 03. word to id by dictionary
self.train_en, self.train_cn = self.wordToID(
self.train_en, self.train_cn, self.en_word_dict, self.cn_word_dict)
self.dev_en, self.dev_cn = self.wordToID(
self.dev_en, self.dev_cn, self.en_word_dict, self.cn_word_dict)
# 04. batch + padding + mask
self.train_data = self.splitBatch(
self.train_en, self.train_cn, BATCH_SIZE)
self.dev_data = self.splitBatch(self.dev_en, self.dev_cn, BATCH_SIZE)
def load_data(self, path):
"""
Read English and Chinese Data
tokenize the sentence and add start/end marks(Begin of Sentence; End of Sentence)
en = [['BOS', 'i', 'love', 'you', 'EOS'],
['BOS', 'me', 'too', 'EOS'], ...]
cn = [['BOS', '我', '爱', '你', 'EOS'],
['BOS', '我', '也', '是', 'EOS'], ...]
"""
en = []
cn = []
with open(path, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip().split('\t')
en.append(["BOS"] + word_tokenize(line[0].lower()) + ["EOS"])
cn.append(
["BOS"] + word_tokenize(" ".join([w for w in line[1]])) + ["EOS"])
return en, cn
def build_dict(self, sentences, max_words=50000):
"""
sentences: list of word list
build dictionary as {key(word): value(id)}
"""
word_count = Counter()
for sentence in sentences:
for s in sentence:
word_count[s] += 1
ls = word_count.most_common(max_words)
total_words = len(ls) + 2
word_dict = {w[0]: index + 2 for index, w in enumerate(ls)}
word_dict['UNK'] = UNK
word_dict['PAD'] = PAD
# inverted index: {key(id): value(word)}
index_dict = {v: k for k, v in word_dict.items()}
return word_dict, total_words, index_dict
def wordToID(self, en, cn, en_dict, cn_dict, sort=True):
"""
convert input/output word lists to id lists.
Use input word list length to sort, reduce padding.
"""
out_en_ids = [[en_dict.get(w, 0) for w in sent] for sent in en]
out_cn_ids = [[cn_dict.get(w, 0) for w in sent] for sent in cn]
def len_argsort(seq):
"""
get sorted index w.r.t length.
"""
return sorted(range(len(seq)), key=lambda x: len(seq[x]))
if sort: # update index
sorted_index = len_argsort(out_en_ids) # English
out_en_ids = [out_en_ids[id] for id in sorted_index]
out_cn_ids = [out_cn_ids[id] for id in sorted_index]
return out_en_ids, out_cn_ids
def splitBatch(self, en, cn, batch_size, shuffle=True):
"""
get data into batches
"""
idx_list = np.arange(0, len(en), batch_size)
if shuffle:
np.random.shuffle(idx_list)
batch_indexs = []
for idx in idx_list:
batch_indexs.append(np.arange(idx, min(idx + batch_size, len(en))))
batches = []
for batch_index in batch_indexs:
batch_en = [en[index] for index in batch_index]
batch_cn = [cn[index] for index in batch_index]
# paddings: batch, batch_size, batch_MaxLength
batch_cn = seq_padding(batch_cn)
batch_en = seq_padding(batch_en)
batches.append(Batch(batch_en, batch_cn))
#!!! 'Batch' Class is called here but defined in later section.
return batches
class Embeddings(nn.Module):
def __init__(self, d_model, vocab):
super(Embeddings, self).__init__()
self.lut = nn.Embedding(vocab, d_model)
self.d_model = d_model
def forward(self, x):
# return x's embedding vector(times math.sqrt(d_model))
return self.lut(x) * math.sqrt(self.d_model)
# %% 2.1 Positional Encoding
class PositionalEncoding(nn.Module):
def __init__(self, d_model, dropout, max_len=5000):
super(PositionalEncoding, self).__init__()
self.dropout = nn.Dropout(p=dropout)
pe = torch.zeros(max_len, d_model, device=DEVICE)
position = torch.arange(0., max_len, device=DEVICE).unsqueeze(1)
div_term = torch.exp(torch.arange(
0., d_model, 2, device=DEVICE) * -(math.log(10000.0) / d_model))
pe_pos = torch.mul(position, div_term)
pe[:, 0::2] = torch.sin(pe_pos)
pe[:, 1::2] = torch.cos(pe_pos)
pe = pe.unsqueeze(0)
self.register_buffer('pe', pe) # pe
def forward(self, x):
# build pe w.r.t to the max_length
x = x + Variable(self.pe[:, :x.size(1)], requires_grad=False)
return self.dropout(x)
# pe = PositionalEncoding(32, 0, 100) # d_model, dropout-ratio, max_len
# positional_encoding = pe.forward(
# Variable(torch.zeros(1, 100, 32))) # sequence length, d_model
# plt.figure(figsize=(10, 10))
# sns.heatmap(positional_encoding.squeeze()) # 100x32 matrix
# plt.title("Sinusoidal Function")
# plt.xlabel("hidden dimension")
# plt.ylabel("sequence length")
# None
# plt.figure(figsize=(15, 5))
# pe = PositionalEncoding(24, 0)
# y = pe.forward(Variable(torch.zeros(1, 100, 24)))
# plt.plot(np.arange(100), y[0, :, 5:10].data.numpy())
# plt.legend(["dim %d" % p for p in [5, 6, 7, 8, 9]])
# None
# %% 2.2 Self Attention and Mask
def attention(query, key, value, mask=None, dropout=None):
"Compute 'Scaled Dot Product Attention'"
d_k = query.size(-1)
scores = torch.matmul(query, key.transpose(-2, -1)) / math.sqrt(d_k)
if mask is not None:
scores = scores.masked_fill(mask == 0, -1e9)
p_attn = F.softmax(scores, dim=-1)
if dropout is not None:
p_attn = dropout(p_attn)
return torch.matmul(p_attn, value), p_attn
class MultiHeadedAttention(nn.Module):
def __init__(self, h, d_model, dropout=0.1):
"Take in model size and number of heads."
super(MultiHeadedAttention, self).__init__()
assert d_model % h == 0 # check the h number
self.d_k = d_model // h
self.h = h
# 4 linear layers: WQ WK WV and final linear mapping WO
self.linears = clones(nn.Linear(d_model, d_model), 4)
self.attn = None
self.dropout = nn.Dropout(p=dropout)
def forward(self, query, key, value, mask=None):
if mask is not None:
# Same mask applied to all h heads.
mask = mask.unsqueeze(1)
nbatches = query.size(0) # get batch size
# 1) Do all the linear projections in batch from d_model => h x d_k
# parttion into h sections,switch 2,3 axis for computation.
query, key, value = [l(x).view(nbatches, -1, self.h, self.d_k).transpose(1, 2)
for l, x in zip(self.linears, (query, key, value))]
# 2) Apply attention on all the projected vectors in batch.
x, self.attn = attention(
query, key, value, mask=mask, dropout=self.dropout)
# 3) "Concat" using a view and apply a final linear.
x = x.transpose(1, 2).contiguous().view(
nbatches, -1, self.h * self.d_k)
return self.linears[-1](x) # final linear layer
# **Attention Mask**
class Batch:
"Object for holding a batch of data with mask during training."
def __init__(self, src, trg=None, pad=0):
# convert words id to long format.
src = torch.from_numpy(src).to(DEVICE).long()
trg = torch.from_numpy(trg).to(DEVICE).long()
self.src = src
# get the padding postion binary mask
# change the matrix shape to 1×seq.length
self.src_mask = (src != pad).unsqueeze(-2)
# 如果输出目标不为空,则需要对decoder要使用到的target句子进行mask
if trg is not None:
# decoder input from target
self.trg = trg[:, :-1]
# decoder target from trg
self.trg_y = trg[:, 1:]
# add attention mask to decoder input
self.trg_mask = self.make_std_mask(self.trg, pad)
# check decoder output padding number
self.ntokens = (self.trg_y != pad).data.sum()
# Mask
@staticmethod
def make_std_mask(tgt, pad):
"Create a mask to hide padding and future words."
tgt_mask = (tgt != pad).unsqueeze(-2)
tgt_mask = tgt_mask & Variable(
subsequent_mask(tgt.size(-1)).type_as(tgt_mask.data))
return tgt_mask # subsequent_mask is defined in 'decoder' section.
# %% 2.3 Layer Normalization and Residual Connection
class LayerNorm(nn.Module):
def __init__(self, features, eps=1e-6):
super(LayerNorm, self).__init__()
self.a_2 = nn.Parameter(torch.ones(features))
self.b_2 = nn.Parameter(torch.zeros(features))
self.eps = eps
def forward(self, x):
mean = x.mean(-1, keepdim=True) # rows
std = x.std(-1, keepdim=True)
x_zscore = (x - mean) / torch.sqrt(std ** 2 + self.eps)
return self.a_2*x_zscore+self.b_2
class SublayerConnection(nn.Module):
"""
A residual connection followed by a layer norm.
Note for code simplicity the norm is first as opposed to last.
SublayerConnection: connect Multi-Head Attention and Feed Forward Layers
"""
def __init__(self, size, dropout):
super(SublayerConnection, self).__init__()
self.norm = LayerNorm(size)
self.dropout = nn.Dropout(dropout)
def forward(self, x, sublayer):
"Apply residual connection to any sublayer with the same size."
return x + self.dropout(sublayer(self.norm(x)))
# %% 2.4 Feedforwad Networks
class PositionwiseFeedForward(nn.Module):
def __init__(self, d_model, d_ff, dropout=0.1):
super(PositionwiseFeedForward, self).__init__()
self.w_1 = nn.Linear(d_model, d_ff)
self.w_2 = nn.Linear(d_ff, d_model)
self.dropout = nn.Dropout(dropout)
def forward(self, x):
h1 = self.w_1(x)
h2 = self.dropout(h1)
return self.w_2(h2)
# %% 2.5 Transformer Encoder Overview
def clones(module, N):
"""
"Produce N identical layers."
Use deepcopy the weight are indenpendent.
"""
return nn.ModuleList([copy.deepcopy(module) for _ in range(N)])
class Encoder(nn.Module):
"Core encoder is a stack of N layers (blocks)"
def __init__(self, layer, N):
super(Encoder, self).__init__()
self.layers = clones(layer, N)
self.norm = LayerNorm(layer.size)
def forward(self, x, mask):
"""
Pass the input (and mask) through each layer in turn.
"""
for layer in self.layers:
x = layer(x, mask)
return self.norm(x)
class EncoderLayer(nn.Module):
def __init__(self, size, self_attn, feed_forward, dropout):
super(EncoderLayer, self).__init__()
self.self_attn = self_attn
self.feed_forward = feed_forward
self.sublayer = clones(SublayerConnection(size, dropout), 2)
self.size = size # d_model
def forward(self, x, mask):
# X-embedding to Multi-head-Attention
x = self.sublayer[0](x, lambda x: self.self_attn(x, x, x, mask))
# X-embedding to feed-forwad nn
return self.sublayer[1](x, self.feed_forward)
# %% 3. Decoder
class Decoder(nn.Module):
def __init__(self, layer, N):
"Generic N layer decoder with masking."
super(Decoder, self).__init__()
self.layers = clones(layer, N)
self.norm = LayerNorm(layer.size)
def forward(self, x, memory, src_mask, tgt_mask):
"""
Repeat decoder N times
Decoderlayer get a input attention mask (src)
and a output attention mask (tgt) + subsequent mask
"""
for layer in self.layers:
x = layer(x, memory, src_mask, tgt_mask)
return self.norm(x)
class DecoderLayer(nn.Module):
def __init__(self, size, self_attn, src_attn, feed_forward, dropout):
super(DecoderLayer, self).__init__()
self.size = size
self.self_attn = self_attn
self.src_attn = src_attn
self.feed_forward = feed_forward
self.sublayer = clones(SublayerConnection(size, dropout), 3)
def forward(self, x, memory, src_mask, tgt_mask):
m = memory # encoder output embedding
x = self.sublayer[0](x, lambda x: self.self_attn(x, x, x, tgt_mask))
x = self.sublayer[1](x, lambda x: self.src_attn(x, m, m, src_mask))
# Context-Attention:q=decoder hidden,k,v from encoder hidden
return self.sublayer[2](x, self.feed_forward)
def subsequent_mask(size):
"Mask out subsequent positions."
attn_shape = (1, size, size)
subsequent_mask = np.triu(np.ones(attn_shape), k=1).astype('uint8')
return torch.from_numpy(subsequent_mask) == 0
# plt.figure(figsize=(5, 5))
# plt.imshow(subsequent_mask(20)[0])
# None
# %% 4. Transformer Model
class Transformer(nn.Module):
def __init__(self, encoder, decoder, src_embed, tgt_embed, generator):
super(Transformer, self).__init__()
self.encoder = encoder
self.decoder = decoder
self.src_embed = src_embed
self.tgt_embed = tgt_embed
self.generator = generator
def encode(self, src, src_mask):
return self.encoder(self.src_embed(src), src_mask)
def decode(self, memory, src_mask, tgt, tgt_mask):
return self.decoder(self.tgt_embed(tgt), memory, src_mask, tgt_mask)
def forward(self, src, tgt, src_mask, tgt_mask):
"Take in and process masked src and target sequences."
# encoder output will be the decoder's memory for decoding
return self.decode(self.encode(src, src_mask), src_mask, tgt, tgt_mask)
class Generator(nn.Module):
def __init__(self, d_model, vocab):
super(Generator, self).__init__()
# decode: d_model to vocab mapping
self.proj = nn.Linear(d_model, vocab)
def forward(self, x):
return F.log_softmax(self.proj(x), dim=-1)
def make_model(src_vocab, tgt_vocab, N=6, d_model=512, d_ff=2048, h=8, dropout=0.1):
c = copy.deepcopy
# Attention
attn = MultiHeadedAttention(h, d_model).to(DEVICE)
# FeedForward
ff = PositionwiseFeedForward(d_model, d_ff, dropout).to(DEVICE)
# Positional Encoding
position = PositionalEncoding(d_model, dropout).to(DEVICE)
# Transformer
model = Transformer(
Encoder(EncoderLayer(d_model, c(attn), c(ff),
dropout).to(DEVICE), N).to(DEVICE),
Decoder(DecoderLayer(d_model, c(attn), c(attn),
c(ff), dropout).to(DEVICE), N).to(DEVICE),
nn.Sequential(Embeddings(d_model, src_vocab).to(DEVICE), c(position)),
nn.Sequential(Embeddings(d_model, tgt_vocab).to(DEVICE), c(position)),
Generator(d_model, tgt_vocab)).to(DEVICE)
# This was important from their code.
# Initialize parameters with Glorot / fan_avg.
# Paper title: Understanding the difficulty of training deep feedforward neural networks Xavier
for p in model.parameters():
if p.dim() > 1:
nn.init.xavier_uniform_(p)
return model.to(DEVICE)
# %% 5. Transformer Model Training: English-to-Chinese
class LabelSmoothing(nn.Module):
"Implement label smoothing."
def __init__(self, size, padding_idx, smoothing=0.0):
super(LabelSmoothing, self).__init__()
self.criterion = nn.KLDivLoss(reduction='sum') # 2020 update
self.padding_idx = padding_idx
self.confidence = 1.0 - smoothing
self.smoothing = smoothing
self.size = size
self.true_dist = None
def forward(self, x, target):
assert x.size(1) == self.size
true_dist = x.data.clone()
true_dist.fill_(self.smoothing / (self.size - 2))
true_dist.scatter_(1, target.data.unsqueeze(1), self.confidence)
true_dist[:, self.padding_idx] = 0
mask = torch.nonzero(target.data == self.padding_idx)
if mask.dim() > 0:
true_dist.index_fill_(0, mask.squeeze(), 0.0)
self.true_dist = true_dist
return self.criterion(x, Variable(true_dist, requires_grad=False))
# # Example of label smoothing.
# crit = LabelSmoothing(5, 0, 0.1) # ϵ=0.4
# predict = torch.FloatTensor([[0, 0.2, 0.7, 0.1, 0],
# [0, 0.2, 0.7, 0.1, 0],
# [0, 0.2, 0.7, 0.1, 0]])
# v = crit(Variable(predict.log()), Variable(torch.LongTensor([2, 1, 0])))
# # Show the target distributions expected by the system.
# plt.imshow(crit.true_dist)
# None
# crit = LabelSmoothing(5, 0, 0.1)
# def loss(x):
# d = x + 3 * 1
# predict = torch.FloatTensor([[0, x / d, 1 / d, 1 / d, 1 / d]])
# return crit(Variable(predict.log()), Variable(torch.LongTensor([1]))).item()
# plt.plot(np.arange(1, 100), [loss(x) for x in range(1, 100)])
# None
# **Loss Computation**
class SimpleLossCompute:
def __init__(self, generator, criterion, opt=None):
self.generator = generator
self.criterion = criterion
self.opt = opt
def __call__(self, x, y, norm):
x = self.generator(x)
loss = self.criterion(x.contiguous().view(-1, x.size(-1)),
y.contiguous().view(-1)) / norm
loss.backward()
if self.opt is not None:
self.opt.step()
self.opt.optimizer.zero_grad()
return loss.data.item() * norm.float()
# **Optimizer with Warmup Learning Rate**
class NoamOpt:
"Optim wrapper that implements rate."
def __init__(self, model_size, factor, warmup, optimizer):
self.optimizer = optimizer
self._step = 0
self.warmup = warmup
self.factor = factor
self.model_size = model_size
self._rate = 0
def step(self):
"Update parameters and rate"
self._step += 1
rate = self.rate()
for p in self.optimizer.param_groups:
p['lr'] = rate
self._rate = rate
self.optimizer.step()
def rate(self, step=None):
"Implement `lrate` above"
if step is None:
step = self._step
return self.factor * (self.model_size ** (-0.5) * min(step ** (-0.5), step * self.warmup ** (-1.5)))
# We used factor=2, warmup-step = 4000
def get_std_opt(model):
return NoamOpt(model.src_embed[0].d_model, 2, 4000,
torch.optim.Adam(model.parameters(), lr=0, betas=(0.9, 0.98), eps=1e-9))
# # Three settings of the lrate hyperparameters.
# opts = [NoamOpt(512, 1, 4000, None),
# NoamOpt(512, 1, 8000, None),
# NoamOpt(256, 1, 4000, None)]
# plt.plot(np.arange(1, 20000), [[opt.rate(i)
# for opt in opts] for i in range(1, 20000)])
# plt.legend(["512:4000", "512:8000", "256:4000"])
# None
# **Training Iterators**
def run_epoch(data, model, loss_compute, epoch):
start = time.time()
total_tokens = 0.
total_loss = 0.
tokens = 0.
for i, batch in enumerate(data):
out = model(batch.src, batch.trg, batch.src_mask, batch.trg_mask)
loss = loss_compute(out, batch.trg_y, batch.ntokens)
total_loss += loss
total_tokens += batch.ntokens
tokens += batch.ntokens
if i % 50 == 1:
elapsed = time.time() - start
print("Epoch {:d} Batch: {:d} Loss: {:.4f} Tokens per Sec: {:.2f}s".format(
epoch, i - 1, loss / batch.ntokens, (tokens.float() / elapsed / 1000.)))
start = time.time()
tokens = 0
return total_loss / total_tokens
def train(data, model, criterion, optimizer):
"""
Train and Save the model.
"""
# init loss as a large value
best_dev_loss = 1e5
for epoch in range(EPOCHS):
# Train model
model.train()
run_epoch(data.train_data, model, SimpleLossCompute(
model.generator, criterion, optimizer), epoch)
model.eval()
# validate model on dev dataset
print('>>>>> Evaluate')
dev_loss = run_epoch(data.dev_data, model, SimpleLossCompute(
model.generator, criterion, None), epoch)
print('<<<<< Evaluate loss: {:.2f}'.format(dev_loss))
# save the model with best-dev-loss
if dev_loss < best_dev_loss:
best_dev_loss = dev_loss
# SAVE_FILE = 'save/model.pt'
torch.save(model.state_dict(), SAVE_FILE)
print(f">>>>> current best loss: {best_dev_loss}")
# %% Main Function
# Step 1: Data Preprocessing
data = PrepareData(TRAIN_FILE, DEV_FILE)
src_vocab = len(data.en_word_dict)
tgt_vocab = len(data.cn_word_dict)
print(f"src_vocab {src_vocab}")
print(f"tgt_vocab {tgt_vocab}")
# Step 2: Init model
model = make_model(
src_vocab,
tgt_vocab,
LAYERS,
D_MODEL,
D_FF,
H_NUM,
DROPOUT
)
# Step 3: Training model
print(">>>>>>> start train")
train_start = time.time()
criterion = LabelSmoothing(tgt_vocab, padding_idx=0, smoothing=0.0)
optimizer = NoamOpt(D_MODEL, 1, 2000, torch.optim.Adam(
model.parameters(), lr=0, betas=(0.9, 0.98), eps=1e-9))
train(data, model, criterion, optimizer)
print(f"<<<<<<< finished train, cost {time.time()-train_start:.4f} seconds")
# %% 6. Prediction with English-to-Chinese Translator
def greedy_decode(model, src, src_mask, max_len, start_symbol):
"""
Translate src with model
"""
# decode the src
memory = model.encode(src, src_mask)
# init 1×1 tensor as prediction,fill in ('BOS')id, type: (LongTensor)
ys = torch.ones(1, 1).fill_(start_symbol).type_as(src.data)
# run 遍历输出的长度下标
for i in range(max_len-1):
# decode one by one
out = model.decode(memory,
src_mask,
Variable(ys),
Variable(subsequent_mask(ys.size(1)).type_as(src.data)))
# out to log_softmax
prob = model.generator(out[:, -1])
# get the max-prob id
_, next_word = torch.max(prob, dim=1)
next_word = next_word.data[0]
# concatnate with early predictions
ys = torch.cat([ys, torch.ones(1, 1).type_as(
src.data).fill_(next_word)], dim=1)
return ys
# English to Chinese Translations
def evaluate(data, model):
"""
Make prediction with trained model, and print results.
"""
with torch.no_grad():
# pick some random sentences from dev data.
for i in np.random.randint(len(data.dev_en), size=10):
# Print English sentence
en_sent = " ".join([data.en_index_dict[w] for w in data.dev_en[i]])
print("\n" + en_sent)
# Print Target Chinese sentence
cn_sent = " ".join([data.cn_index_dict[w] for w in data.dev_cn[i]])
print("".join(cn_sent))
# conver English to tensor
src = torch.from_numpy(np.array(data.dev_en[i])).long().to(DEVICE)
src = src.unsqueeze(0)
# set attention mask
src_mask = (src != 0).unsqueeze(-2)
# apply model to decode, make prediction
out = greedy_decode(
model, src, src_mask, max_len=MAX_LENGTH, start_symbol=data.cn_word_dict["BOS"])
# save all in the translation list
translation = []
# convert id to Chinese, skip 'BOS' 0.
# 遍历翻译输出字符的下标(注意:跳过开始符"BOS"的索引 0)
for j in range(1, out.size(1)):
sym = data.cn_index_dict[out[0, j].item()]
if sym != 'EOS':
translation.append(sym)
else:
break
print("translation: {}".format(" ".join(translation)))
# **English to Chinese Translator**
# Predition
model.load_state_dict(torch.load(SAVE_FILE))
print(">>>>>>> start evaluate")
evaluate_start = time.time()
evaluate(data, model)
print(
f"<<<<<<< finished evaluate, cost {time.time()-evaluate_start:.4f} seconds")
# Here is my pretrained model with CUDA support
if DEVICE == "cuda":
SAVE_FILE_EXTRA = 'save/models/large_model.pt'
model.load_state_dict(torch.load(SAVE_FILE_EXTRA))
print(">>>>>>> start evaluate")
evaluate_start = time.time()
evaluate(data, model)
print(
f"<<<<<<< finished evaluate, cost {time.time()-evaluate_start:.4f} seconds")
# %% Reference
#
# - The Annotated Transformer http://nlp.seas.harvard.edu/2018/04/03/attention.html
# - The Illustrated Transformer http://jalammar.github.io/illustrated-transformer/
# - GreedAI NLP Training Camp: https://www.greedyai.com/
#
# THE END