-
Notifications
You must be signed in to change notification settings - Fork 2
/
module.py
1286 lines (1057 loc) · 50.8 KB
/
module.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
import random
import math
import time
from transformers.modeling_bert import BertPreTrainedModel, BertModel
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from pyemd import emd_samples
from sklearn.metrics import f1_score
from utils import *
import torch
from .modeling_bart import BartEncoder, BartDecoder, BartModel
from transformers import BartTokenizer
from fastNLP import seq_len_to_mask
from fastNLP.modules import Seq2SeqEncoder, Seq2SeqDecoder, State
import torch.nn.functional as F
from fastNLP.models import Seq2SeqModel
from torch import nn
import math
class Criterion(nn.Module):
def __init__(self, model, reward_type, loss_weight,aspect_num=5,
supervised=True, rl_lambda=1.0, rl_alpha=0.5,
pretrain_epochs=0, total_epochs=-1, anneal_type='none',
LM=None, training_set_label_samples=None):
super(Criterion, self).__init__()
self.model = model
self.reward_type = reward_type
self.supervised = supervised
self.rl_lambda = rl_lambda
self.rl_alpha = rl_alpha
self.pretrain_epochs = pretrain_epochs
self.epoch = 0
self.total_epochs = total_epochs
self.anneal_type = anneal_type
if anneal_type == 'linear' and (total_epochs is None):
raise ValueError("Please set total_epochs if you want to " \
"use anneal_type='linear'")
if anneal_type == 'switch' and pretrain_epochs == 0:
raise ValueError("Please set pretrain_epochs > 0 if you want to " \
"use anneal_type='switch'")
self.LM = LM
self.aspect_num = aspect_num
self.BCE = nn.BCEWithLogitsLoss(reduction='none')
self.CE = nn.CrossEntropyLoss(weight=loss_weight, reduction='none')
if 'em' in reward_type:
samples = sum(training_set_label_samples, [])
np.random.shuffle(samples)
n = 10
size = len(samples) // n
self.samples = [
samples[i*size:(i+1)*size]
for i in range(n)
]
def set_scorer(self, scorer):
self.scorer = scorer
def epoch_end(self):
self.epoch += 1
if self.anneal_type != 'none' and self.epoch == self.pretrain_epochs:
print_time_info("loss scheduling started ({})".format(self.anneal_type))
def earth_mover(self, decisions):
# decisions.size() == (batch_size, sample_size, attr_vocab_size)
length = decisions.size(-1)
indexes = (decisions.float().numpy() >= 0.5)
emd = [
[
emd_samples(
np.arange(length)[index].tolist(),
self.samples[0]
) if index.sum() > 0 else 1.0
for index in indexes[bid]
]
for bid in range(decisions.size(0))
]
return torch.tensor(emd, dtype=torch.float, device=decisions.device)
def get_scheduled_loss(self, sup_loss, rl_loss):
if self.epoch < self.pretrain_epochs:
return sup_loss, 0
elif self.anneal_type == 'none':
return sup_loss, rl_loss
elif self.anneal_type == 'switch':
return 0, rl_loss
assert self.anneal_type == 'linear'
rl_weight = (self.epoch - self.pretrain_epochs + 1) / (self.total_epochs - self.pretrain_epochs + 1)
return (1-rl_weight) * sup_loss, rl_weight * rl_loss
def get_scores(self, name, logits):
size = logits.size(0)
ret = torch.tensor(getattr(self.scorer, name)[-size:]).float()
if len(ret.size()) == 2:
ret = ret.mean(dim=-1)
return ret
def get_log_joint_prob_nlg(self, logits, decisions):
"""
args:
logits: tensor of shape [batch_size, beam_size, seq_length, vocab_size]
decisions: tensor of shape [batch_size, beam_size, seq_length, vocab_size]
one-hot vector of decoded word-ids
returns:
log_joint_prob: tensor of shape [batch_size, beam_size]
"""
logits = logits.contiguous().view(*decisions.size())
probs = torch.softmax(logits, dim=-1)
return (decisions * probs).sum(dim=-1).log().sum(dim=-1)
def get_log_joint_prob_nlu(self, logits, decisions):
"""
args:
logits: tensor of shape [batch_size, attr_vocab_size]
or [batch_size, sample_size, attr_vocab_size]
decisions: tensor of shape [batch_size, sample_size, attr_vocab_size]
decisions(0/1)
returns:
log_joint_prob: tensor of shape [batch_size, sample_size]
"""
if len(logits.size()) == len(decisions.size()) - 1:
logits = logits.unsqueeze(1).expand(-1, decisions.size(1), -1)
probs = torch.sigmoid(logits)
decisions = decisions.float()
probs = probs * decisions + (1-probs) * (1-decisions)
return probs.log().sum(dim=-1)
def lm_log_prob(self, decisions):
# decisions.size() == (batch_size, beam_size, seq_length, vocab_size)
log_probs = [
self.LM.get_log_prob(decisions[:, i])
for i in range(decisions.size(1))
]
return torch.stack(log_probs, dim=0).transpose(0, 1)
def sentiment_log_prob(self, decisions):
log_probs = [
1 / self.aspect_num
for i in range(decisions.size(1))
]
return torch.stack(log_probs, dim=0).transpose(0, 1)
def nlg_loss(self, logits, targets):
bs = targets.size(0)
loss = [
self.CE(logits[:, i].contiguous().view(-1, logits.size(-1)), targets.view(-1)).view(bs, -1).mean(-1)
for i in range(logits.size(1))
]
return torch.stack(loss, dim=0).transpose(0, 1)
def nlg_score(self, decisions, targets, func):
scores = [
func(targets, np.argmax(decisions.detach().cpu().numpy()[:, i], axis=-1))
for i in range(decisions.size(1))
]
scores = torch.tensor(scores, dtype=torch.float, device=decisions.device).transpose(0, 1)
if len(scores.size()) == 3:
scores = scores.mean(-1)
return scores
def nlu_loss(self, logits, targets):
loss = [
self.BCE(logits[:, i], targets).mean(-1)
for i in range(logits.size(1))
]
return torch.stack(loss, dim=0).transpose(0, 1)
def nlu_score(self, decisions, targets, average):
device = decisions.device
decisions = decisions.detach().cpu().long().numpy()
targets = targets.detach().cpu().long().numpy()
scores = [
[
f1_score(y_true=np.array([label]), y_pred=np.array([pred]), average=average)
for label, pred in zip(targets, decisions[:, i])
]
for i in range(decisions.shape[1])
]
return torch.tensor(scores, dtype=torch.float, device=device).transpose(0, 1)
def get_reward(self, logits, targets, decisions=None):
reward = 0
if decisions is not None:
decisions = decisions.detach()
if self.model == "nlu":
if self.reward_type == "loss":
reward = self.nlu_loss(logits, targets)
elif self.reward_type == "micro-f1":
reward = -self.nlu_score(decisions, targets, 'micro')
elif self.reward_type == "weighted-f1":
reward = -self.nlu_score(decisions, targets, 'weighted')
elif self.reward_type == "f1":
reward = -(self.nlu_score(decisions, targets, 'micro') + self.nlu_score(decisions, targets, 'weighted'))
elif self.reward_type == "em":
reward = self.earth_mover(decisions)
elif self.reward_type == "made":
reward = -self.sentiment_log_prob(decisions)
elif self.reward_type == "loss-em":
reward = self.nlu_loss(logits, targets) + self.earth_mover(decisions)
elif self.model == "nlg":
if self.reward_type == "loss":
reward = self.nlg_loss(logits, targets)
elif self.reward_type == "lm":
reward = -self.lm_log_prob(decisions)
elif self.reward_type == "bleu":
reward = -self.nlg_score(decisions, targets, func=single_BLEU)
elif self.reward_type == "rouge":
reward = -self.nlg_score(decisions, targets, func=single_ROUGE)
elif self.reward_type == "bleu-rouge":
reward = -(self.nlg_score(decisions, targets, func=single_BLEU) + self.nlg_score(decisions, targets, func=single_ROUGE))
elif self.reward_type == "loss-lm":
reward = self.nlg_loss(logits, targets) - self.lm_log_prob(decisions)
return reward
def forward(self, logits, targets, decisions=None, n_supervise=1,
log_joint_prob=None, supervised=True, last_reward=0.0, calculate_reward=True):
"""
args:
logits: tensor of shape [batch_size, sample_size, * ]
targets: tensor of shape [batch_size, *]
decisions: tensor of shape [batch_size, sample_size, *]
"""
if not self.supervised:
supervised = False
logits = logits.contiguous()
targets = targets.contiguous()
sup_loss = rl_loss = 0
reward = 0.0
if self.epoch >= self.pretrain_epochs and calculate_reward:
reward = self.rl_lambda * self.get_reward(logits, targets, decisions)
if isinstance(last_reward, torch.Tensor):
reward = self.rl_alpha * last_reward + (1 - self.rl_alpha) * reward
if self.model == "FGSC":
if supervised:
splits = logits.split(split_size=1, dim=1)
for i in range(n_supervise):
sup_loss += self.BCE(splits[i].squeeze(1), targets).mean()
X = self.get_log_joint_prob_nlu(logits, decisions) if log_joint_prob is None else log_joint_prob
elif self.model == "FGSG":
if supervised:
splits = logits.split(split_size=1, dim=1)
for i in range(n_supervise):
sup_loss += self.CE(splits[i].contiguous().view(-1, logits.size(-1)), targets.view(-1)).mean()
X = self.get_log_joint_prob_nlg(logits, decisions) if log_joint_prob is None else log_joint_prob
if isinstance(reward, torch.Tensor):
rl_loss = (reward * X).mean()
sup_loss, rl_loss = self.get_scheduled_loss(sup_loss, rl_loss)
return sup_loss, rl_loss, X, reward
class RNNModel(nn.Module):
def __init__(self,
dim_embedding,
dim_hidden,
attr_vocab_size,
vocab_size,
n_layers=1,
bidirectional=False):
super(RNNModel, self).__init__()
if attr_vocab_size and attr_vocab_size > dim_hidden:
raise ValueError(
"attr_vocab_size ({}) should be no larger than "
"dim_hidden ({})".format(attr_vocab_size, dim_hidden)
)
self.dim_embedding = dim_embedding
self.dim_hidden = dim_hidden
self.attr_vocab_size = attr_vocab_size
self.vocab_size = vocab_size
self.n_layers = n_layers
self.bidirectional = bidirectional
self.n_directions = 2 if bidirectional else 1
self.embedding = nn.Embedding(vocab_size, dim_embedding)
self.rnn = nn.GRU(dim_embedding,
dim_hidden,
num_layers=n_layers,
batch_first=True,
bidirectional=bidirectional)
def forward(self, *args, **kwargs):
raise NotImplementedError()
def _init_hidden(self, inputs):
"""
args:
inputs: shape [batch_size, *]
a input tensor with correct device
returns:
hidden: shpae [n_layers*n_directions, batch_size, dim_hidden]
all-zero hidden state
"""
batch_size = inputs.size(0)
return torch.zeros(self.n_layers*self.n_directions,
batch_size,
self.dim_hidden,
dtype=torch.float,
device=inputs.device)
def _init_hidden_with_attrs(self, attrs):
"""
args:
attrs: shape [batch_size, attr_vocab_size], a n-hot vector
returns:
hidden: shape [n_layers*n_directions, batch_size, dim_hidden]
"""
batch_size = attrs.size(0)
hidden = torch.cat(
[
attrs,
torch.zeros(batch_size,
self.dim_hidden - self.attr_vocab_size,
dtype=attrs.dtype,
device=attrs.device)
], 1)
'''
# ignore _UNK and _PAD
hidden[:, 0:2] = 0
'''
return hidden.unsqueeze(0).expand(self.n_layers*self.n_directions, -1, -1).float()
class NLGRNN(RNNModel):
def __init__(self, *args, **kwargs):
super(NLGRNN, self).__init__(*args, **kwargs)
if self.n_directions != 1:
raise ValueError("RNN must be uni-directional in NLG model.")
self.transform = nn.Linear(self.attr_vocab_size, self.dim_hidden)
self.linear = nn.Linear(self.dim_hidden, self.vocab_size)
def _st_softmax(self, logits, hard=False, dim=-1):
y_soft = logits.softmax(dim)
if hard:
# Straight through.
index = y_soft.max(dim, keepdim=True)[1]
y_hard = torch.zeros_like(logits).scatter_(dim, index, 1.0)
ret = y_hard - y_soft.detach() + y_soft
else:
ret = y_soft
return ret
def _st_onehot(self, logits, indices, hard=True, dim=-1):
y_soft = logits.softmax(dim)
if isinstance(indices, np.ndarray):
indices = torch.from_numpy(indices).long().to(logits.device)
if len(logits.size()) == len(indices.size()) + 1:
indices = indices.unsqueeze(-1)
y_hard = torch.zeros_like(logits).scatter_(dim, indices, 1.0)
if hard:
return y_hard - y_soft.detach() + y_soft, y_hard
else:
return y_soft, y_hard
def forward(self, attrs, bos_id, labels=None,
tf_ratio=0.5, max_decode_length=50, beam_size=5, st=True):
"""
args:
attrs: shape [batch_size, attr_vocab_size]
bos_id: integer
labels: shape [batch_size, seq_length]
outputs:
logits: shape [batch_size, beam_size, seq_length, vocab_size]
outputs: shape [batch_size, beam_size, seq_length, vocab_size]
output words as one-hot vectors (maybe soft)
decisions: shape [batch_size, beam_size, seq_length, vocab_size]
output words as one-hot vectors (hard)
"""
if beam_size == 1:
logits, outputs = self.forward_greedy(
attrs, bos_id, labels,
tf_ratio=tf_ratio, max_decode_length=max_decode_length,
st=st
)
return logits.unsqueeze(1), outputs.unsqueeze(1), outputs.unsqueeze(1)
decode_length = max_decode_length if labels is None else labels.size(1)
batch_size = attrs.size(0)
# hidden.size() should be (n_layers*n_directions, beam_size*batch_size, dim_hidden)
hiddens = self.transform(attrs.float()).unsqueeze(0).unsqueeze(0)
hiddens = hiddens.expand(self.n_layers*self.n_directions, beam_size, -1, -1)
hiddens = hiddens.contiguous().view(-1, beam_size*batch_size, self.dim_hidden)
last_output = torch.full_like(attrs[:, 0], bos_id, dtype=torch.long)
# last_output.size() == (beam_size, batch_size)
last_output = [last_output for _ in range(beam_size)]
# logits.shape will be [seq_length, beam_size, batch_size, vocab_size]
logits = []
beam_probs = np.full((beam_size, batch_size), -math.inf)
beam_probs[0, :] = 0.0
# last_indices.shape will be [seq_length, batch_size, beam_size]
last_indices = []
output_ids = []
for step in range(decode_length):
curr_inputs = []
for beam in range(beam_size):
use_tf = False if step == 0 else random.random() < tf_ratio
if use_tf:
curr_input = labels[:, step-1]
else:
curr_input = last_output[beam].detach()
if len(curr_input.size()) == 1:
# curr_input are ids
curr_input = self.embedding(curr_input).unsqueeze(1)
else:
# curr_input are one-hot vectors
curr_input = torch.matmul(curr_input.float(), self.embedding.weight).unsqueeze(1)
curr_inputs.append(curr_input)
curr_inputs = torch.stack(curr_inputs, dim=0)
# curr_inputs.size() == (beam_size, batch_size, 1, dim_embedding)
curr_inputs = curr_inputs.view(-1, 1, self.dim_embedding)
output, new_hiddens = self.rnn(curr_inputs, hiddens)
output = self.linear(output.squeeze(1))
output = output.view(beam_size, batch_size, -1)
new_hiddens = new_hiddens.view(self.n_layers*self.n_directions, beam_size, batch_size, -1)
probs = torch.log_softmax(output.detach(), dim=-1)
# top_probs.size() == top_indices.size() == (beam_size, batch_size, k)
top_probs, top_indices = torch.topk(probs, k=beam_size, dim=-1)
top_probs = top_probs.detach().cpu().numpy()
top_indices = top_indices.detach().cpu().numpy()
last_index = []
output_id = []
for bid in range(batch_size):
beam_prob = []
for beam in range(beam_size):
beam_prob.extend([
(
beam,
top_indices[beam, bid, i],
beam_probs[beam][bid] + top_probs[beam, bid, i]
)
for i in range(beam_size)
])
topk = sorted(beam_prob, key=lambda x: x[2], reverse=True)[:beam_size]
last_index.append([item[0] for item in topk])
output_id.append([item[1] for item in topk])
beam_probs[:, bid] = np.array([item[2] for item in topk])
last_indices.append(last_index)
output_ids.append(output_id)
new_hiddens = new_hiddens.permute([2, 0, 1, 3]).split(split_size=1, dim=0)
hiddens = torch.stack([
new_hiddens[bid].squeeze(0).index_select(dim=1, index=torch.tensor(indices).to(new_hiddens[bid].device))
for bid, indices in enumerate(last_index)
], dim=0).permute([1, 2, 0, 3]).contiguous().view(-1, beam_size*batch_size, self.dim_hidden)
output = output.transpose(0, 1).split(split_size=1, dim=0)
output = [
output[bid].squeeze(0).index_select(dim=0, index=torch.tensor(indices).to(output[bid].device))
for bid, indices in enumerate(last_index)
]
logits.append(output)
last_output = [
torch.tensor(
[output_id[bid][beam] for bid in range(batch_size)],
dtype=torch.long, device=attrs.device
)
for beam in range(beam_size)
]
last_indices = np.array(last_indices)
output_ids = np.array(output_ids)
# back-trace the beams to get outputs
beam_outputs = []
beam_logits = []
beam_decisions = []
for bid in range(batch_size):
this_index = np.arange(beam_size)
step_logits = []
step_output_ids = []
for step in range(decode_length-1, -1, -1):
this_logits = logits[step][bid].index_select(dim=0, index=torch.from_numpy(this_index).to(logits[step][bid].device))
step_logits.append(this_logits)
step_output_ids.append(output_ids[step, bid, this_index])
this_index = last_indices[step, bid, this_index]
step_logits = torch.stack(step_logits[::-1], dim=0)
step_outputs, step_decisions = self._st_onehot(step_logits, np.array(step_output_ids[::-1]), hard=st)
beam_outputs.append(step_outputs)
beam_logits.append(step_logits)
beam_decisions.append(step_decisions)
logits = torch.stack(beam_logits).transpose(1, 2)
outputs = torch.stack(beam_outputs).transpose(1, 2)
decisions = torch.stack(beam_decisions).transpose(1, 2)
return logits, outputs, decisions
def forward_greedy(self, attrs, bos_id, labels=None, sampling=False,
tf_ratio=0.5, max_decode_length=50, st=True):
"""
args:
attrs: shape [batch_size, attr_vocab_size]
bos_id: integer
labels: shape [batch_size, seq_length]
outputs:
logits: shape [batch_size, seq_length, vocab_size]
outputs: shape [batch_size, seq_length, vocab_size]
output words as one-hot vectors
"""
decode_length = max_decode_length if labels is None else labels.size(1)
hidden = self.transform(attrs.float()).unsqueeze(0)
hidden = hidden.expand(self.n_layers*self.n_directions, -1, -1).contiguous()
last_output = torch.full_like(attrs[:, 0], bos_id, dtype=torch.long)
logits = []
outputs = []
for step in range(decode_length):
use_tf = False if step == 0 else random.random() < tf_ratio
if use_tf:
curr_input = labels[:, step-1]
else:
curr_input = last_output.detach()
if len(curr_input.size()) == 1:
curr_input = self.embedding(curr_input).unsqueeze(1)
else:
curr_input = torch.matmul(curr_input.float(), self.embedding.weight).unsqueeze(1)
output, hidden = self.rnn(curr_input, hidden)
output = self.linear(output.squeeze(1))
logits.append(output)
if sampling:
last_output = F.gumbel_softmax(output, hard=True)
else:
last_output = self._st_softmax(output, hard=True, dim=-1)
outputs.append(self._st_softmax(output, hard=st, dim=-1))
logits = torch.stack(logits).transpose(0, 1)
outputs = torch.stack(outputs).transpose(0, 1)
return logits, outputs
class NLURNN(RNNModel):
def __init__(self, *args, **kwargs):
super(NLURNN, self).__init__(*args, **kwargs)
self.linear = nn.Linear(
self.n_layers * self.n_directions * self.dim_hidden,
self.attr_vocab_size
)
def forward(self, inputs, sample_size=1):
"""
args:
inputs: shape [batch_size, seq_length]
or shape [batch_size, seq_length, attr_vocab_size] one-hot vectors
outputs:
logits: shape [batch_size, attr_vocab_size]
"""
batch_size = inputs.size(0)
if len(inputs.size()) == 2:
inputs = self.embedding(inputs)
else:
# suppose the inputs are one-hot vectors
inputs = torch.matmul(inputs.float(), self.embedding.weight)
_, hidden = self.rnn(inputs)
hidden = hidden.transpose(0, 1).contiguous().view(batch_size, -1)
logits = self.linear(hidden)
return logits
class LMRNN(RNNModel):
def __init__(self, *args, **kwargs):
super(LMRNN, self).__init__(*args, **kwargs)
if self.n_directions != 1:
raise ValueError("RNN must be uni-directional in LM model.")
self.linear = nn.Linear(self.dim_hidden, self.vocab_size)
def forward(self, inputs):
inputs = self.embedding(inputs)
output, _ = self.rnn(inputs)
logits = self.linear(output)
return logits
class MaskedLinear(nn.Linear):
""" same as Linear except has a configurable mask on the weights """
def __init__(self, in_features, out_features, bias=True):
super().__init__(in_features, out_features, bias)
self.register_buffer('mask', torch.ones(out_features, in_features))
def set_mask(self, mask):
self.mask.data.copy_(torch.from_numpy(mask.astype(np.uint8).T))
def forward(self, input):
return F.linear(input, self.mask * self.weight, self.bias)
class ScaledDotProductAttention(nn.Module):
def __init__(self, d_k):
super(ScaledDotProductAttention, self).__init__()
self.d_k = d_k
def forward(self, q, k, v, attn_mask):
attn_score = torch.matmul(q, k.transpose(-1, -2)) / np.sqrt(self.d_k)
attn_score.masked_fill_(attn_mask, -1e9)
attn_weights = nn.Softmax(dim=-1)(attn_score)
output = torch.matmul(attn_weights, v)
return output, attn_weights
class MultiHeadAttention(nn.Module):
def __init__(self, d_model, n_heads):
super(MultiHeadAttention, self).__init__()
self.n_heads = n_heads
self.d_k = self.d_v = d_model // n_heads
self.WQ = nn.Linear(d_model, d_model)
self.WK = nn.Linear(d_model, d_model)
self.WV = nn.Linear(d_model, d_model)
self.scaled_dot_product_attn = ScaledDotProductAttention(self.d_k)
self.linear = nn.Linear(n_heads * self.d_v, d_model)
def forward(self, Q, K, V, attn_mask):
batch_size = Q.size(0)
q_heads = self.WQ(Q).view(batch_size, -1, self.n_heads, self.d_k).transpose(1, 2)
k_heads = self.WK(K).view(batch_size, -1, self.n_heads, self.d_k).transpose(1, 2)
v_heads = self.WV(V).view(batch_size, -1, self.n_heads, self.d_v).transpose(1, 2)
attn_mask = attn_mask.unsqueeze(1).repeat(1, self.n_heads, 1, 1)
attn, attn_weights = self.scaled_dot_product_attn(q_heads, k_heads, v_heads, attn_mask)
attn = attn.transpose(1, 2).contiguous().view(batch_size, -1, self.n_heads * self.d_v)
output = self.linear(attn)
return output, attn_weights
class PositionWiseFeedForwardNetwork(nn.Module):
def __init__(self, d_model, d_ff):
super(PositionWiseFeedForwardNetwork, self).__init__()
self.linear1 = nn.Linear(d_model, d_ff)
self.linear2 = nn.Linear(d_ff, d_model)
self.relu = nn.ReLU()
def forward(self, inputs):
output = self.relu(self.linear1(inputs))
output = self.linear2(output)
return output
class EncoderLayer(nn.Module):
def __init__(self, d_model, n_heads, p_drop, d_ff):
super(EncoderLayer, self).__init__()
self.mha = MultiHeadAttention(d_model, n_heads)
self.dropout1 = nn.Dropout(p_drop)
self.layernorm1 = nn.LayerNorm(d_model, eps=1e-6)
self.ffn = PositionWiseFeedForwardNetwork(d_model, d_ff)
self.dropout2 = nn.Dropout(p_drop)
self.layernorm2 = nn.LayerNorm(d_model, eps=1e-6)
def forward(self, inputs, attn_mask):
attn_outputs, attn_weights = self.mha(inputs, inputs, inputs, attn_mask)
attn_outputs = self.dropout1(attn_outputs)
attn_outputs = self.layernorm1(inputs + attn_outputs)
ffn_outputs = self.ffn(attn_outputs)
ffn_outputs = self.dropout2(ffn_outputs)
ffn_outputs = self.layernorm2(attn_outputs + ffn_outputs)
return ffn_outputs, attn_weights
class TransformerEncoder(nn.Module):
def __init__(self, vocab_size, seq_len=300, d_model=768, n_layers=12, n_heads=8, p_drop=0.1, d_ff=500, pad_id=0):
super(TransformerEncoder, self).__init__()
self.pad_id = pad_id
self.sinusoid_table = self.get_sinusoid_table(seq_len + 1, d_model) # (seq_len+1, d_model)
self.embedding = nn.Embedding(vocab_size, d_model)
self.pos_embedding = nn.Embedding.from_pretrained(self.sinusoid_table, freeze=True)
self.layers = nn.ModuleList([EncoderLayer(d_model, n_heads, p_drop, d_ff) for _ in range(n_layers)])
def forward(self, inputs):
positions = torch.arange(inputs.size(1), device=inputs.device, dtype=inputs.dtype).repeat(inputs.size(0), 1) + 1
position_pad_mask = inputs.eq(self.pad_id)
positions.masked_fill_(position_pad_mask, 0)
outputs = self.embedding(inputs) + self.pos_embedding(positions)
attn_pad_mask = self.get_attention_padding_mask(inputs, inputs, self.pad_id)
for layer in self.layers:
outputs, attn_weights = layer(outputs, attn_pad_mask)
return outputs
def get_attention_padding_mask(self, q, k, pad_id):
attn_pad_mask = k.eq(pad_id).unsqueeze(1).repeat(1, q.size(1), 1)
return attn_pad_mask
def get_sinusoid_table(self, seq_len, d_model):
def get_angle(pos, i, d_model):
return pos / np.power(10000, (2 * (i // 2)) / d_model)
sinusoid_table = np.zeros((seq_len, d_model))
for pos in range(seq_len):
for i in range(d_model):
if i % 2 == 0:
sinusoid_table[pos, i] = np.sin(get_angle(pos, i, d_model))
else:
sinusoid_table[pos, i] = np.cos(get_angle(pos, i, d_model))
return torch.FloatTensor(sinusoid_table)
class LMTrm(TransformerEncoder):
def __init__(self, *args, **kwargs):
super(LMTrm, self).__init__(*args, **kwargs)
if self.n_directions != 1:
raise ValueError("RNN must be uni-directional in LM model.")
self.linear = nn.Linear(self.dim_hidden, self.vocab_size)
def forward(self, inputs):
"""
args:
inputs: shape [batch_size, seq_length]
outputs:
logits: shape [batch_size, seq_length, vocab_size]
"""
positions = torch.arange(inputs.size(1), device=inputs.device, dtype=inputs.dtype).repeat(inputs.size(0), 1) + 1
position_pad_mask = inputs.eq(self.pad_id)
positions.masked_fill_(position_pad_mask, 0)
outputs = self.embedding(inputs) + self.pos_embedding(positions)
attn_pad_mask = self.get_attention_padding_mask(inputs, inputs, self.pad_id)
for layer in self.layers:
outputs, attn_weights = layer(outputs, attn_pad_mask)
logits = self.linear(outputs)
return logits
class OrderedCounter(Counter, OrderedDict):
"""Counter that remembers the order elements are first encountered"""
def __repr__(self):
return '%s(%r)' % (self.__class__.__name__, OrderedDict(self))
def __reduce__(self):
return self.__class__, (OrderedDict(self),)
def to_var(x):
if torch.cuda.is_available():
x = x.cuda()
return x
def idx2word(idx, i2w, pad_idx):
sent_str = [str()]*len(idx)
for i, sent in enumerate(idx):
for word_id in sent:
if word_id == pad_idx:
break
sent_str[i] += i2w[str(word_id.item())] + " "
sent_str[i] = sent_str[i].strip()
return sent_str
def interpolate(start, end, steps):
interpolation = np.zeros((start.shape[0], steps + 2))
for dim, (s, e) in enumerate(zip(start, end)):
interpolation[dim] = np.linspace(s, e, steps+2)
return interpolation.T
def expierment_name(args, ts):
exp_name = str()
exp_name += "BS=%i_" % args.batch_size
exp_name += "LR={}_".format(args.learning_rate)
exp_name += "EB=%i_" % args.embedding_size
exp_name += "%s_" % args.rnn_type.upper()
exp_name += "HS=%i_" % args.hidden_size
exp_name += "L=%i_" % args.num_layers
exp_name += "BI=%i_" % args.bidirectional
exp_name += "LS=%i_" % args.latent_size
exp_name += "WD={}_".format(args.word_dropout)
exp_name += "ANN=%s_" % args.anneal_function.upper()
exp_name += "K={}_".format(args.k)
exp_name += "X0=%i_" % args.x0
exp_name += "TS=%s" % ts
return exp_name
def cosine_similarity(x, y, norm=False):
assert len(x) == len(y), "len(x) != len(y)"
zero_list = [0] * len(x)
if x == zero_list or y == zero_list:
return float(1) if x == y else float(0)
res = np.array([[x[i] * y[i], x[i] * x[i], y[i] * y[i]] for i in range(len(x))])
cos = sum(res[:, 0]) / (np.sqrt(sum(res[:, 1])) * np.sqrt(sum(res[:, 2])))
return 0.5 * cos + 0.5 if norm else cos
class Variables(nn.Module):
def __init__(self, hidden_size, output_size, latent_size, latent_num, explicit_size, explicit_num):
super(Variables).__init__()
self.latent_size = latent_size
self.explicit_size = explicit_size
self.latent_num = latent_num
self.explicit_num = explicit_num
self.attention = MultiHeadAttention(hidden_size,1)
self.hidden_size = hidden_size
self.hidden2mean = nn.Linear(hidden_size , latent_size)
self.hidden2logv = nn.Linear(hidden_size , latent_size)
self.latent2hidden = nn.Linear(latent_size, hidden_size )
self.outputs = nn.Linear(hidden_size , output_size)
def forward_cross_referring(self, vars_src, vars_tgt):
sim = cosine_similarity(vars_src, vars_tgt)
return 1/nn.exp(sim)
def forward(self, hidden):
batch_size = hidden.size(0)
hidden = hidden.squeeze()
expvars = []
for lat_k in range(self.latent_num):
expvars.append(self.attention(hidden))
latvars = []
for lat_k in range(self.latent_num):
mean = self.hidden2mean(hidden)
logv = self.hidden2logv(hidden)
std = torch.exp(0.5 * logv)
z = to_var(torch.randn([batch_size, self.latent_size]))
z = z * std + mean
hidden = self.latent2hidden(z)
hidden = hidden.unsqueeze(0)
if self.word_dropout_rate > 0:
prob = torch.rand(hidden.size())
if torch.cuda.is_available():
prob=prob.cuda()
prob[(hidden.data - self.sos_idx) * (hidden.data - self.pad_idx) == 0] = 1
decoder_input_sequence = hidden.clone()
decoder_input_sequence[prob < self.word_dropout_rate] = self.unk_idx
# process outputs
padded_outputs = rnn_utils.pad_packed_sequence(hidden, batch_first=True)[0]
padded_outputs = padded_outputs.contiguous()
_,reversed_idx = torch.sort(sorted_idx)
padded_outputs = padded_outputs[reversed_idx]
b,s,_ = padded_outputs.size()
logp = nn.functional.log_softmax(self.outputs2vocab(padded_outputs.view(-1, padded_outputs.size(2))), dim=-1)
logp = logp.view(b, s, self.embedding.num_embeddings)
lats.append((logp, mean, logv, z))
return expvars, latvars
def var_inference(self, n, z=None):
if z is None:
batch_size = n
z = to_var(torch.randn([batch_size, self.latent_size]))
else:
batch_size = z.size(0)
hidden = self.latent2hidden(z)
if self.bidirectional or self.num_layers > 1:
# unflatten hidden state
hidden = hidden.view(self.hidden_factor, batch_size, self.hidden_size)
hidden = hidden.unsqueeze(0)
sequence_idx = torch.arange(0, batch_size, out=self.tensor()).long() # all idx of batch
sequence_running = torch.arange(0, batch_size, out=self.tensor()).long()
sequence_mask = torch.ones(batch_size, out=self.tensor()).bool()
running_seqs = torch.arange(0, batch_size, out=self.tensor()).long()
generations = self.tensor(batch_size, self.max_sequence_length).fill_(self.pad_idx).long()
t = 0
while t < self.max_sequence_length and len(running_seqs) > 0:
if t == 0:
input_sequence = to_var(torch.Tensor(batch_size).fill_(self.sos_idx).long())
input_sequence = input_sequence.unsqueeze(1)
input_embedding = self.embedding(input_sequence)
output, hidden = self.decoder_rnn(input_embedding, hidden)
logits = self.outputs2vocab(output)
input_sequence = self._sample(logits)
generations = self._save_sample(generations, input_sequence, sequence_running, t)
sequence_mask[sequence_running] = (input_sequence != self.eos_idx)
sequence_running = sequence_idx.masked_select(sequence_mask)
running_mask = (input_sequence != self.eos_idx).data
running_seqs = running_seqs.masked_select(running_mask)
if len(running_seqs) > 0:
input_sequence = input_sequence[running_seqs]
hidden = hidden[:, running_seqs]
running_seqs = torch.arange(0, len(running_seqs), out=self.tensor()).long()
t += 1
return generations, z
def _sample(self, dist, mode='greedy'):
if mode == 'greedy':
_, sample = torch.topk(dist, 1, dim=-1)
sample = sample.reshape(-1)
return sample
def _save_sample(self, save_to, sample, running_seqs, t):
running_latest = save_to[running_seqs]
running_latest[:,t] = sample.data
save_to[running_seqs] = running_latest
return save_to
class SCModel(BertPreTrainedModel):
# BertForMultiLable
def __init__(self, arg):
super(SCModel, self).__init__(arg)
self.bert = BertModel(arg)
self.dropout = nn.Dropout(arg.hidden_dropout_prob)
self.classifier = nn.Linear(arg.hidden_size, arg.num_labels)
self.var_num = arg.num_labels + 1
self.vars = [Variables(arg.hidden_size, arg.output_size, arg.latent_size, 1, arg.explicit_size, arg.num_labels) for _ in range(self.var_num)]
self.init_weights()
def _st_softmax(self, logits, hard=False, dim=-1):
y_soft = logits.softmax(dim)
if hard:
# Straight through.
index = y_soft.max(dim, keepdim=True)[1]
y_hard = torch.zeros_like(logits).scatter_(dim, index, 1.0)
ret = y_hard - y_soft.detach() + y_soft
else:
ret = y_soft
return ret