-
Notifications
You must be signed in to change notification settings - Fork 15
/
train.py
878 lines (701 loc) · 39.5 KB
/
train.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
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
# -*- coding: utf-8 -*-
"""
Python File Template
"""
import json
import os
import logging
import numpy as np
from torch.optim import Adam
import evaluate
import utils
import copy
import torch
from beam_search import SequenceGenerator
from evaluate import evaluate_beam_search, get_match_result, self_redundancy
from pykp.dataloader import KeyphraseDataLoader
from utils import Progbar, plot_learning_curve_and_write_csv
from config import init_logging, init_opt
import pykp
from pykp.io import KeyphraseDataset
from pykp.model import Seq2SeqLSTMAttention, Seq2SeqLSTMAttentionCascading, Seq2SeqBERT, Seq2SeqBERTLow, Seq2SeqTransformer
from pytorch_pretrained_bert.tokenization import BertTokenizer
import time
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger()
def to_cpu_list(input):
assert isinstance(input, list)
output = [int(item.data.cpu().numpy()) for item in input]
return output
def time_usage(func):
# argnames = func.func_code.co_varnames[:func.func_code.co_argcount]
fname = func.__name__
def wrapper(*args, **kwargs):
beg_ts = time.time()
retval = func(*args, **kwargs)
end_ts = time.time()
print(fname, "elapsed time: %f" % (end_ts - beg_ts))
return retval
return wrapper
__author__ = "Rui Meng"
__email__ = "[email protected]"
@time_usage
def _valid_error(data_loader, model, criterion, epoch, opt):
progbar = Progbar(title='Validating', target=len(data_loader), batch_size=data_loader.batch_size,
total_examples=len(data_loader.dataset))
model.eval()
losses = []
# Note that the data should be shuffled every time
for i, batch in enumerate(data_loader):
# if i >= 100:
# break
one2many_batch, one2one_batch = batch
src, trg, trg_target, trg_copy_target, src_ext, oov_lists = one2one_batch
if torch.cuda.is_available() and opt.useGpu:
src = src.cuda()
trg = trg.cuda()
trg_target = trg_target.cuda()
trg_copy_target = trg_copy_target.cuda()
src_ext = src_ext.cuda()
decoder_log_probs, _, _ = model.forward(src, trg, src_ext)
if not opt.copy_attention:
loss = criterion(
decoder_log_probs.contiguous().view(-1, opt.vocab_size),
trg_target.contiguous().view(-1)
)
else:
loss = criterion(
decoder_log_probs.contiguous().view(-1, opt.vocab_size + opt.max_unk_words),
trg_copy_target.contiguous().view(-1)
)
losses.append(loss.data[0])
progbar.update(epoch, i, [('valid_loss', loss.data[0]), ('PPL', loss.data[0])])
return losses
def getPos(src_len):
return torch.tensor([list(range(1, id + 1)) + [0] * (max(src_len) - id) for id in src_len])
def getLen(src):
return [a.size(0) if int(a[-1]) != 0 else [int(id) for id in a].index(0) for a in src]
def train_ml(one2one_batch, model, optimizer, criterion, opt):
src, src_len, trg, trg_target, trg_copy_target, src_oov, oov_lists = one2one_batch
max_oov_number = max([len(oov) for oov in oov_lists])
src_pos = getPos(src_len)
trg_pos = getPos(getLen(trg))
print("src size - ", src.size())
print("target size - ", trg.size())
if torch.cuda.is_available() and opt.useGpu:
src = src.cuda()
trg = trg.cuda()
trg_target = trg_target.cuda()
trg_copy_target = trg_copy_target.cuda()
src_oov = src_oov.cuda()
src_pos = src_pos.cuda()
trg_pos = trg_pos.cuda()
optimizer.zero_grad()
if opt.encoder_type == 'bert':
decoder_log_probs, _, _ = model.forward(src, trg, src_oov, oov_lists)
elif opt.encoder_type == 'bert_low':
decoder_log_probs, _ = model.forward(src, trg)
elif opt.encoder_type == 'transformer':
decoder_log_probs, _, _ = model.forward(src, src_pos, trg, trg_pos, src_oov, oov_lists)
else:
decoder_log_probs, _, _ = model.forward(src, src_len, trg, src_oov, oov_lists)
# simply average losses of all the predicitons
# IMPORTANT, must use logits instead of probs to compute the loss, otherwise it's super super slow at the beginning (grads of probs are small)!
start_time = time.time()
if not opt.copy_attention:
loss = criterion(
decoder_log_probs.contiguous().view(-1, opt.vocab_size),
trg_target.contiguous().view(-1)
)
else:
loss = criterion(
decoder_log_probs.contiguous().view(-1, opt.vocab_size + max_oov_number),
trg_copy_target.contiguous().view(-1)
)
if opt.train_rl:
loss = loss * (1 - opt.loss_scale)
print("--loss calculation- %s seconds ---" % (time.time() - start_time))
start_time = time.time()
loss.backward()
print("--backward- %s seconds ---" % (time.time() - start_time))
if opt.max_grad_norm > 0:
pre_norm = torch.nn.utils.clip_grad_norm_(model.parameters(), opt.max_grad_norm)
after_norm = (sum([p.grad.data.norm(2) ** 2 for p in model.parameters() if p.grad is not None])) ** (1.0 / 2)
# logging.info('clip grad (%f -> %f)' % (pre_norm, after_norm))
optimizer.step()
if torch.cuda.is_available() and opt.useGpu:
loss_value = loss.cpu().data.numpy()
else:
loss_value = loss.data.numpy()
return loss_value, decoder_log_probs
def train_rl_0(one2many_batch, model, optimizer, generator, opt):
src_list, src_len, trg_list, _, trg_copy_target_list, src_oov_map_list, oov_list = one2many_batch
if torch.cuda.is_available() and opt.useGpu:
src_list = src_list.cuda()
src_oov_map_list = src_oov_map_list.cuda()
# Baseline sequences for self-critic
baseline_seqs_list = generator.sample(src_list, src_len, src_oov_map_list, oov_list, opt.word2id, k=5, is_greedy=True)
# Sample number_batch*beam_size sequences
sampled_seqs_list = generator.sample(src_list, src_len, src_oov_map_list, oov_list, opt.word2id, k=5, is_greedy=False)
policy_loss = []
policy_rewards = []
# Compute their rewards and losses
for seq_i, (src, trg, trg_copy, sampled_seqs, baseline_seqs, oov) in enumerate(zip(src_list, trg_list, trg_copy_target_list, sampled_seqs_list, baseline_seqs_list, oov_list)):
# convert to string sequences
baseline_str_seqs = [[opt.id2word[x] if x < opt.vocab_size else oov[x - opt.vocab_size] for x in to_cpu_list(seq.sentence)] for seq in baseline_seqs]
baseline_str_seqs = [seq[:seq.index(pykp.io.EOS_WORD) + 1] if pykp.io.EOS_WORD in seq else seq for seq in baseline_str_seqs]
sampled_str_seqs = [[opt.id2word[x] if x < opt.vocab_size else oov[x - opt.vocab_size] for x in to_cpu_list(seq.sentence)] for seq in sampled_seqs]
sampled_str_seqs = [seq[:seq.index(pykp.io.EOS_WORD) + 1] if pykp.io.EOS_WORD in seq else seq for seq in sampled_str_seqs]
# pad trg seqs with EOS to the same length
trg_seqs = [[opt.id2word[x] if x < opt.vocab_size else oov[x - opt.vocab_size] for x in seq] for seq in trg_copy]
# trg_seqs = [seq + [pykp.IO.EOS_WORD] * (opt.max_sent_length - len(seq)) for seq in trg_seqs]
# local rewards (bleu)
bleu_baselines = get_match_result(true_seqs=trg_seqs, pred_seqs=baseline_str_seqs, type='bleu')
bleu_samples = get_match_result(true_seqs=trg_seqs, pred_seqs=sampled_str_seqs, type='bleu')
# global rewards
match_baselines = get_match_result(true_seqs=trg_seqs, pred_seqs=baseline_str_seqs, type='exact')
match_samples = get_match_result(true_seqs=trg_seqs, pred_seqs=sampled_str_seqs, type='exact')
_, _, fscore_baselines = evaluate.evaluate(match_baselines, baseline_str_seqs, trg_seqs, topk=5)
_, _, fscore_samples = evaluate.evaluate(match_samples, sampled_str_seqs, trg_seqs, topk=5)
# compute the final rewards
alpha = 0.0
baseline = alpha * np.average(bleu_baselines) + (1.0 - alpha) * fscore_baselines
rewards = alpha * np.asarray(bleu_samples) + (1.0 - alpha) * fscore_samples
"""
print('*' * 20 + ' ' + str(seq_i) + ' ' + '*' * 20)
print('Target Sequences:\n\t\t %s' % str(trg_seqs))
print('Baseline Sequences:')
for pred_seq, reward in zip(baseline_str_seqs, baselines):
print('\t\t[%f] %s' % (reward, ' '.join(pred_seq)))
print('Predict Sequences:')
for pred_seq, reward in zip(sampled_str_seqs, rewards):
print('\t\t[%f] %s' % (reward, ' '.join(pred_seq)))
"""
[policy_loss.append(-torch.stack(seq.logprobs, dim=0) * float(reward - baseline)) for seq, reward in zip(sampled_seqs, rewards)]
[policy_rewards.append(reward) for reward in rewards]
optimizer.zero_grad()
policy_loss = torch.cat(policy_loss).sum() * (1 - opt.loss_scale)
policy_loss.backward()
if opt.max_grad_norm > 0:
pre_norm = torch.nn.utils.clip_grad_norm(model.parameters(), opt.max_grad_norm)
after_norm = (sum([p.grad.data.norm(2) ** 2 for p in model.parameters() if p.grad is not None])) ** (1.0 / 2)
# logging.info('clip grad (%f -> %f)' % (pre_norm, after_norm))
optimizer.step()
return np.average(policy_rewards)
class RewardCache(object):
def __init__(self, capacity=2000):
# vanilla replay memory
self.capacity = capacity
self.memory = []
self.reset()
def push(self, stuff):
if len(self.memory) == self.capacity:
self.memory = self.memory[1:]
self.memory.append(stuff)
def get_average(self):
if len(self.memory) == 0:
return 0
return np.mean(np.array(self.memory))
def reset(self):
self.memory = []
def __len__(self):
return len(self.memory)
def train_rl_1(one2many_batch, model, optimizer, generator, opt, reward_cache):
src_list, src_len, trg_list, _, trg_copy_target_list, src_oov_map_list, oov_list = one2many_batch
if torch.cuda.is_available() and opt.useGpu:
src_list = src_list.cuda()
src_oov_map_list = src_oov_map_list.cuda()
# Sample number_batch sequences
sampled_seqs_list = generator.sample(src_list, src_len, src_oov_map_list, oov_list, opt.word2id, k=5, is_greedy=False)
policy_loss = []
policy_rewards = []
# Compute their rewards and losses
for seq_i, (src, trg, trg_copy, sampled_seqs, oov) in enumerate(zip(src_list, trg_list, trg_copy_target_list, sampled_seqs_list, oov_list)):
# convert to string sequences
sampled_str_seqs = [[opt.id2word[x] if x < opt.vocab_size else oov[x - opt.vocab_size] for x in to_cpu_list(seq.sentence)] for seq in sampled_seqs]
sampled_str_seqs = [seq[:seq.index(pykp.io.EOS_WORD) + 1] if pykp.io.EOS_WORD in seq else seq for seq in sampled_str_seqs]
# pad trg seqs with EOS to the same length
trg_seqs = [[opt.id2word[x] if x < opt.vocab_size else oov[x - opt.vocab_size] for x in seq] for seq in trg_copy]
# trg_seqs = [seq + [pykp.IO.EOS_WORD] * (opt.max_sent_length - len(seq)) for seq in trg_seqs]
# local rewards (bleu)
bleu_samples = get_match_result(true_seqs=trg_seqs, pred_seqs=sampled_str_seqs, type='bleu')
# global rewards
match_samples = get_match_result(true_seqs=trg_seqs, pred_seqs=sampled_str_seqs, type='exact')
_, _, fscore_samples = evaluate.evaluate(match_samples, sampled_str_seqs, trg_seqs, topk=5)
# compute the final rewards
alpha = 0.0
rewards = alpha * np.asarray(bleu_samples) + (1.0 - alpha) * fscore_samples
baseline = reward_cache.get_average()
for reward in rewards:
reward_cache.push(float(reward))
[policy_loss.append(-torch.stack(seq.logprobs, dim=0).sum() * float(reward - baseline)) for seq, reward in zip(sampled_seqs, rewards)]
[policy_rewards.append(reward) for reward in rewards]
optimizer.zero_grad()
policy_loss = torch.stack(policy_loss).mean() * (1 - opt.loss_scale)
policy_loss.backward()
if opt.max_grad_norm > 0:
pre_norm = torch.nn.utils.clip_grad_norm(model.parameters(), opt.max_grad_norm)
after_norm = (sum([p.grad.data.norm(2) ** 2 for p in model.parameters() if p.grad is not None])) ** (1.0 / 2)
# logging.info('clip grad (%f -> %f)' % (pre_norm, after_norm))
optimizer.step()
return np.average(policy_rewards)
def train_rl_2(one2many_batch, model, optimizer, generator, opt, reward_cache):
src_list, src_len, trg_list, _, trg_copy_target_list, src_oov_map_list, oov_list = one2many_batch
if torch.cuda.is_available() and opt.useGpu:
src_list = src_list.cuda()
src_oov_map_list = src_oov_map_list.cuda()
# Sample number_batch sequences
sampled_seqs_list = generator.sample(src_list, src_len, src_oov_map_list, oov_list, opt.word2id, k=5, is_greedy=False)
policy_loss = []
policy_rewards = []
# Compute their rewards and losses
for seq_i, (src, trg, trg_copy, sampled_seqs, oov) in enumerate(zip(src_list, trg_list, trg_copy_target_list, sampled_seqs_list, oov_list)):
# convert to string sequences
sampled_str_seqs = [[opt.id2word[x] if x < opt.vocab_size else oov[x - opt.vocab_size] for x in to_cpu_list(seq.sentence)] for seq in sampled_seqs]
sampled_str_seqs = [seq[:seq.index(pykp.io.EOS_WORD) + 1] if pykp.io.EOS_WORD in seq else seq for seq in sampled_str_seqs]
redundancy = self_redundancy(sampled_str_seqs)
reward = 1.0 - redundancy # the less redundant, the better
baseline = reward_cache.get_average()
reward_cache.push(float(reward))
[policy_loss.append(-torch.stack(seq.logprobs, dim=0).sum() * float(reward - baseline)) for seq in sampled_seqs]
policy_rewards.append(reward)
optimizer.zero_grad()
policy_loss = torch.stack(policy_loss).mean() * (1 - opt.loss_scale)
policy_loss.backward()
if opt.max_grad_norm > 0:
pre_norm = torch.nn.utils.clip_grad_norm(model.parameters(), opt.max_grad_norm)
after_norm = (sum([p.grad.data.norm(2) ** 2 for p in model.parameters() if p.grad is not None])) ** (1.0 / 2)
# logging.info('clip grad (%f -> %f)' % (pre_norm, after_norm))
optimizer.step()
return np.average(policy_rewards)
def train_rl(one2many_batch, model, optimizer, generator, opt, reward_cache):
if opt.rl_method == 0:
return train_rl_0(one2many_batch, model, optimizer, generator, opt)
elif opt.rl_method == 1:
return train_rl_1(one2many_batch, model, optimizer, generator, opt, reward_cache)
elif opt.rl_method == 2:
return train_rl_2(one2many_batch, model, optimizer, generator, opt, reward_cache)
def brief_report(epoch, batch_i, one2one_batch, loss_ml, decoder_log_probs, opt):
logging.info('====================== %d =========================' % (batch_i))
logging.info('Epoch : %d Minibatch : %d, Loss=%.5f' % (epoch, batch_i, np.mean(loss_ml)))
sampled_size = 2
logging.info('Printing predictions on %d sampled examples by greedy search' % sampled_size)
src, _, trg, trg_target, trg_copy_target, src_ext, oov_lists = one2one_batch
if torch.cuda.is_available() and opt.useGpu:
src = src.data.cpu().numpy()
decoder_log_probs = decoder_log_probs.data.cpu().numpy()
max_words_pred = decoder_log_probs.argmax(axis=-1)
trg_target = trg_target.data.cpu().numpy()
trg_copy_target = trg_copy_target.data.cpu().numpy()
else:
src = src.data.numpy()
decoder_log_probs = decoder_log_probs.data.numpy()
max_words_pred = decoder_log_probs.argmax(axis=-1)
trg_target = trg_target.data.numpy()
trg_copy_target = trg_copy_target.data.numpy()
sampled_trg_idx = np.random.random_integers(low=0, high=len(trg) - 1, size=sampled_size)
src = src[sampled_trg_idx]
oov_lists = [oov_lists[i] for i in sampled_trg_idx]
max_words_pred = [max_words_pred[i] for i in sampled_trg_idx]
decoder_log_probs = decoder_log_probs[sampled_trg_idx]
if not opt.copy_attention:
trg_target = [trg_target[i] for i in
sampled_trg_idx] # use the real target trg_loss (the starting <BOS> has been removed and contains oov ground-truth)
else:
trg_target = [trg_copy_target[i] for i in sampled_trg_idx]
for i, (src_wi, pred_wi, trg_i, oov_i) in enumerate(
zip(src, max_words_pred, trg_target, oov_lists)):
nll_prob = -np.sum([decoder_log_probs[i][l][pred_wi[l]] for l in range(len(trg_i))])
find_copy = np.any([x >= opt.vocab_size for x in src_wi])
has_copy = np.any([x >= opt.vocab_size for x in trg_i])
sentence_source = [opt.id2word[x] if x < opt.vocab_size else oov_i[x - opt.vocab_size] for x in
src_wi]
sentence_pred = [opt.id2word[x] if x < opt.vocab_size else oov_i[x - opt.vocab_size] for x in
pred_wi]
sentence_real = [opt.id2word[x] if x < opt.vocab_size else oov_i[x - opt.vocab_size] for x in
trg_i]
sentence_source = sentence_source[:sentence_source.index(
'<pad>')] if '<pad>' in sentence_source else sentence_source
sentence_pred = sentence_pred[
:sentence_pred.index('<pad>')] if '<pad>' in sentence_pred else sentence_pred
sentence_real = sentence_real[
:sentence_real.index('<pad>')] if '<pad>' in sentence_real else sentence_real
logging.info('==================================================')
logging.info('Source: %s ' % (' '.join(sentence_source)))
logging.info('\t\tPred : %s (%.4f)' % (' '.join(sentence_pred), nll_prob) + (
' [FIND COPY]' if find_copy else ''))
logging.info('\t\tReal : %s ' % (' '.join(sentence_real)) + (
' [HAS COPY]' + str(trg_i) if has_copy else ''))
def train_model(model, optimizer_ml, optimizer_rl, criterion, train_data_loader, valid_data_loader, test_data_loader, opt):
generator = SequenceGenerator(model,
opt.word_vec_size if opt.encoder_type == 'transformer' else opt.vocab_size,
eos_id=opt.word2id[pykp.io.EOS_WORD],
beam_size=opt.beam_size,
max_sequence_length=opt.max_sent_length,
useGpu = opt.useGpu
)
logging.info('====================== Checking GPU Availability =========================')
if torch.cuda.is_available() and opt.useGpu:
if isinstance(opt.gpuid, int):
opt.gpuid = [opt.gpuid]
logging.info('Running on GPU! devices=%s' % str(opt.gpuid))
# model = nn.DataParallel(model, device_ids=opt.gpuid)
else:
logging.info('Running on CPU!')
logging.info('====================== Start Training =========================')
checkpoint_names = []
train_ml_history_losses = []
train_rl_history_losses = []
valid_history_losses = []
test_history_losses = []
# best_loss = sys.float_info.max # for normal training/testing loss (likelihood)
best_loss = 0.0 # for f-score
stop_increasing = 0
train_ml_losses = []
train_rl_losses = []
total_batch = -1
early_stop_flag = False
if opt.train_rl:
reward_cache = RewardCache(2000)
if False: # opt.train_from:
state_path = opt.train_from.replace('.model', '.state')
logging.info('Loading training state from: %s' % state_path)
if os.path.exists(state_path):
(epoch, total_batch, best_loss, stop_increasing, checkpoint_names, train_ml_history_losses, train_rl_history_losses, valid_history_losses,
test_history_losses) = torch.load(open(state_path, 'rb'))
opt.start_epoch = epoch
for epoch in range(opt.start_epoch, opt.epochs):
if early_stop_flag:
break
progbar = Progbar(logger=logging, title='Training', target=len(train_data_loader), batch_size=train_data_loader.batch_size,
total_examples=len(train_data_loader.dataset.examples))
no_num = 0
for batch_i, batch in enumerate(train_data_loader):
model.train()
total_batch += 1
one2many_batch, one2one_batch = batch
report_loss = []
if batch_i == 100:
print ()
# Training
if opt.train_ml and (not opt.encoder_type == 'transformer' or (one2one_batch[0].size(0) * one2one_batch[0].size(1) <= 12800 and one2one_batch[0].size(1) <= 512)) and (not opt.encoder_type.startswith('bert') or (one2one_batch[0].size(0) * one2one_batch[0].size(1) <= 2560 and one2one_batch[0].size(1) <= 512)):
loss_ml, decoder_log_probs = train_ml(one2one_batch, model, optimizer_ml, criterion, opt)
train_ml_losses.append(loss_ml)
report_loss.append(('train_ml_loss', loss_ml))
report_loss.append(('PPL', loss_ml))
# Brief report
if batch_i % opt.report_every == 0:
brief_report(epoch, batch_i, one2one_batch, loss_ml, decoder_log_probs, opt)
else:
no_num += 1
print ("the number of batches not to be trained :", no_num)
print ()
# do not apply rl in 0th epoch, need to get a resonable model before that.
if opt.train_rl:
if epoch >= opt.rl_start_epoch:
loss_rl = train_rl(one2many_batch, model, optimizer_rl, generator, opt, reward_cache)
else:
loss_rl = 0.0
train_rl_losses.append(loss_rl)
report_loss.append(('train_rl_loss', loss_rl))
progbar.update(epoch, batch_i, report_loss)
# Validate and save checkpoint
if (opt.run_valid_every == -1 and batch_i == len(train_data_loader) - 1 and epoch % 5 == 0) or\
(opt.run_valid_every > -1 and total_batch > 1 and total_batch % opt.run_valid_every == 0):
logging.info('*' * 50)
logging.info('Run validing and testing @Epoch=%d,#(Total batch)=%d' % (epoch, total_batch))
# valid_losses = _valid_error(valid_data_loader, model, criterion, epoch, opt)
# valid_history_losses.append(valid_losses)
if opt.onlyTest:
test_score_dict = evaluate_beam_search(generator, test_data_loader, opt,
title='Testing, epoch=%d, batch=%d, total_batch=%d' % (
epoch, batch_i, total_batch), epoch=epoch,
predict_save_path=opt.pred_path + '/epoch%d_batch%d_total_batch%d' % (
epoch, batch_i, total_batch))
early_stop_flag = True
break
else:
valid_score_dict = evaluate_beam_search(generator, valid_data_loader, opt, title='Validating, epoch=%d, batch=%d, total_batch=%d' % (epoch, batch_i, total_batch), epoch=epoch, predict_save_path=opt.pred_path + '/epoch%d_batch%d_total_batch%d' % (epoch, batch_i, total_batch))
test_score_dict = evaluate_beam_search(generator, test_data_loader, opt, title='Testing, epoch=%d, batch=%d, total_batch=%d' % (epoch, batch_i, total_batch), epoch=epoch, predict_save_path=opt.pred_path + '/epoch%d_batch%d_total_batch%d' % (epoch, batch_i, total_batch))
checkpoint_names.append('epoch=%d-batch=%d-total_batch=%d' % (epoch, batch_i, total_batch))
curve_names = []
scores = []
if opt.train_ml:
train_ml_history_losses.append(copy.copy(train_ml_losses))
scores += [train_ml_history_losses]
curve_names += ['Training ML Error']
train_ml_losses = []
if opt.train_rl:
train_rl_history_losses.append(copy.copy(train_rl_losses))
scores += [train_rl_history_losses]
curve_names += ['Training RL Reward']
train_rl_losses = []
valid_history_losses.append(valid_score_dict)
test_history_losses.append(test_score_dict)
scores += [[result_dict[name] for result_dict in valid_history_losses] for name in opt.report_score_names]
curve_names += ['Valid-' + name for name in opt.report_score_names]
scores += [[result_dict[name] for result_dict in test_history_losses] for name in opt.report_score_names]
curve_names += ['Test-' + name for name in opt.report_score_names]
scores = [np.asarray(s) for s in scores]
# Plot the learning curve
plot_learning_curve_and_write_csv(scores=scores,
curve_names=curve_names,
checkpoint_names=checkpoint_names,
title='Training Validation & Test',
save_path=opt.exp_path + '/[epoch=%d,batch=%d,total_batch=%d]train_valid_test_curve.png' % (epoch, batch_i, total_batch))
'''
determine if early stop training (whether f-score increased, before is if valid error decreased)
'''
valid_loss = np.average(valid_history_losses[-1][opt.report_score_names[0]])
is_best_loss = valid_loss > best_loss
rate_of_change = float(valid_loss - best_loss) / float(best_loss) if float(best_loss) > 0 else 0.0
# valid error doesn't increase
if rate_of_change <= 0:
stop_increasing += 1
else:
stop_increasing = 0
if is_best_loss:
logging.info('Validation: update best loss (%.4f --> %.4f), rate of change (ROC)=%.2f' % (
best_loss, valid_loss, rate_of_change * 100))
else:
logging.info('Validation: best loss is not updated for %d times (%.4f --> %.4f), rate of change (ROC)=%.2f' % (
stop_increasing, best_loss, valid_loss, rate_of_change * 100))
best_loss = max(valid_loss, best_loss)
# only store the checkpoints that make better validation performances
if total_batch > 1 and (total_batch % opt.save_model_every == 0 or is_best_loss): # epoch >= opt.start_checkpoint_at and
# Save the checkpoint
logging.info('Saving checkpoint to: %s' % os.path.join(opt.model_path, '%s.epoch=%d.batch=%d.total_batch=%d.error=%f' % (opt.exp, epoch, batch_i, total_batch, valid_loss) + '.model'))
torch.save(
model.state_dict(),
open(os.path.join(opt.model_path, '%s.epoch=%d.batch=%d.total_batch=%d' % (opt.exp, epoch, batch_i, total_batch) + '.model'), 'wb')
)
torch.save(
(epoch, total_batch, best_loss, stop_increasing, checkpoint_names, train_ml_history_losses, train_rl_history_losses, valid_history_losses, test_history_losses),
open(os.path.join(opt.model_path, '%s.epoch=%d.batch=%d.total_batch=%d' % (opt.exp, epoch, batch_i, total_batch) + '.state'), 'wb')
)
if stop_increasing >= opt.early_stop_tolerance:
logging.info('Have not increased for %d epoches, early stop training' % stop_increasing)
early_stop_flag = True
break
logging.info('*' * 50)
'''
# only store the checkpoints that make better validation performances
if total_batch > 1 and (total_batch % opt.save_model_every == 0): # epoch >= opt.start_checkpoint_at and
# Save the checkpoint
logging.info('Saving checkpoint to: %s' % os.path.join(opt.model_path,
'%s.epoch=%d.batch=%d.total_batch=%d' % (
opt.exp, epoch, batch_i, total_batch) + '.model'))
torch.save(
model.state_dict(),
open(os.path.join(opt.model_path, '%s.epoch=%d.batch=%d.total_batch=%d' % (
opt.exp, epoch, batch_i, total_batch) + '.model'), 'wb')
)
'''
def load_data_vocab(opt, load_train=True):
logging.info("Loading vocab from disk: %s" % (opt.vocab))
word2id, id2word, vocab = torch.load(opt.vocab, 'rb')
pin_memory = torch.cuda.is_available() and opt.useGpu
# one2one data loader
logging.info("Loading train and validate data from '%s'" % opt.data)
logging.info('====================== Dataset =========================')
# one2many data loader
if load_train:
train_one2many = torch.load(opt.data + '.train.one2many.pt', 'rb')
train_one2many_dataset = KeyphraseDataset(train_one2many, word2id=word2id, id2word=id2word, type='one2many', include_original=False)
train_one2many_loader = KeyphraseDataLoader(dataset=train_one2many_dataset,
collate_fn=train_one2many_dataset.collate_fn_one2many if opt.useCLF else train_one2many_dataset.collate_fn_one2many_noBeginEnd,
num_workers=opt.batch_workers,
max_batch_example=1024,
max_batch_pair=opt.batch_size,
pin_memory=pin_memory,
shuffle=True)
logging.info('#(train data size: #(one2many pair)=%d, #(one2one pair)=%d, #(batch)=%d, #(average examples/batch)=%.3f' % (len(train_one2many_loader.dataset), train_one2many_loader.one2one_number(), len(train_one2many_loader), train_one2many_loader.one2one_number() / len(train_one2many_loader)))
else:
train_one2many_loader = None
valid_one2many = torch.load(opt.data + '.valid.one2many.pt', 'rb')
test_one2many = torch.load(opt.data + '.test.one2many.pt', 'rb')
# !important. As it takes too long to do beam search, thus reduce the size of validation and test datasets
valid_one2many = valid_one2many[:2000]
test_one2many = test_one2many#[:2000]
valid_one2many_dataset = KeyphraseDataset(valid_one2many, word2id=word2id, id2word=id2word, type='one2many', include_original=True)
test_one2many_dataset = KeyphraseDataset(test_one2many, word2id=word2id, id2word=id2word, type='one2many', include_original=True)
"""
# temporary code, exporting test data for Theano model
for e_id, e in enumerate(test_one2many_dataset.examples):
with open(os.path.join('data', 'new_kp20k_for_theano_model', 'text', '%d.txt' % e_id), 'w') as t_file:
t_file.write(' '.join(e['src_str']))
with open(os.path.join('data', 'new_kp20k_for_theano_model', 'keyphrase', '%d.txt' % e_id), 'w') as t_file:
t_file.writelines([(' '.join(t))+'\n' for t in e['trg_str']])
exit()
"""
valid_one2many_loader = KeyphraseDataLoader(dataset=valid_one2many_dataset,
collate_fn=valid_one2many_dataset.collate_fn_one2many if opt.useCLF else valid_one2many_dataset.collate_fn_one2many_noBeginEnd,
num_workers=opt.batch_workers,
max_batch_example=opt.beam_search_batch_example,
max_batch_pair=opt.beam_search_batch_size,
pin_memory=pin_memory,
shuffle=False)
test_one2many_loader = KeyphraseDataLoader(dataset=test_one2many_dataset,
collate_fn=test_one2many_dataset.collate_fn_one2many if opt.useCLF else test_one2many_dataset.collate_fn_one2many_noBeginEnd,
num_workers=opt.batch_workers,
max_batch_example=opt.beam_search_batch_example,
max_batch_pair=opt.beam_search_batch_size,
pin_memory=pin_memory,
shuffle=False)
opt.word2id = word2id
opt.id2word = id2word
opt.vocab = vocab
if not opt.decode_old:
opt.vocab_size = len(word2id)
logging.info('#(valid data size: #(one2many pair)=%d, #(one2one pair)=%d, #(batch)=%d' % (len(valid_one2many_loader.dataset), valid_one2many_loader.one2one_number(), len(valid_one2many_loader)))
logging.info('#(test data size: #(one2many pair)=%d, #(one2one pair)=%d, #(batch)=%d' % (len(test_one2many_loader.dataset), test_one2many_loader.one2one_number(), len(test_one2many_loader)))
logging.info('#(vocab)=%d' % len(word2id))
logging.info('#(vocab used)=%d' % opt.vocab_size)
return train_one2many_loader, valid_one2many_loader, test_one2many_loader, word2id, id2word, vocab
def init_optimizer_criterion(model, opt):
"""
mask the PAD <pad> when computing loss, before we used weight matrix, but not handy for copy-model, change to ignore_index
:param model:
:param opt:
:return:
"""
'''
if not opt.copy_attention:
weight_mask = torch.ones(opt.vocab_size).cuda() if torch.cuda.is_available() else torch.ones(opt.vocab_size)
else:
weight_mask = torch.ones(opt.vocab_size + opt.max_unk_words).cuda() if torch.cuda.is_available() else torch.ones(opt.vocab_size + opt.max_unk_words)
weight_mask[opt.word2id[pykp.IO.PAD_WORD]] = 0
criterion = torch.nn.NLLLoss(weight=weight_mask)
optimizer = Adam(params=filter(lambda p: p.requires_grad, model.parameters()), lr=opt.learning_rate)
# optimizer = torch.optim.Adadelta(model.parameters(), lr=0.1)
# optimizer = torch.optim.RMSprop(model.parameters(), lr=0.1)
'''
criterion = torch.nn.NLLLoss(ignore_index=opt.word2id[pykp.io.PAD_WORD])
if opt.train_ml:
optimizer_ml = Adam(params=filter(lambda p: p.requires_grad, model.parameters()), lr=opt.learning_rate)
else:
optimizer_ml = None
if opt.train_rl:
optimizer_rl = Adam(params=filter(lambda p: p.requires_grad, model.parameters()), lr=opt.learning_rate_rl)
else:
optimizer_rl = None
if torch.cuda.is_available() and opt.useGpu:
criterion = criterion.cuda()
return optimizer_ml, optimizer_rl, criterion
def init_model(opt):
logging.info('====================== Model Parameters =========================')
if opt.cascading_model:
model = Seq2SeqLSTMAttentionCascading(opt)
else:
if opt.copy_attention:
logging.info('Train a Seq2Seq model with Copy Mechanism')
else:
logging.info('Train a normal Seq2Seq model')
if opt.encoder_type == 'bert':
model = Seq2SeqBERT(opt)
elif opt.encoder_type == 'bert_low':
model = Seq2SeqBERTLow(opt)
elif opt.encoder_type == 'transformer':
model = Seq2SeqTransformer(opt)
else:
model = Seq2SeqLSTMAttention(opt)
if opt.train_from:
logging.info("loading previous checkpoint from %s" % opt.train_from)
# train_from_model_dir = opt.train_from[:opt.train_from.rfind('model/') + 6]
# load the saved the meta-model and override the current one
# model = torch.load(
# open(os.path.join(opt.model_path, opt.exp + '.initial.model'), 'rb')
# )
if torch.cuda.is_available() and opt.useGpu:
checkpoint = torch.load(open(opt.train_from, 'rb'))
else:
checkpoint = torch.load(
open(opt.train_from, 'rb'), map_location=lambda storage, loc: storage
)
# some compatible problems, keys are started with 'module.'
# checkpoint = dict([(k[7:], v) if k.startswith('module.') else (k, v) for k, v in checkpoint.items()])
model.load_state_dict(checkpoint)
else:
# dump the meta-model
torch.save(
model.state_dict(),
open(os.path.join(opt.train_from[: opt.train_from.find('.epoch=')], 'initial.model'), 'wb')
)
utils.tally_parameters(model)
return model
def main():
# load settings for training
opt = init_opt(description='train.py')
opt.useGpu = 1
opt.encoder_type = 'bert'
opt.useCLF = True
opt.data = 'data/kp20k/kp20k'
opt.vocab = 'data/kp20k/kp20k.vocab.pt'
if opt.encoder_type == 'transformer':
opt.batch_size = 32
opt.d_inner = 2048
opt.enc_n_layers = 4
opt.dec_n_layers = 2
opt.n_head = 8
opt.d_k = 64
opt.d_v = 64
opt.d_model = 512
opt.word_vec_size = 512
opt.run_valid_every = 5000000
opt.save_model_every = 20000
opt.decode_old = True
#opt.copy_attention = False
elif opt.encoder_type == 'bert':
opt.useOnlyTwo = False
opt.avgHidden = False
opt.useZeroDecodeHidden = True
opt.useSameEmbeding = False
opt.batch_size = 10
opt.max_sent_length = 10
opt.run_valid_every = 40000
opt.decode_old = False
opt.beam_search_batch_size = 10
opt.bert_model = 'bert-base-uncased'
opt.tokenizer = BertTokenizer.from_pretrained(opt.bert_model)
else:
opt.enc_layers = 2
opt.bidirectional = True
opt.decode_old = True
opt.run_valid_every = 2
opt.onlyTest = False
if opt.onlyTest:
opt.train_ml = False
opt.run_valid_every = 5
opt.beam_size = 64
opt.beam_search_batch_size = 128
#opt.train_from = 'exp/kp20k.ml.copy.20181129-193506/model/kp20k.ml.copy.epoch=1.batch=20000.total_batch=20000.model'
opt.train_from = 'exp/kp20k.ml.copy.20181128-153121/model/kp20k.ml.copy.epoch=2.batch=15495.total_batch=38000.model'
#opt.train_from = 'exp/kp20k.ml.copy.20181117-190121/model/kp20k.ml.copy.epoch=3.batch=14172.total_batch=56000.model'
logging = init_logging(logger_name='train.py', log_file=opt.log_file, redirect_to_stdout=False)
logging.info('EXP_PATH : ' + opt.exp_path)
logging.info('Parameters:')
[logging.info('%s : %s' % (k, str(v))) for k, v in opt.__dict__.items()]
logging.info('====================== Checking GPU Availability =========================')
if torch.cuda.is_available() and opt.useGpu:
if isinstance(opt.gpuid, int):
opt.gpuid = [opt.gpuid]
logging.info('Running on %s! devices=%s' % ('MULTIPLE GPUs' if len(opt.gpuid) > 1 else '1 GPU', str(opt.gpuid)))
else:
logging.info('Running on CPU!')
try:
train_data_loader, valid_data_loader, test_data_loader, word2id, id2word, vocab = load_data_vocab(opt)
model = init_model(opt)
if torch.cuda.is_available() and opt.useGpu:
model.cuda()
print ("model:")
print (model)
print ()
optimizer_ml, optimizer_rl, criterion = init_optimizer_criterion(model, opt)
if torch.cuda.is_available() and opt.useGpu:
criterion.cuda()
train_model(model, optimizer_ml, optimizer_rl, criterion, train_data_loader, valid_data_loader, test_data_loader, opt)
except Exception as e:
logging.error(e, exc_info=True)
raise
if __name__ == '__main__':
main()