-
Notifications
You must be signed in to change notification settings - Fork 10
/
transformer.py
448 lines (367 loc) · 19.6 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
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import math
from Model.common_layer import EncoderLayer, DecoderLayer, MultiHeadAttention, Conv, PositionwiseFeedForward, LayerNorm , _gen_bias_mask ,_gen_timing_signal, share_embedding, LabelSmoothing, NoamOpt, _get_attn_subsequent_mask
from utils import config
import random
# from numpy import random
import os
import pprint
from tqdm import tqdm
pp = pprint.PrettyPrinter(indent=1)
import os
import time
from copy import deepcopy
from sklearn.metrics import accuracy_score
import pdb
torch.manual_seed(0)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
np.random.seed(0)
class Encoder(nn.Module):
"""
A Transformer Encoder module.
Inputs should be in the shape [batch_size, length, hidden_size]
Outputs will have the shape [batch_size, length, hidden_size]
Refer Fig.1 in https://arxiv.org/pdf/1706.03762.pdf
"""
def __init__(self, embedding_size, hidden_size, num_layers, num_heads, total_key_depth, total_value_depth,
filter_size, max_length=1000, input_dropout=0.0, layer_dropout=0.0,
attention_dropout=0.0, relu_dropout=0.0, use_mask=False, universal=False, concept=False):
"""
Parameters:
embedding_size: Size of embeddings
hidden_size: Hidden size
num_layers: Total layers in the Encoder 2
num_heads: Number of attention heads 2
total_key_depth: Size of last dimension of keys. Must be divisible by num_head 40
total_value_depth: Size of last dimension of values. Must be divisible by num_head 40
output_depth: Size last dimension of the final output
filter_size: Hidden size of the middle layer in FFN 50
max_length: Max sequence length (required for timing signal)
input_dropout: Dropout just after embedding
layer_dropout: Dropout for each layer
attention_dropout: Dropout probability after attention (Should be non-zero only during training)
relu_dropout: Dropout probability after relu in FFN (Should be non-zero only during training)
use_mask: Set to True to turn on future value masking
"""
super(Encoder, self).__init__()
self.universal = universal
self.num_layers = num_layers
self.timing_signal = _gen_timing_signal(max_length, hidden_size)
if(self.universal):
## for t
self.position_signal = _gen_timing_signal(num_layers, hidden_size)
params =(hidden_size,
total_key_depth or hidden_size,
total_value_depth or hidden_size,
filter_size,
num_heads,
_gen_bias_mask(max_length) if use_mask else None,
layer_dropout,
attention_dropout,
relu_dropout)
self.embedding_proj = nn.Linear(embedding_size, hidden_size, bias=False)
if(self.universal):
self.enc = EncoderLayer(*params)
else:
self.enc = nn.ModuleList([EncoderLayer(*params) for _ in range(num_layers)])
self.layer_norm = LayerNorm(hidden_size)
self.input_dropout = nn.Dropout(input_dropout)
def forward(self, inputs, mask):
#Add input dropout
x = self.input_dropout(inputs)
# Project to hidden size
x = self.embedding_proj(x)
if(self.universal):
if(config.act):
x, (self.remainders, self.n_updates) = self.act_fn(x, inputs, self.enc, self.timing_signal, self.position_signal, self.num_layers)
y = self.layer_norm(x)
else:
for l in range(self.num_layers):
x += self.timing_signal[:, :inputs.shape[1], :].type_as(inputs.data)
x += self.position_signal[:, l, :].unsqueeze(1).repeat(1,inputs.shape[1],1).type_as(inputs.data)
x = self.enc(x, mask=mask)
y = self.layer_norm(x)
else:
# Add timing signal
x += self.timing_signal[:, :inputs.shape[1], :].type_as(inputs.data)
for i in range(self.num_layers):
x = self.enc[i](x, mask)
y = self.layer_norm(x)
return y
class Decoder(nn.Module):
"""
A Transformer Decoder module.
Inputs should be in the shape [batch_size, length, hidden_size]
Outputs will have the shape [batch_size, length, hidden_size]
Refer Fig.1 in https://arxiv.org/pdf/1706.03762.pdf
"""
def __init__(self, embedding_size, hidden_size, num_layers, num_heads, total_key_depth, total_value_depth,
filter_size, max_length=1000, input_dropout=0.0, layer_dropout=0.0,
attention_dropout=0.0, relu_dropout=0.0, universal=False):
"""
Parameters:
embedding_size: Size of embeddings
hidden_size: Hidden size
num_layers: Total layers in the Encoder
num_heads: Number of attention heads
total_key_depth: Size of last dimension of keys. Must be divisible by num_head
total_value_depth: Size of last dimension of values. Must be divisible by num_head
output_depth: Size last dimension of the final output
filter_size: Hidden size of the middle layer in FFN
max_length: Max sequence length (required for timing signal)
input_dropout: Dropout just after embedding
layer_dropout: Dropout for each layer
attention_dropout: Dropout probability after attention (Should be non-zero only during training)
relu_dropout: Dropout probability after relu in FFN (Should be non-zero only during training)
"""
super(Decoder, self).__init__()
self.universal = universal
self.num_layers = num_layers
self.timing_signal = _gen_timing_signal(max_length, hidden_size)
if(self.universal):
## for t
self.position_signal = _gen_timing_signal(num_layers, hidden_size)
self.mask = _get_attn_subsequent_mask(max_length)
params =(hidden_size,
total_key_depth or hidden_size,
total_value_depth or hidden_size,
filter_size,
num_heads,
_gen_bias_mask(max_length), # mandatory
layer_dropout,
attention_dropout,
relu_dropout)
if(self.universal):
self.dec = DecoderLayer(*params)
else:
self.dec = nn.Sequential(*[DecoderLayer(*params) for l in range(num_layers)])
self.embedding_proj = nn.Linear(embedding_size, hidden_size, bias=False)
self.layer_norm = LayerNorm(hidden_size)
self.input_dropout = nn.Dropout(input_dropout)
def forward(self, inputs, encoder_output, mask):
mask_src, mask_trg = mask
dec_mask = torch.gt(mask_trg.bool() + self.mask[:, :mask_trg.size(-1), :mask_trg.size(-1)].bool(), 0)
#Add input dropout
x = self.input_dropout(inputs)
x = self.embedding_proj(x)
if(self.universal):
if(config.act):
x, attn_dist, (self.remainders,self.n_updates) = self.act_fn(x, inputs, self.dec, self.timing_signal, self.position_signal, self.num_layers, encoder_output, decoding=True)
y = self.layer_norm(x)
else:
x += self.timing_signal[:, :inputs.shape[1], :].type_as(inputs.data)
for l in range(self.num_layers):
x += self.position_signal[:, l, :].unsqueeze(1).repeat(1,inputs.shape[1],1).type_as(inputs.data)
x, _, attn_dist, _ = self.dec((x, encoder_output, [], (mask_src,dec_mask)))
y = self.layer_norm(x)
else:
# Add timing signal
x += self.timing_signal[:, :inputs.shape[1], :].type_as(inputs.data)
# Run decoder
y, _, attn_dist, _ = self.dec((x, encoder_output, [], (mask_src,dec_mask)))
# Final layer normalization
y = self.layer_norm(y)
return y, attn_dist
class Generator(nn.Module):
"Define standard linear + softmax generation step."
def __init__(self, d_model, vocab):
super(Generator, self).__init__()
self.proj = nn.Linear(d_model, vocab)
self.p_gen_linear = nn.Linear(config.hidden_dim, 1)
def forward(self, x, attn_dist=None, enc_batch_extend_vocab=None, max_oov_length=None, temp=1, beam_search=False, attn_dist_db=None):
if config.pointer_gen:
p_gen = self.p_gen_linear(x)
alpha = torch.sigmoid(p_gen)
logit = self.proj(x)
if config.pointer_gen:
vocab_dist = F.softmax(logit/temp, dim=2)
vocab_dist_ = alpha * vocab_dist
attn_dist = F.softmax(attn_dist/temp, dim=-1)
attn_dist_ = (1 - alpha) * attn_dist
enc_batch_extend_vocab_ = torch.cat([enc_batch_extend_vocab.unsqueeze(1)]*x.size(1),1) ## extend for all seq
# if beam_search:
# enc_batch_extend_vocab_ = torch.cat([enc_batch_extend_vocab_[0].unsqueeze(0)]*x.size(0),0) ## extend for all seq
logit = torch.log(vocab_dist_.scatter_add(2, enc_batch_extend_vocab_, attn_dist_))
return logit
else:
return F.log_softmax(logit, dim=-1)
class Transformer(nn.Module):
def __init__(self, vocab, decoder_number, model_file_path=None, is_eval=False, load_optim=False):
super(Transformer, self).__init__()
self.vocab = vocab
self.vocab_size = vocab.n_words
self.embedding = share_embedding(self.vocab,config.pretrain_emb)
self.encoder = Encoder(config.emb_dim, config.hidden_dim, num_layers=config.hop, num_heads=config.heads,
total_key_depth=config.depth, total_value_depth=config.depth,
filter_size=config.filter,universal=config.universal)
## multiple decoders
self.decoder = Decoder(config.emb_dim, hidden_size = config.hidden_dim, num_layers=config.hop, num_heads=config.heads,
total_key_depth=config.depth,total_value_depth=config.depth,
filter_size=config.filter)
self.decoder_key = nn.Linear(config.hidden_dim ,decoder_number, bias=False)
self.generator = Generator(config.hidden_dim, self.vocab_size)
if config.weight_sharing:
# Share the weight matrix between target word embedding & the final logit dense layer
self.generator.proj.weight = self.embedding.lut.weight
self.criterion = nn.NLLLoss(ignore_index=config.PAD_idx)
if config.label_smoothing:
self.criterion = LabelSmoothing(size=self.vocab_size, padding_idx=config.PAD_idx, smoothing=0.1)
self.criterion_ppl = nn.NLLLoss(ignore_index=config.PAD_idx)
self.optimizer = torch.optim.Adam(self.parameters(), lr=config.lr)
if config.noam:
self.optimizer = NoamOpt(config.hidden_dim, 1, 8000, torch.optim.Adam(self.parameters(), lr=0, betas=(0.9, 0.98), eps=1e-9))
if model_file_path is not None:
print("loading weights")
state = torch.load(model_file_path, map_location=lambda storage, location: storage)
self.encoder.load_state_dict(state['encoder_state_dict'])
self.decoder.load_state_dict(state['decoder_state_dict'])
self.generator.load_state_dict(state['generator_dict'])
self.embedding.load_state_dict(state['embedding_dict'])
self.decoder_key.load_state_dict(state['decoder_key_state_dict'])
if load_optim:
self.optimizer.load_state_dict(state['optimizer'])
self.eval()
self.model_dir = config.save_path
if not os.path.exists(self.model_dir):
os.makedirs(self.model_dir)
self.best_path = ""
def save_model(self, running_avg_ppl, iter, f1_g,f1_b,ent_g,ent_b):
state = {
'iter': iter,
'encoder_state_dict': self.encoder.state_dict(),
'decoder_state_dict': self.decoder.state_dict(),
'generator_dict': self.generator.state_dict(),
'decoder_key_state_dict': self.decoder_key.state_dict(),
'embedding_dict': self.embedding.state_dict(),
'optimizer': self.optimizer.state_dict(),
'current_loss': running_avg_ppl
}
model_save_path = os.path.join(self.model_dir, 'model_{}_{:.4f}_{:.4f}_{:.4f}_{:.4f}_{:.4f}'.format(iter,running_avg_ppl,f1_g,f1_b,ent_g,ent_b) )
self.best_path = model_save_path
torch.save(state, model_save_path)
def train_one_batch(self, batch, iter, train=True):
enc_batch = batch["context_batch"]
enc_batch_extend_vocab = batch["context_ext_batch"]
oovs = batch["oovs"]
max_oov_length = len(sorted(oovs, key=lambda i: len(i), reverse=True)[0])
dec_batch = batch["target_batch"]
dec_ext_batch = batch["target_ext_batch"]
if config.noam:
self.optimizer.optimizer.zero_grad()
else:
self.optimizer.zero_grad()
## Embedding - context
mask_src = enc_batch.data.eq(config.PAD_idx).unsqueeze(1) # (bsz, src_len)->(bsz, 1, src_len)
emb_mask = self.embedding(batch["mask_context"])
src_emb = self.embedding(enc_batch)+emb_mask
encoder_outputs = self.encoder(src_emb, mask_src) # (bsz, src_len, emb_dim)
sos_token = torch.LongTensor([config.SOS_idx] * enc_batch.size(0)).unsqueeze(1) # (bsz, 1)
if config.USE_CUDA: sos_token = sos_token.cuda()
dec_batch_shift = torch.cat((sos_token, dec_batch[:, :-1]), 1) # (bsz, tgt_len)
mask_trg = dec_batch_shift.data.eq(config.PAD_idx).unsqueeze(1)
pre_logit, attn_dist = self.decoder(self.embedding(dec_batch_shift), encoder_outputs, (mask_src,mask_trg))
enc_ext_batch = enc_batch_extend_vocab
logit = self.generator(pre_logit, attn_dist, enc_ext_batch if config.pointer_gen else None, max_oov_length, attn_dist_db=None)
#logit = F.log_softmax(logit,dim=-1) #fix the name later
## loss: NNL if ptr else Cross entropy
loss = self.criterion(logit.contiguous().view(-1, logit.size(-1)), dec_batch.contiguous().view(-1))
if config.label_smoothing:
loss_ppl = self.criterion_ppl(logit.contiguous().view(-1, logit.size(-1)), dec_batch.contiguous().view(-1)).item()
if train:
loss.backward()
self.optimizer.step()
if config.label_smoothing:
return loss_ppl, math.exp(min(loss_ppl, 100)), 0, 0
else:
return loss.item(), math.exp(min(loss.item(), 100)), 0, 0
def compute_act_loss(self,module):
R_t = module.remainders
N_t = module.n_updates
p_t = R_t + N_t
avg_p_t = torch.sum(torch.sum(p_t,dim=1)/p_t.size(1))/p_t.size(0)
loss = config.act_loss_weight * avg_p_t.item()
return loss
def decoder_greedy(self, batch, max_dec_step=30):
enc_batch_extend_vocab, extra_zeros = None, None
enc_batch = batch["context_batch"]
enc_batch_extend_vocab = batch["context_ext_batch"]
oovs = batch["oovs"]
max_oov_length = len(sorted(oovs, key=lambda i: len(i), reverse=True)[0])
## Encode - context
mask_src = enc_batch.data.eq(config.PAD_idx).unsqueeze(1) # (bsz, src_len)->(bsz, 1, src_len)
emb_mask = self.embedding(batch["mask_context"])
src_emb = self.embedding(enc_batch) + emb_mask
encoder_outputs = self.encoder(src_emb, mask_src) # (bsz, src_len, emb_dim)
enc_ext_batch = enc_batch_extend_vocab
ys = torch.ones(1, 1).fill_(config.SOS_idx).long() # test的时候 bsz = 1
if config.USE_CUDA:
ys = ys.cuda()
mask_trg = ys.data.eq(config.PAD_idx).unsqueeze(1)
decoded_words = []
for i in range(max_dec_step+1):
if config.project:
out, attn_dist = self.decoder(self.embedding_proj_in(self.embedding(ys)),self.embedding_proj_in(encoder_outputs), (mask_src,mask_trg))
else:
out, attn_dist = self.decoder(self.embedding(ys), encoder_outputs, (mask_src,mask_trg))
prob = self.generator(out, attn_dist, enc_ext_batch, max_oov_length, attn_dist_db=None)
#logit = F.log_softmax(logit,dim=-1) #fix the name later
#filtered_logit = top_k_top_p_filtering(logit[:, -1], top_k=0, top_p=0, filter_value=-float('Inf'))
# Sample from the filtered distribution
#next_word = torch.multinomial(F.softmax(filtered_logit, dim=-1), 1).squeeze()
_, next_word = torch.max(prob[:, -1], dim = 1)
decoded_words.append(['<EOS>' if ni.item() == config.EOS_idx else self.vocab.index2word[ni.item()] for ni in next_word.view(-1)])
next_word = next_word.data[0]
if config.USE_CUDA:
ys = torch.cat([ys, torch.ones(1, 1).long().fill_(next_word).cuda()], dim=1)
ys = ys.cuda()
else:
ys = torch.cat([ys, torch.ones(1, 1).long().fill_(next_word)], dim=1)
mask_trg = ys.data.eq(config.PAD_idx).unsqueeze(1)
sent = []
for _, row in enumerate(np.transpose(decoded_words)):
st = ''
for e in row:
if e == '<EOS>': break
else: st+= e + ' '
sent.append(st)
return sent
def decoder_topk(self, batch, max_dec_step=30):
enc_batch_extend_vocab, extra_zeros = None, None
enc_batch = batch["context_batch"]
mask_src = enc_batch.data.eq(config.PAD_idx).unsqueeze(1)
emb_mask = self.embedding(batch["mask_input"])
encoder_outputs = self.encoder(self.embedding(enc_batch)+emb_mask, mask_src)
ys = torch.ones(1, 1).fill_(config.SOS_idx).long()
if config.USE_CUDA:
ys = ys.cuda()
mask_trg = ys.data.eq(config.PAD_idx).unsqueeze(1)
decoded_words = []
for i in range(max_dec_step+1):
if config.project:
out, attn_dist = self.decoder(self.embedding_proj_in(self.embedding(ys)),self.embedding_proj_in(encoder_outputs), (mask_src,mask_trg))
else:
out, attn_dist = self.decoder(self.embedding(ys),encoder_outputs, (mask_src,mask_trg))
logit = self.generator(out,attn_dist,enc_batch_extend_vocab, extra_zeros, attn_dist_db=None)
filtered_logit = top_k_top_p_filtering(logit[:, -1], top_k=3, top_p=0, filter_value=-float('Inf'))
# Sample from the filtered distribution
next_word = torch.multinomial(F.softmax(filtered_logit, dim=-1), 1).squeeze()
decoded_words.append(['<EOS>' if ni.item() == config.EOS_idx else self.vocab.index2word[ni.item()] for ni in next_word.view(-1)])
next_word = next_word.data[0]
if config.USE_CUDA:
ys = torch.cat([ys, torch.ones(1, 1).long().fill_(next_word).cuda()], dim=1)
ys = ys.cuda()
else:
ys = torch.cat([ys, torch.ones(1, 1).long().fill_(next_word)], dim=1)
mask_trg = ys.data.eq(config.PAD_idx).unsqueeze(1)
sent = []
for _, row in enumerate(np.transpose(decoded_words)):
st = ''
for e in row:
if e == '<EOS>': break
else: st+= e + ' '
sent.append(st)
return sent