forked from hrlinlp/entail_sum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnmt.py
1462 lines (1160 loc) · 63.3 KB
/
nmt.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
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import print_function
import re
import torch
import torch.nn as nn
import torch.nn.utils
from torch.autograd import Variable
from torch import optim
from torch.nn import Parameter
import torch.nn.functional as F
from torch.nn.utils.rnn import pad_packed_sequence, pack_padded_sequence
from nltk.translate.bleu_score import corpus_bleu, sentence_bleu, SmoothingFunction
import time
import numpy as np
from collections import defaultdict, Counter, namedtuple
from itertools import chain, islice
import argparse, os, sys
from util import read_corpus, data_iter, batch_slice, map_ent_label
from vocab import Vocab, VocabEntry
from process_samples import generate_hamming_distance_payoff_distribution
import math
from rougescore import *
from nli_models.baseline_snli_one import *
def init_config():
parser = argparse.ArgumentParser()
parser.add_argument('--seed', default=5783287, type=int, help='random seed')
parser.add_argument('--cuda', action='store_true', default=False, help='use gpu')
parser.add_argument('--mode', choices=['train', 'raml_train', 'test', 'sample', 'prob', 'interactive'],
default='train', help='run mode')
parser.add_argument('--vocab', type=str, help='path of the serialized vocabulary')
parser.add_argument('--switch', default=10, type=int, help='multi-task switch')
parser.add_argument('--batch_size', default=64, type=int, help='batch size')
parser.add_argument('--beam_size', default=10, type=int, help='beam size for beam search')
parser.add_argument('--sample_size', default=5, type=int, help='sample size')
parser.add_argument('--embed_size', default=300, type=int, help='size of word embeddings')
parser.add_argument('--hidden_size', default=512, type=int, help='size of LSTM hidden states')
parser.add_argument('--dropout', default=0.5, type=float, help='dropout rate')
parser.add_argument('--train_src', type=str, help='path to the training source file')
parser.add_argument('--train_tgt', type=str, help='path to the training target file')
parser.add_argument('--train_ent_x', type=str, help='path to the training entailment x file')
parser.add_argument('--train_ent_y', type=str, help='path to the training entailment y file')
parser.add_argument('--train_ent_label', type=str, help='path to the training entailment label file')
parser.add_argument('--dev_src', type=str, help='path to the dev source file')
parser.add_argument('--dev_tgt', type=str, help='path to the dev target file')
parser.add_argument('--test_src', type=str, help='path to the test source file')
parser.add_argument('--test_tgt', type=str, help='path to the test target file')
parser.add_argument('--decode_max_time_step', default=20, type=int, help='maximum number of time steps used '
'in decoding and sampling')
parser.add_argument('--valid_niter', default=2000, type=int, help='every n iterations to perform validation')
parser.add_argument('--valid_metric', default='rouge2_f', choices=['rouge2_f', 'bleu', 'ppl', 'word_acc', 'sent_acc'], help='metric used for validation')
parser.add_argument('--log_every', default=500, type=int, help='every n iterations to log training statistics')
parser.add_argument('--load_model', default=None, type=str, help='load a pre-trained model')
parser.add_argument('--save_to', default='model', type=str, help='save trained model to')
parser.add_argument('--save_model_after', default=0, help='save the model only after n validation iterations')
parser.add_argument('--save_to_file', default=None, type=str, help='if provided, save decoding results to file')
parser.add_argument('--save_nbest', default=False, action='store_true', help='save nbest decoding results')
parser.add_argument('--patience', default=30, type=int, help='training patience')
parser.add_argument('--drop_patience', default=12, type=int, help='training lr drop patience')
parser.add_argument('--uniform_init', default=0.1, type=float, help='if specified, use uniform initialization for all parameters')
parser.add_argument('--clip_grad', default=5., type=float, help='clip gradients')
parser.add_argument('--max_niter', default=-1, type=int, help='maximum number of training iterations')
parser.add_argument('--lr', default=0.0005, type=float, help='learning rate')
parser.add_argument('--lr_decay', default=0.5, type=float, help='decay learning rate if the validation performance drops')
# raml training
parser.add_argument('--debug', default=False, action='store_true')
parser.add_argument('--temp', default=0.85, type=float, help='temperature in reward distribution')
parser.add_argument('--raml_sample_mode', default='pre_sample',
choices=['pre_sample', 'hamming_distance', 'hamming_distance_impt_sample'],
help='sample mode when using RAML')
parser.add_argument('--raml_sample_file', type=str, help='path to the sampled targets')
parser.add_argument('--raml_bias_groundtruth', action='store_true', default=False, help='make sure ground truth y* is in samples')
parser.add_argument('--reward_type', default='entailment', type=str, choices=['rouge2_f', 'bleu', 'entailment'])
parser.add_argument('--smooth_bleu', action='store_true', default=False,
help='smooth sentence level BLEU score.')
#TODO: greedy sampling is still buggy!
parser.add_argument('--sample_method', default='random', choices=['random', 'greedy'])
args = parser.parse_args()
# seed the RNG
torch.manual_seed(args.seed)
if args.cuda:
torch.cuda.manual_seed(args.seed)
np.random.seed(args.seed * 13 / 7)
return args
def input_transpose(sents, pad_token):
max_len = max(len(s) for s in sents)
batch_size = len(sents)
sents_t = []
masks = []
for i in xrange(max_len):
sents_t.append([sents[k][i] if len(sents[k]) > i else pad_token for k in xrange(batch_size)])
masks.append([1 if len(sents[k]) > i else 0 for k in xrange(batch_size)])
return sents_t, masks
def word2id(sents, vocab):
if type(sents[0]) == list:
return [[vocab[w] for w in s] for s in sents]
else:
return [vocab[w] for w in sents]
def tensor_transform(linear, X):
# X is a 3D tensor
return linear(X.contiguous().view(-1, X.size(2))).view(X.size(0), X.size(1), -1)
def sentence_entailment(src, targ, word_dic, input_encoder, inter_atten):
if '<unk>' in src:
src.remove('<unk>')
if '<unk>' in targ:
targ.remove('<unk>')
src = ['<s>'] + src
targ = ['<s>'] + targ
src = [word_dic[w] for w in src if w in word_dic]
targ = [word_dic[w] for w in targ if w in word_dic]
source = Variable((torch.from_numpy(np.array(src, dtype=int)) - 1).cuda())
target = Variable((torch.from_numpy(np.array(targ, dtype=int)) - 1).cuda())
test_src_linear, test_tgt_linear=input_encoder(source, target)
log_prob=inter_atten(test_src_linear, test_tgt_linear)
scores = F.softmax(log_prob).data.cpu().numpy()[0]
reward = float(scores[0])
return reward
class NMT(nn.Module):
def __init__(self, args, vocab):
super(NMT, self).__init__()
self.args = args
self.vocab = vocab
self.src_embed = nn.Embedding(len(vocab.src), args.embed_size, padding_idx=vocab.src['<pad>'])
self.tgt_embed = nn.Embedding(len(vocab.tgt), args.embed_size, padding_idx=vocab.tgt['<pad>'])
self.encoder_lstm = nn.LSTM(args.embed_size, args.hidden_size, bidirectional=True, dropout=args.dropout)
self.decoder_lstm = nn.LSTMCell(args.embed_size + args.hidden_size, args.hidden_size)
# attention: dot product attention
# project source encoding to decoder rnn's h space
self.att_src_linear = nn.Linear(args.hidden_size * 2, args.hidden_size, bias=False)
# transformation of decoder hidden states and context vectors before reading out target words
# this produces the `attentional vector` in (Luong et al., 2015)
self.att_vec_linear = nn.Linear(args.hidden_size * 2 + args.hidden_size, args.hidden_size, bias=False)
# prediction layer of the target vocabulary
self.readout = nn.Linear(args.hidden_size, len(vocab.tgt), bias=False)
# dropout layer
self.dropout = nn.Dropout(args.dropout)
self.decoder_cell_init = nn.Linear(args.hidden_size * 2, args.hidden_size)
self.decoder_hidden_init = nn.Linear(args.hidden_size * 2, args.hidden_size)
self.classifier = nn.Sequential(
nn.Dropout(p=args.dropout),
nn.Linear(args.hidden_size * 8, 512),
nn.Tanh(),
nn.Dropout(p=args.dropout),
nn.Linear(512, 512),
nn.Tanh(),
nn.Dropout(p=args.dropout),
nn.Linear(512, 3),
)
#self.sigmoid = nn.Sigmoid()
def forward(self, task, src_sents, src_sents_len, tgt_words, tgt_sents_len):
if task == "summarization":
src_encodings, sv, init_ctx_vec = self.encode(src_sents, src_sents_len)
scores = self.decode(src_encodings, init_ctx_vec, tgt_words)
elif task == "entailment":
src_encodings, src_sentence_vectors, init_ctx_vec_src = self.encode(src_sents, src_sents_len)
tgt_sentence_vectors = self.encode_sort(tgt_words, tgt_sents_len)
features = torch.cat((src_sentence_vectors, tgt_sentence_vectors, torch.abs(src_sentence_vectors - tgt_sentence_vectors), \
src_sentence_vectors * tgt_sentence_vectors), 1)
scores = self.classifier(features)
return scores
def encode_sort(self, src_sents, src_sents_len):
"""
:param src_sents: (src_sent_len, batch_size), sorted by the length of the source
:param src_sents_len: (src_sent_len)
"""
src_sents_len_sort, idx_sort = np.sort(src_sents_len)[::-1], np.argsort(src_sents_len)[::-1]
idx_sort = list(idx_sort)
src_sents_sort = src_sents.index_select(1, Variable(torch.cuda.LongTensor(idx_sort)))
#src_sents_sort = torch.index_select(src_sents, 1, Variable(torch.cuda.LongTensor(idx_sort)))
# (src_sent_len, batch_size, embed_size)
src_word_embed = self.src_embed(src_sents_sort)
packed_src_embed = pack_padded_sequence(src_word_embed, src_sents_len_sort)
# output: (src_sent_len, batch_size, hidden_size)
output, (last_state, last_cell) = self.encoder_lstm(packed_src_embed)
sentence_vector = torch.cat([last_state[0], last_state[1]], 1)
idx_unsort = np.argsort(idx_sort)
sentence_vector = sentence_vector.index_select(0, Variable(torch.cuda.LongTensor(idx_unsort)))
return sentence_vector
def encode(self, src_sents, src_sents_len):
"""
:param src_sents: (src_sent_len, batch_size), sorted by the length of the source
:param src_sents_len: (src_sent_len)
"""
# (src_sent_len, batch_size, embed_size)
src_word_embed = self.src_embed(src_sents)
packed_src_embed = pack_padded_sequence(src_word_embed, src_sents_len)
# output: (src_sent_len, batch_size, hidden_size)
output, (last_state, last_cell) = self.encoder_lstm(packed_src_embed)
output, _ = pad_packed_sequence(output)
#dec_init_state = F.tanh(self.decoder_init(torch.cat([last_state[0], last_state[1]], 1)))
dec_init_cell = F.tanh(self.decoder_cell_init(torch.cat([last_cell[0], last_cell[1]], 1)))
dec_init_state = F.tanh(self.decoder_hidden_init(torch.cat([last_state[0], last_state[1]], 1)))
sentence_vector = torch.cat([last_state[0], last_state[1]], 1)
return output, sentence_vector, (dec_init_state, dec_init_cell)
def decode(self, src_encoding, dec_init_vec, tgt_sents):
"""
:param src_encoding: (src_sent_len, batch_size, hidden_size)
:param dec_init_vec: (batch_size, hidden_size)
:param tgt_sents: (tgt_sent_len, batch_size)
:return:
"""
init_state = dec_init_vec[0]
init_cell = dec_init_vec[1]
hidden = (init_state, init_cell)
new_tensor = init_cell.data.new
batch_size = src_encoding.size(1)
# (batch_size, src_sent_len, hidden_size * 2)
src_encoding = src_encoding.permute(1, 0, 2)
# (batch_size, src_sent_len, hidden_size)
src_encoding_att_linear = tensor_transform(self.att_src_linear, src_encoding)
# initialize attentional vector
att_tm1 = Variable(new_tensor(batch_size, self.args.hidden_size).zero_(), requires_grad=False)
tgt_word_embed = self.tgt_embed(tgt_sents)
scores = []
# start from `<s>`, until y_{T-1}
for y_tm1_embed in tgt_word_embed.split(split_size=1):
# input feeding: concate y_tm1 and previous attentional vector
x = torch.cat([y_tm1_embed.squeeze(0), att_tm1], 1)
# h_t: (batch_size, hidden_size)
h_t, cell_t = self.decoder_lstm(x, hidden)
h_t = self.dropout(h_t)
ctx_t, alpha_t = self.dot_prod_attention(h_t, src_encoding, src_encoding_att_linear)
att_t = F.tanh(self.att_vec_linear(torch.cat([h_t, ctx_t], 1))) # E.q. (5)
att_t = self.dropout(att_t)
score_t = self.readout(att_t) # E.q. (6)
scores.append(score_t)
att_tm1 = att_t
hidden = h_t, cell_t
scores = torch.stack(scores)
return scores
def translate(self, src_sents, beam_size=None, to_word=True):
"""
perform beam search
TODO: batched beam search
"""
if not type(src_sents[0]) == list:
src_sents = [src_sents]
if not beam_size:
beam_size = args.beam_size
src_sents_var = to_input_variable(src_sents, self.vocab.src, cuda=args.cuda, is_test=True)
src_encoding, sv, dec_init_vec = self.encode(src_sents_var, [len(src_sents[0])])
src_encoding_att_linear = tensor_transform(self.att_src_linear, src_encoding)
init_state = dec_init_vec[0]
init_cell = dec_init_vec[1]
hidden = (init_state, init_cell)
att_tm1 = Variable(torch.zeros(1, self.args.hidden_size), volatile=True)
hyp_scores = Variable(torch.zeros(1), volatile=True)
if args.cuda:
att_tm1 = att_tm1.cuda()
hyp_scores = hyp_scores.cuda()
eos_id = self.vocab.tgt['</s>']
bos_id = self.vocab.tgt['<s>']
tgt_vocab_size = len(self.vocab.tgt)
hypotheses = [[bos_id]]
completed_hypotheses = []
completed_hypothesis_scores = []
t = 0
while len(completed_hypotheses) < beam_size and t < args.decode_max_time_step:
t += 1
hyp_num = len(hypotheses)
expanded_src_encoding = src_encoding.expand(src_encoding.size(0), hyp_num, src_encoding.size(2))
expanded_src_encoding_att_linear = src_encoding_att_linear.expand(src_encoding_att_linear.size(0), hyp_num, src_encoding_att_linear.size(2))
y_tm1 = Variable(torch.LongTensor([hyp[-1] for hyp in hypotheses]), volatile=True)
if args.cuda:
y_tm1 = y_tm1.cuda()
y_tm1_embed = self.tgt_embed(y_tm1)
x = torch.cat([y_tm1_embed, att_tm1], 1)
# h_t: (hyp_num, hidden_size)
h_t, cell_t = self.decoder_lstm(x, hidden)
h_t = self.dropout(h_t)
ctx_t, alpha_t = self.dot_prod_attention(h_t, expanded_src_encoding.permute(1, 0, 2), expanded_src_encoding_att_linear.permute(1, 0, 2))
att_t = F.tanh(self.att_vec_linear(torch.cat([h_t, ctx_t], 1)))
att_t = self.dropout(att_t)
score_t = self.readout(att_t)
p_t = F.log_softmax(score_t)
live_hyp_num = beam_size - len(completed_hypotheses)
new_hyp_scores = (hyp_scores.unsqueeze(1).expand_as(p_t) + p_t).view(-1)
top_new_hyp_scores, top_new_hyp_pos = torch.topk(new_hyp_scores, k=live_hyp_num)
prev_hyp_ids = top_new_hyp_pos / tgt_vocab_size
word_ids = top_new_hyp_pos % tgt_vocab_size
# new_hyp_scores = new_hyp_scores[top_new_hyp_pos.data]
new_hypotheses = []
live_hyp_ids = []
new_hyp_scores = []
for prev_hyp_id, word_id, new_hyp_score in zip(prev_hyp_ids.cpu().data, word_ids.cpu().data, top_new_hyp_scores.cpu().data):
hyp_tgt_words = hypotheses[prev_hyp_id] + [word_id]
if word_id == eos_id:
completed_hypotheses.append(hyp_tgt_words)
completed_hypothesis_scores.append(new_hyp_score)
else:
new_hypotheses.append(hyp_tgt_words)
live_hyp_ids.append(prev_hyp_id)
new_hyp_scores.append(new_hyp_score)
if len(completed_hypotheses) == beam_size:
break
live_hyp_ids = torch.LongTensor(live_hyp_ids)
if args.cuda:
live_hyp_ids = live_hyp_ids.cuda()
hidden = (h_t[live_hyp_ids], cell_t[live_hyp_ids])
att_tm1 = att_t[live_hyp_ids]
hyp_scores = Variable(torch.FloatTensor(new_hyp_scores), volatile=True) # new_hyp_scores[live_hyp_ids]
if args.cuda:
hyp_scores = hyp_scores.cuda()
hypotheses = new_hypotheses
if len(completed_hypotheses) == 0:
completed_hypotheses = [hypotheses[0]]
completed_hypothesis_scores = [0.0]
if to_word:
for i, hyp in enumerate(completed_hypotheses):
completed_hypotheses[i] = [self.vocab.tgt.id2word[w] for w in hyp]
ranked_hypotheses = sorted(zip(completed_hypotheses, completed_hypothesis_scores), key=lambda x: x[1], reverse=True)
return [hyp for hyp, score in ranked_hypotheses]
def sample(self, src_sents, sample_size=None, to_word=False):
if not type(src_sents[0]) == list:
src_sents = [src_sents]
if not sample_size:
sample_size = args.sample_size
src_sents_num = len(src_sents)
batch_size = src_sents_num * sample_size
src_sents_var = to_input_variable(src_sents, self.vocab.src, cuda=args.cuda, is_test=True)
src_encoding, sv, (dec_init_state, dec_init_cell) = self.encode(src_sents_var, [len(s) for s in src_sents])
dec_init_state = dec_init_state.repeat(sample_size, 1)
dec_init_cell = dec_init_cell.repeat(sample_size, 1)
hidden = (dec_init_state, dec_init_cell)
src_encoding = src_encoding.repeat(1, sample_size, 1)
src_encoding_att_linear = tensor_transform(self.att_src_linear, src_encoding)
src_encoding = src_encoding.permute(1, 0, 2)
src_encoding_att_linear = src_encoding_att_linear.permute(1, 0, 2)
new_tensor = dec_init_state.data.new
att_tm1 = Variable(new_tensor(batch_size, self.args.hidden_size).zero_(), volatile=True)
y_0 = Variable(torch.LongTensor([self.vocab.tgt['<s>'] for _ in xrange(batch_size)]), volatile=True)
eos = self.vocab.tgt['</s>']
# eos_batch = torch.LongTensor([eos] * batch_size)
sample_ends = torch.ByteTensor([0] * batch_size)
all_ones = torch.ByteTensor([1] * batch_size)
if args.cuda:
y_0 = y_0.cuda()
sample_ends = sample_ends.cuda()
all_ones = all_ones.cuda()
samples = [y_0]
t = 0
while t < args.decode_max_time_step:
t += 1
# (sample_size)
y_tm1 = samples[-1]
y_tm1_embed = self.tgt_embed(y_tm1)
x = torch.cat([y_tm1_embed, att_tm1], 1)
# h_t: (batch_size, hidden_size)
h_t, cell_t = self.decoder_lstm(x, hidden)
h_t = self.dropout(h_t)
ctx_t, alpha_t = self.dot_prod_attention(h_t, src_encoding, src_encoding_att_linear)
att_t = F.tanh(self.att_vec_linear(torch.cat([h_t, ctx_t], 1))) # E.q. (5)
att_t = self.dropout(att_t)
score_t = self.readout(att_t) # E.q. (6)
p_t = F.softmax(score_t)
if args.sample_method == 'random':
y_t = torch.multinomial(p_t, num_samples=1).squeeze(1)
elif args.sample_method == 'greedy':
_, y_t = torch.topk(p_t, k=1, dim=1)
y_t = y_t.squeeze(1)
samples.append(y_t)
sample_ends |= torch.eq(y_t, eos).byte().data
if torch.equal(sample_ends, all_ones):
break
# if torch.equal(y_t.data, eos_batch):
# break
att_tm1 = att_t
hidden = h_t, cell_t
# post-processing
completed_samples = [list([list() for _ in xrange(sample_size)]) for _ in xrange(src_sents_num)]
for y_t in samples:
for i, sampled_word in enumerate(y_t.cpu().data):
src_sent_id = i % src_sents_num
sample_id = i / src_sents_num
if len(completed_samples[src_sent_id][sample_id]) == 0 or completed_samples[src_sent_id][sample_id][-1] != eos:
completed_samples[src_sent_id][sample_id].append(sampled_word)
if to_word:
for i, src_sent_samples in enumerate(completed_samples):
completed_samples[i] = word2id(src_sent_samples, self.vocab.tgt.id2word)
return completed_samples
def attention(self, h_t, src_encoding, src_linear_for_att):
# (1, batch_size, attention_size) + (src_sent_len, batch_size, attention_size) =>
# (src_sent_len, batch_size, attention_size)
att_hidden = F.tanh(self.att_h_linear(h_t).unsqueeze(0).expand_as(src_linear_for_att) + src_linear_for_att)
# (batch_size, src_sent_len)
att_weights = F.softmax(tensor_transform(self.att_vec_linear, att_hidden).squeeze(2).permute(1, 0))
# (batch_size, hidden_size * 2)
ctx_vec = torch.bmm(src_encoding.permute(1, 2, 0), att_weights.unsqueeze(2)).squeeze(2)
return ctx_vec, att_weights
def dot_prod_attention(self, h_t, src_encoding, src_encoding_att_linear, mask=None):
"""
:param h_t: (batch_size, hidden_size)
:param src_encoding: (batch_size, src_sent_len, hidden_size * 2)
:param src_encoding_att_linear: (batch_size, src_sent_len, hidden_size)
:param mask: (batch_size, src_sent_len)
"""
# (batch_size, src_sent_len)
att_weight = torch.bmm(src_encoding_att_linear, h_t.unsqueeze(2)).squeeze(2)
if mask:
att_weight.data.masked_fill_(mask, -float('inf'))
att_weight = F.softmax(att_weight)
att_view = (att_weight.size(0), 1, att_weight.size(1))
# (batch_size, hidden_size)
ctx_vec = torch.bmm(att_weight.view(*att_view), src_encoding).squeeze(1)
return ctx_vec, att_weight
def save(self, path):
print('save parameters to [%s]' % path, file=sys.stderr)
params = {
'args': self.args,
'vocab': self.vocab,
'state_dict': self.state_dict()
}
torch.save(params, path)
def to_input_variable(sents, vocab, cuda=False, is_test=False):
"""
return a tensor of shape (src_sent_len, batch_size)
"""
word_ids = word2id(sents, vocab)
sents_t, masks = input_transpose(word_ids, vocab['<pad>'])
sents_var = Variable(torch.LongTensor(sents_t), volatile=is_test, requires_grad=False)
if cuda:
sents_var = sents_var.cuda()
return sents_var
def evaluate_loss(model, data, crit):
model.eval()
cum_loss = 0.
cum_tgt_words = 0.
for src_sents, tgt_sents in data_iter(data, batch_size=args.batch_size, shuffle=False):
pred_tgt_word_num = sum(len(s[1:]) for s in tgt_sents) # omitting leading `<s>`
src_sents_len = [len(s) for s in src_sents]
src_sents_var = to_input_variable(src_sents, model.vocab.src, cuda=args.cuda, is_test=True)
tgt_sents_var = to_input_variable(tgt_sents, model.vocab.tgt, cuda=args.cuda, is_test=True)
# (tgt_sent_len, batch_size, tgt_vocab_size)
scores = model('summarization', src_sents_var, src_sents_len, tgt_sents_var[:-1], src_sents_len)
loss = crit(scores.view(-1, scores.size(2)), tgt_sents_var[1:].view(-1))
cum_loss += loss.data[0]
cum_tgt_words += pred_tgt_word_num
loss = cum_loss / cum_tgt_words
return loss
def init_training(args):
vocab = torch.load(args.vocab)
model = NMT(args, vocab)
model.train()
if args.uniform_init:
print('uniformly initialize parameters [-%f, +%f]' % (args.uniform_init, args.uniform_init), file=sys.stderr)
for p in model.parameters():
p.data.uniform_(-args.uniform_init, args.uniform_init)
vocab_mask = torch.ones(len(vocab.tgt))
vocab_mask[vocab.tgt['<pad>']] = 0
nll_loss = nn.NLLLoss(weight=vocab_mask, size_average=False)
cross_entropy_loss = nn.CrossEntropyLoss(weight=vocab_mask, size_average=False)
weight = torch.FloatTensor(3).fill_(1)
loss_ent = nn.CrossEntropyLoss(weight=weight, size_average=False)
if args.cuda:
model = model.cuda()
nll_loss = nll_loss.cuda()
cross_entropy_loss = cross_entropy_loss.cuda()
loss_ent = loss_ent.cuda()
optimizer = torch.optim.Adam(model.parameters(), lr=args.lr)
return vocab, model, optimizer, nll_loss, cross_entropy_loss, loss_ent
def cont_training_halve_raml(args, lr_now):
load_model_dir = args.save_to + '.bin'
print('load model from [%s]' % load_model_dir, file=sys.stderr)
params = torch.load(load_model_dir, map_location=lambda storage, loc: storage)
vocab = params['vocab']
state_dict = params['state_dict']
model = NMT(args, vocab)
model.train()
model.load_state_dict(state_dict)
vocab_mask = torch.ones(len(vocab.tgt))
vocab_mask[vocab.tgt['<pad>']] = 0
nll_loss = nn.NLLLoss(weight=vocab_mask, size_average=False)
cross_entropy_loss = nn.CrossEntropyLoss(weight=vocab_mask, size_average=False)
if args.cuda:
model = model.cuda()
nll_loss = nll_loss.cuda()
cross_entropy_loss = cross_entropy_loss.cuda()
optimizer = torch.optim.Adam(model.parameters(), lr=lr_now)
return vocab, model, optimizer, nll_loss, cross_entropy_loss
def cont_training_halve_ent(args, lr_now):
load_model_dir = args.save_to + '.bin'
print('load model from [%s]' % load_model_dir, file=sys.stderr)
params = torch.load(load_model_dir, map_location=lambda storage, loc: storage)
vocab = params['vocab']
state_dict = params['state_dict']
model = NMT(args, vocab)
model.train()
model.load_state_dict(state_dict)
vocab_mask = torch.ones(len(vocab.tgt))
vocab_mask[vocab.tgt['<pad>']] = 0
nll_loss = nn.NLLLoss(weight=vocab_mask, size_average=False)
cross_entropy_loss = nn.CrossEntropyLoss(weight=vocab_mask, size_average=False)
weight = torch.FloatTensor(3).fill_(1)
loss_ent = nn.CrossEntropyLoss(weight=weight, size_average=False)
if args.cuda:
model = model.cuda()
nll_loss = nll_loss.cuda()
cross_entropy_loss = cross_entropy_loss.cuda()
loss_ent = loss_ent.cuda()
optimizer = torch.optim.Adam(model.parameters(), lr=lr_now)
return vocab, model, optimizer, nll_loss, cross_entropy_loss, loss_ent
def cont_training_raml(args):
print('load model from [%s]' % args.load_model, file=sys.stderr)
params = torch.load(args.load_model, map_location=lambda storage, loc: storage)
vocab = params['vocab']
state_dict = params['state_dict']
model = NMT(args, vocab)
model.train()
model.load_state_dict(state_dict, strict=False)
vocab_mask = torch.ones(len(vocab.tgt))
vocab_mask[vocab.tgt['<pad>']] = 0
nll_loss = nn.NLLLoss(weight=vocab_mask, size_average=False)
cross_entropy_loss = nn.CrossEntropyLoss(weight=vocab_mask, size_average=False)
if args.cuda:
model = model.cuda()
nll_loss = nll_loss.cuda()
cross_entropy_loss = cross_entropy_loss.cuda()
optimizer = torch.optim.Adam(model.parameters(), lr=args.lr)
return vocab, model, optimizer, nll_loss, cross_entropy_loss
def cont_training(args):
print('load model from [%s]' % args.load_model, file=sys.stderr)
params = torch.load(args.load_model, map_location=lambda storage, loc: storage)
vocab = params['vocab']
state_dict = params['state_dict']
model = NMT(args, vocab)
model.train()
model.load_state_dict(state_dict, strict=False)
vocab_mask = torch.ones(len(vocab.tgt))
vocab_mask[vocab.tgt['<pad>']] = 0
nll_loss = nn.NLLLoss(weight=vocab_mask, size_average=False)
cross_entropy_loss = nn.CrossEntropyLoss(weight=vocab_mask, size_average=False)
weight = torch.FloatTensor(3).fill_(1)
loss_ent = nn.CrossEntropyLoss(weight=weight, size_average=False)
if args.cuda:
model = model.cuda()
nll_loss = nll_loss.cuda()
cross_entropy_loss = cross_entropy_loss.cuda()
loss_ent = loss_ent.cuda()
optimizer = torch.optim.Adam(model.parameters(), lr=args.lr)
return vocab, model, optimizer, nll_loss, cross_entropy_loss, loss_ent
def train(args):
train_data_src = read_corpus(args.train_src, source='src')
train_data_tgt = read_corpus(args.train_tgt, source='tgt')
train_data_ent_x = read_corpus(args.train_ent_x, source='src')
train_data_ent_y = read_corpus(args.train_ent_y, source='src')
train_data_ent_label = read_corpus(args.train_ent_label, source='src')
train_data_ent_label = map_ent_label(train_data_ent_label)
dev_data_src = read_corpus(args.dev_src, source='src')
dev_data_tgt = read_corpus(args.dev_tgt, source='tgt')
train_data = zip(train_data_src, train_data_tgt)
train_data_ent = zip(train_data_ent_x, train_data_ent_y, train_data_ent_label)
dev_data = zip(dev_data_src, dev_data_tgt)
if not args.load_model:
vocab, model , optimizer, nll_loss, cross_entropy_loss, loss_ent = init_training(args)
else:
vocab, model, optimizer, nll_loss, cross_entropy_loss, loss_ent = cont_training(args)
train_iter = patience = drop_patience = cum_loss = report_loss = cum_tgt_words = report_tgt_words = 0
cum_examples = cum_batches = report_examples = valid_num = best_model_iter = 0
train_multi_task_iter = 0
train_ent_iter = 0
hist_valid_scores = []
train_time = begin_time = time.time()
print('begin Maximum Likelihood training')
sum_data_generator = data_iter(train_data, batch_size=args.batch_size)
ent_data_generator = data_iter(train_data_ent, batch_size=args.batch_size)
while True:
train_multi_task_iter += 1
for train_iter in range((train_multi_task_iter - 1) * 100, train_multi_task_iter * 100):
try:
src_sents, tgt_sents = next(sum_data_generator)
except:
sum_data_generator = data_iter(train_data, batch_size=args.batch_size)
src_sents, tgt_sents = next(sum_data_generator)
src_sents_var = to_input_variable(src_sents, vocab.src, cuda=args.cuda)
tgt_sents_var = to_input_variable(tgt_sents, vocab.tgt, cuda=args.cuda)
batch_size = len(src_sents)
src_sents_len = [len(s) for s in src_sents]
pred_tgt_word_num = sum(len(s[1:]) for s in tgt_sents) # omitting leading `<s>`
optimizer.zero_grad()
# (tgt_sent_len, batch_size, tgt_vocab_size)
scores = model('summarization', src_sents_var, src_sents_len, tgt_sents_var[:-1], src_sents_len)
word_loss = cross_entropy_loss(scores.view(-1, scores.size(2)), tgt_sents_var[1:].view(-1))
loss = word_loss / batch_size
word_loss_val = word_loss.data[0]
loss_val = loss.data[0]
loss.backward()
# clip gradient
grad_norm = torch.nn.utils.clip_grad_norm(model.parameters(), args.clip_grad)
optimizer.step()
report_loss += word_loss_val
cum_loss += word_loss_val
report_tgt_words += pred_tgt_word_num
cum_tgt_words += pred_tgt_word_num
report_examples += batch_size
cum_examples += batch_size
cum_batches += batch_size
if train_iter > 0 and train_iter % args.log_every == 0:
print('iter %d, avg. loss %.2f, avg. ppl %.2f ' \
'cum. examples %d, speed %.2f words/sec, time elapsed %.2f sec' % (train_iter,
report_loss / report_examples,
np.exp(report_loss / report_tgt_words),
cum_examples,
report_tgt_words / (time.time() - train_time),
time.time() - begin_time), file=sys.stderr)
train_time = time.time()
report_loss = report_tgt_words = report_examples = 0.
# perform validation
if train_iter > 0 and train_iter % args.valid_niter == 0:
print('iter %d, cum. loss %.2f, cum. ppl %.2f cum. examples %d' % (train_iter,
cum_loss / cum_batches,
np.exp(cum_loss / cum_tgt_words),
cum_examples), file=sys.stderr)
cum_loss = cum_batches = cum_tgt_words = 0.
valid_num += 1
print('begin validation ...', file=sys.stderr)
model.eval()
# compute dev. ppl and bleu
dev_loss = evaluate_loss(model, dev_data, cross_entropy_loss)
dev_ppl = np.exp(dev_loss)
if args.valid_metric in ['rouge2_f', 'bleu', 'word_acc', 'sent_acc']:
dev_hyps = decode(model, dev_data)
dev_hyps = [hyps[0] for hyps in dev_hyps]
if args.valid_metric == 'bleu':
valid_metric = get_bleu([tgt for src, tgt in dev_data], dev_hyps)
elif args.valid_metric == 'rouge2_f':
valid_metric = get_rouge2f([tgt for src, tgt in dev_data], dev_hyps)
else:
valid_metric = get_acc([tgt for src, tgt in dev_data], dev_hyps, acc_type=args.valid_metric)
print('validation: iter %d, dev. ppl %f, dev. %s %f' % (train_iter, dev_ppl, args.valid_metric, valid_metric),
file=sys.stderr)
else:
valid_metric = -dev_ppl
print('validation: iter %d, dev. ppl %f' % (train_iter, dev_ppl),
file=sys.stderr)
model.train()
is_better = len(hist_valid_scores) == 0 or valid_metric > max(hist_valid_scores)
is_better_than_last = len(hist_valid_scores) == 0 or valid_metric > hist_valid_scores[-1]
hist_valid_scores.append(valid_metric)
if valid_num > args.save_model_after:
model_file = args.save_to + '.iter%d.bin' % train_iter
print('save model to [%s]' % model_file, file=sys.stderr)
model.save(model_file)
if is_better:
patience = 0
drop_patience = 0
best_model_iter = train_iter
if valid_num > args.save_model_after:
print('save currently the best model ..', file=sys.stderr)
model_file_abs_path = os.path.abspath(model_file)
symlin_file_abs_path = os.path.abspath(args.save_to + '.bin')
os.system('ln -sf %s %s' % (model_file_abs_path, symlin_file_abs_path))
else:
drop_patience += 1
print('hit drop patience %d' % drop_patience, file=sys.stderr)
if drop_patience == args.drop_patience and args.lr_decay:
drop_patience = 0
lr = optimizer.param_groups[0]['lr'] * args.lr_decay
vocab, model, optimizer, nll_loss, cross_entropy_loss, loss_ent = cont_training_halve_ent(args, lr)
print('decay learning rate to %.12f' % lr, file=sys.stderr)
patience += 1
print('hit patience %d' % patience, file=sys.stderr)
if patience == args.patience:
print('early stop!', file=sys.stderr)
print('the best model is from iteration [%d]' % best_model_iter, file=sys.stderr)
exit(0)
for train_ent_iter in range((train_multi_task_iter - 1) * args.switch, train_multi_task_iter * args.switch):
try:
ent_xs, ent_ys, ent_ls = next(ent_data_generator)
except:
ent_data_generator = data_iter(train_data_ent, batch_size=args.batch_size)
ent_xs, ent_ys, ent_ls = next(ent_data_generator)
ent_xs_var = to_input_variable(ent_xs, vocab.src, cuda=args.cuda)
ent_ys_var = to_input_variable(ent_ys, vocab.src, cuda=args.cuda)
ent_ls_var = Variable(torch.LongTensor(ent_ls), volatile=False, requires_grad=False)
ent_ls_var = torch.squeeze(ent_ls_var)
if args.cuda:
ent_ls_var = ent_ls_var.cuda()
batch_size = len(ent_xs)
ent_xs_len = [len(s) for s in ent_xs]
ent_ys_len = [len(s) for s in ent_ys]
optimizer.zero_grad()
# (tgt_sent_len, batch_size, tgt_vocab_size) -> (batch_size)
scores = model('entailment', ent_xs_var, ent_xs_len, ent_ys_var, ent_ys_len)
loss = loss_ent(scores, ent_ls_var) / batch_size
loss.backward()
# clip gradient
grad_norm = torch.nn.utils.clip_grad_norm(model.parameters(), args.clip_grad)
optimizer.step()
if train_ent_iter > 0 and ((train_ent_iter + 1 )* 100 / args.switch) % args.log_every == 0:
print('iter %d entailment classifier training complete .' % (train_ent_iter + 1))
def read_raml_train_data(data_file, temp):
train_data = dict()
num_pattern = re.compile('^(\d+) samples$')
with open(data_file) as f:
while True:
line = f.readline()
if line is None or line == '':
break
assert line.startswith('***')
src_sent = f.readline()[len('source: '):].strip()
tgt_num = int(num_pattern.match(f.readline().strip()).group(1))
tgt_samples = []
tgt_scores = []
for i in xrange(tgt_num):
d = f.readline().strip().split(' ||| ')
if len(d) < 2:
continue
tgt_sent = d[0].strip()
bleu_score = float(d[1])
tgt_samples.append(tgt_sent)
tgt_scores.append(bleu_score / temp)
tgt_scores = np.exp(tgt_scores)
tgt_scores = tgt_scores / np.sum(tgt_scores)
tgt_entry = zip(tgt_samples, tgt_scores)
train_data[src_sent] = tgt_entry
line = f.readline()
return train_data
def train_raml(args):
tau = args.temp
train_data_src = read_corpus(args.train_src, source='src')
train_data_tgt = read_corpus(args.train_tgt, source='tgt')
train_data = zip(train_data_src, train_data_tgt)
dev_data_src = read_corpus(args.dev_src, source='src')
dev_data_tgt = read_corpus(args.dev_tgt, source='tgt')
dev_data = zip(dev_data_src, dev_data_tgt)
assert args.load_model, 'You have to specify a pre-trained model'
vocab, model, optimizer, nll_loss, cross_entropy_loss = cont_training_raml(args)
if args.raml_sample_mode == 'pre_sample':
# dict of (src, [tgt: (sent, prob)])
print('read in raml training data...', file=sys.stderr, end='')
begin_time = time.time()
raml_samples = read_raml_train_data(args.raml_sample_file, temp=tau)
print('done[%d s].' % (time.time() - begin_time))
elif args.raml_sample_mode.startswith('hamming_distance'):
print('sample from hamming distance payoff distribution')
payoff_prob, Z_qs = generate_hamming_distance_payoff_distribution(max(len(sent) for sent in train_data_tgt),
vocab_size=len(vocab.tgt) - 3,
tau=tau)
train_iter = patience = drop_patience = cum_loss = report_loss = cum_tgt_words = report_tgt_words = 0
report_weighted_loss = cum_weighted_loss = 0
cum_examples = cum_batches = report_examples = epoch = valid_num = best_model_iter = 0
hist_valid_scores = []
train_time = begin_time = time.time()
print('begin RAML training')
sm_func = None
if args.smooth_bleu:
sm_func = SmoothingFunction().method3
if args.reward_type == 'entailment':
word_dic = {}
for line in open("/path/to/nli/word/dict").readlines():
v, k = line.strip().split()
word_dic[v] = int(k)
input_encoder = encoder(137665, 300, 300, 0.01).cuda()
inter_atten = atten(300, 3, 0.01).cuda()
input_encoder.load_state_dict(torch.load('/path/to/input-encoder.pt', map_location=lambda storage, loc: storage))
inter_atten.load_state_dict(torch.load('/path/to/inter-atten.pt', map_location=lambda storage, loc: storage))
input_encoder.eval()
inter_atten.eval()
while True:
epoch += 1
for src_sents, tgt_sents in data_iter(train_data, batch_size=args.batch_size):
train_iter += 1
raml_src_sents = []
raml_tgt_sents = []
raml_tgt_weights = []
if args.raml_sample_mode == 'pre_sample':
for src_sent in src_sents:
tgt_samples_all = raml_samples[' '.join(src_sent)]
if args.sample_size >= len(tgt_samples_all):
tgt_samples = tgt_samples_all
else:
tgt_samples_id = np.random.choice(range(1, len(tgt_samples_all)), size=args.sample_size - 1, replace=False)
tgt_samples = [tgt_samples_all[0]] + [tgt_samples_all[i] for i in tgt_samples_id] # make sure the ground truth y* is in the samples
raml_src_sents.extend([src_sent] * len(tgt_samples))
raml_tgt_sents.extend([['<s>'] + sent.split(' ') + ['</s>'] for sent, weight in tgt_samples])
raml_tgt_weights.extend([weight for sent, weight in tgt_samples])
elif args.raml_sample_mode in ['hamming_distance', 'hamming_distance_impt_sample']:
for src_sent, tgt_sent in zip(src_sents, tgt_sents):
tgt_samples = [] # make sure the ground truth y* is in the samples
tgt_sent_len = len(tgt_sent) - 3 # remove <s> and </s> and ending period .
tgt_ref_tokens = tgt_sent[1:-1]
bleu_scores = []
# sample an edit distances