-
Notifications
You must be signed in to change notification settings - Fork 3
/
inflection.py
1591 lines (1347 loc) · 60.5 KB
/
inflection.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
# -*- coding: utf-8 -*-
import argparse
import codecs
import dynet as dy
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
import myutil
import numpy as np
from operator import itemgetter
import os, sys
from random import random,shuffle
parser = argparse.ArgumentParser()
parser.add_argument("--datapath", help="path to data", type=str, required=True)
parser.add_argument("--L1", help="transfer languages (split with comma for multiple ones)", type=str, required=False)
parser.add_argument("--L2", help="test languages", type=str, required=True)
parser.add_argument("--mode", help="usage mode", type=str,
choices=['train','test','test-dev','draw-dev','test-dev-ensemble','test-ensemble','test-two-ensemble','test-three-ensemble',
'test-all-ensemble'], default='', required=True)
parser.add_argument("--setting", help="data setting", type=str, choices=['original','swap','low',], default='original')
parser.add_argument("--modelpath", help="path to store the models", type=str, default='./models')
parser.add_argument("--figurepath", help="path to store the output attention figures", type=str, default='./figures')
parser.add_argument("--outputpath", help="path to store the inflected outputs on the test set", type=str, default='./outputs')
parser.add_argument("--notest", help="do not use the test set at all", action="store_true")
parser.add_argument("--testset", help="path to different test set", type=str, required=False)
parser.add_argument("--outputfile", help="path to store the inflected outputs", type=str, required=False)
parser.add_argument("--use_hall", help="whether to use a hallucinated dataset (def: False)", action="store_true")
parser.add_argument("--only_hall", help="only use the hallucinated dataset to train (def: False)", action="store_true")
parser.add_argument("--predict_lang", help="use the language discriminator auxiliary task (def: False)", action="store_true")
parser.add_argument("--use_att_reg", help="use attention regularization on the lemma attention (def: False)", action="store_true")
parser.add_argument("--use_tag_att_reg", help="use attention regularization on the tag attention (def: False)", action="store_true")
parser.add_argument("--dynet-mem", help="set dynet memory", default=800, type=int, required=False)
parser.add_argument("--dynet-autobatch", help="use dynet autobatching (def: 1)", default=1, type=int, required=False)
args = parser.parse_args()
if args.L1:
L1 = args.L1
L1s = L1.split(',')
else:
L1 = ''
L1s = []
L2 = args.L2
DATA_PATH = args.datapath
if not os.path.isdir(DATA_PATH):
print("Wrong data path, the provided one does not exist")
sys.exit()
LOW_PATH = os.path.join(DATA_PATH, L2+ "-train")
DEV_PATH = os.path.join(DATA_PATH, L2+ "-dev")
HALL_PATH = os.path.join(DATA_PATH, L2+ "-hall")
TEST_PATH = os.path.join(DATA_PATH, L2+ "-test-covered")
if args.testset:
TEST_PATH = args.testset
if not os.path.isdir(args.modelpath):
os.mkdir(args.modelpath)
if not os.path.isdir(args.figurepath):
os.mkdir(args.figurepath)
if not os.path.isdir(args.outputpath):
os.mkdir(args.outputpath)
if L1:
exp_dir = L1+"-"+L2
else:
exp_dir = L2
MODEL_DIR = os.path.join(args.modelpath, exp_dir)
if not os.path.isdir(MODEL_DIR):
os.mkdir(MODEL_DIR)
FIGURE_DIR = os.path.join(args.figurepath, exp_dir)
if not os.path.isdir(FIGURE_DIR):
os.mkdir(FIGURE_DIR)
OUTPUT_DIR = os.path.join(args.outputpath, exp_dir)
if not os.path.isdir(OUTPUT_DIR):
os.mkdir(OUTPUT_DIR)
TRAIN=False
TEST = False
TEST_ENSEMBLE = False
TEST_TWO_ENSEMBLE = False
TEST_THREE_ENSEMBLE = False
TEST_ALL_ENSEMBLE = False
TEST_DEV = False
DRAW_DEV = False
TEST_DEV_ENSEMBLE = False
if args.mode == "train":
TRAIN = True
elif args.mode == "test":
TEST = True
elif args.mode == "test-dev":
TEST_DEV = True
elif args.mode == "draw-dev":
DRAW_DEV = True
elif args.mode == "test-dev-ensemble":
TEST_DEV_ENSEMBLE = True
elif args.mode == "test-ensemble":
TEST_ENSEMBLE = True
elif args.mode == "test-two-ensemble":
TEST_TWO_ENSEMBLE = True
elif args.mode == "test-three-ensemble":
TEST_THREE_ENSEMBLE = True
elif args.mode == "test-all-ensemble":
TEST_ALL_ENSEMBLE = True
USE_HALL = False
if args.use_hall:
USE_HALL = True
ONLY_HALL = False
if args.only_hall:
ONLY_HALL = True
if args.setting == "original":
ORIGINAL = True
SWAP = False
LOW = False
elif args.setting == "swap":
ORIGINAL = False
SWAP = True
LOW = False
elif args.setting == "low":
ORIGINAL = False
SWAP = False
LOW = True
else:
ORIGINAL = False
SWAP = False
LOW = False
MODEL_NAME = "orig."
if SWAP:
MODEL_NAME = "swap."
elif LOW:
MODEL_NAME = "low."
if USE_HALL:
MODEL_NAME += "hall."
if ONLY_HALL:
MODEL_NAME += "hallonly."
MAX_PREDICTION_LEN_DEF = 20
if L2 == "kabardian":
MAX_PREDICTION_LEN_DEF = 25
elif L2 == "tatar":
MAX_PREDICTION_LEN_DEF = 23
elif L2 == "greek":
MAX_PREDICTION_LEN_DEF = 30
elif L2 == "latin":
MAX_PREDICTION_LEN_DEF = 25
elif L2 == "livonian":
MAX_PREDICTION_LEN_DEF = 22
elif L2 == "bengali":
MAX_PREDICTION_LEN_DEF = 23
elif L2 == "czech":
MAX_PREDICTION_LEN_DEF = 30
elif L2 == "lithuanian":
MAX_PREDICTION_LEN_DEF = 33
elif L2 == "russian":
MAX_PREDICTION_LEN_DEF = 50
elif L2 == "irish":
MAX_PREDICTION_LEN_DEF = 37
elif L2 == "quechua":
MAX_PREDICTION_LEN_DEF = 30
elif L2 == "azeri":
MAX_PREDICTION_LEN_DEF = 22
elif L2 == "yiddish":
MAX_PREDICTION_LEN_DEF = 22
LENGTH_NORM_WEIGHT = 0.1
EXTRA_WEIGHT = 0.3
USE_ATT_REG = False
USE_TAG_ATT_REG = False
PREDICT_LANG = False
if args.predict_lang:
PREDICT_LANG = True
MODEL_NAME += "lang."
if args.use_att_reg:
USE_ATT_REG = True
if args.use_tag_att_reg:
USE_TAG_ATT_REG = True
if USE_HALL:
low_i, low_o, low_t = myutil.read_data(LOW_PATH)
dev_i, dev_o, dev_t = myutil.read_data(DEV_PATH)
if args.notest:
test_i, test_t = dev_i, dev_t
else:
test_i, test_t = myutil.read_test_data(TEST_PATH)
hall_i, hall_o, hall_t = myutil.read_data(HALL_PATH)
low_i += hall_i
low_o += hall_o
low_t += hall_t
lids_1 = [0]*len(low_i)
high_i, high_o, high_t = [], [], []
for j,L1 in enumerate(L1s):
HIGH_PATH = os.path.join(DATA_PATH, L1+ "-train")
ti, to, tt = myutil.read_data(HIGH_PATH)
high_i += ti
high_o += to
high_t += tt
lids_1 += [j+1]*len(ti)
NUM_LANG = len(L1s)+1
elif ONLY_HALL:
high_i, high_o, high_t = [], [], []
low_i, low_o, low_t = myutil.read_data(LOW_PATH)
dev_i, dev_o, dev_t = myutil.read_data(DEV_PATH)
if args.notest:
test_i, test_t = dev_i, dev_t
else:
test_i, test_t = myutil.read_test_data(TEST_PATH)
hall_i, hall_o, hall_t = myutil.read_data(HALL_PATH)
low_i += hall_i
low_o += hall_o
low_t += hall_t
else:
low_i, low_o, low_t = myutil.read_data(LOW_PATH)
dev_i, dev_o, dev_t = myutil.read_data(DEV_PATH)
if args.notest:
test_i, test_t = dev_i, dev_t
else:
test_i, test_t = myutil.read_test_data(TEST_PATH)
high_i, high_o, high_t = [], [], []
lids_1 = [0]*len(low_i)
for j,L1 in enumerate(L1s):
HIGH_PATH = os.path.join(DATA_PATH, L1+ "-train")
ti, to, tt = myutil.read_data(HIGH_PATH)
high_i += ti
high_o += to
high_t += tt
lids_1 += [j+1]*len(ti)
NUM_LANG = len(L1s)+1
if SWAP:
if len(dev_i) < len(low_o):
N = len(dev_i)
tmp1, tmp2, tmp3 = list(low_i[-N:]), list(low_o[-N:]), list(low_t[-N:])
low_i = list(low_i[:-N] + dev_i)
low_o = list(low_o[:-N] + dev_o)
low_t = list(low_t[:-N] + dev_t)
dev_i, dev_o, dev_t = tmp1, tmp2, tmp3
else:
tmp1, tmp2, tmp3 = list(low_i), list(low_o), list(low_t)
low_i, low_o, low_t = list(dev_i), list(dev_o), list(dev_t)
dev_i, dev_o, dev_t = tmp1, tmp2, tmp3
print("Data lengths")
print("transfer-language", len(high_i), len(high_o), len(high_t))
print("test-language", len(low_i), len(low_o), len(low_t))
print("dev", len(dev_i), len(dev_o), len(dev_t))
print("test", len(test_i), len(test_t))
def compute_mixing_weights(l):
if l == 3:
K = float(len(high_i))
N = float(len(low_i))
M = float(len(dev_i))
denom = 2*N+M+2*K
return [(K+N)/denom, (M+K)/denom, N/denom]
elif l == 2:
K = float(len(high_i))
N = float(len(low_i))
M = float(len(dev_i))
denom = N+M+2*K
return [(K+N)/denom, (M+K)/denom]
COPY_THRESHOLD = 0.9
COPY_TASK_PROB = 0.2
STARTING_LEARNING_RATE = 0.1
EPOCHS_TO_HALVE = 6
MULTIPLY = 1
if len(high_i)+len(low_i) < 5000:
MULTIPLY = 1
STARTING_LEARNING_RATE = 0.2
COPY_THRESHOLD = 0.6
COPY_TASK_PROB = 0.4
EPOCHS_TO_HALVE = 12
def get_chars(l):
flat_list = [char for word in l for char in word]
return list(set(flat_list))
def get_tags(l):
flat_list = [tag for sublist in l for tag in sublist]
return list(set(flat_list))
EOS = "<EOS>"
NULL = "<NULL>"
if TRAIN:
#SOS = "<SOS>"
characters = get_chars(high_i+high_o+low_i+low_o+dev_i+dev_o+test_i)
#characters.append(SOS)
characters.append(EOS)
if u' ' not in characters:
characters.append(u' ')
tags = get_tags(high_t+low_t+dev_t+test_t)
tags.append(NULL)
#Store vocabularies for future reference
myutil.write_vocab(characters, os.path.join(MODEL_DIR, MODEL_NAME+"char.vocab"))
myutil.write_vocab(tags, os.path.join(MODEL_DIR, MODEL_NAME+"tag.vocab"))
else:
characters = myutil.read_vocab(os.path.join(MODEL_DIR, MODEL_NAME+"char.vocab"))
if u' ' not in characters:
characters.append(u' ')
tags = myutil.read_vocab(os.path.join(MODEL_DIR, MODEL_NAME+"tag.vocab"))
int2char = list(characters)
char2int = {c:i for i,c in enumerate(characters)}
int2tag = list(tags)
tag2int = {c:i for i,c in enumerate(tags)}
VOCAB_SIZE = len(characters)
TAG_VOCAB_SIZE = len(tags)
LSTM_NUM_OF_LAYERS = 1
EMBEDDINGS_SIZE = 32
STATE_SIZE = 100
ATTENTION_SIZE = 100
MINIBATCH_SIZE = 1
COPY_WEIGHT = 0.8
DROPOUT_PROB = 0.2
print("Characters:",characters)
print("Vocab size:", VOCAB_SIZE)
print("All tags:", tags)
print("Tag vocab size:", TAG_VOCAB_SIZE)
def run_lstm(init_state, input_vecs):
s = init_state
out_vectors = []
for vector in input_vecs:
s = s.add_input(vector)
out_vector = s.output()
out_vectors.append(out_vector)
return out_vectors
class InflectionModel:
def __init__(self):
self.model = dy.Model()
self.enc_fwd_lstm = dy.CoupledLSTMBuilder(LSTM_NUM_OF_LAYERS, EMBEDDINGS_SIZE, STATE_SIZE, self.model)
self.enc_bwd_lstm = dy.CoupledLSTMBuilder(LSTM_NUM_OF_LAYERS, EMBEDDINGS_SIZE, STATE_SIZE, self.model)
self.dec_lstm = dy.CoupledLSTMBuilder(LSTM_NUM_OF_LAYERS, STATE_SIZE*3+EMBEDDINGS_SIZE, STATE_SIZE, self.model)
self.input_lookup = self.model.add_lookup_parameters((VOCAB_SIZE, EMBEDDINGS_SIZE) )
self.tag_input_lookup = self.model.add_lookup_parameters((TAG_VOCAB_SIZE, EMBEDDINGS_SIZE) )
self.attention_w1 = self.model.add_parameters( (ATTENTION_SIZE, STATE_SIZE*2) )
self.attention_w2 = self.model.add_parameters( (ATTENTION_SIZE, STATE_SIZE*LSTM_NUM_OF_LAYERS*2) )
self.attention_w3 = self.model.add_parameters( (ATTENTION_SIZE, 5) )
self.attention_v = self.model.add_parameters( (1, ATTENTION_SIZE))
self.decoder_w = self.model.add_parameters( (VOCAB_SIZE, STATE_SIZE))
self.decoder_b = self.model.add_parameters( (VOCAB_SIZE))
#output_lookup = model.add_lookup_parameters((VOCAB_SIZE, EMBEDDINGS_SIZE))
self.output_lookup = self.input_lookup
self.enc_tag_lstm = dy.CoupledLSTMBuilder(LSTM_NUM_OF_LAYERS, EMBEDDINGS_SIZE, STATE_SIZE, self.model)
self.tag_attention_w1 = self.model.add_parameters( (ATTENTION_SIZE, STATE_SIZE))
self.tag_attention_w2 = self.model.add_parameters( (ATTENTION_SIZE, STATE_SIZE*LSTM_NUM_OF_LAYERS*2))
self.tag_attention_v = self.model.add_parameters( (1, ATTENTION_SIZE))
if PREDICT_LANG:
self.lang_class_w = self.model.add_parameters((STATE_SIZE*2, NUM_LANG))
#self.lang_class_w = self.model.add_parameters((STATE_SIZE*2, 1))
def embed_tags(self, tags):
tags = [tag2int[t] for t in tags]
return [self.tag_input_lookup[tag] for tag in tags]
def embed_sentence(self, sentence):
sentence = [EOS] + list(sentence) + [EOS]
sentence = [char2int[c] for c in sentence]
return [self.input_lookup[char] for char in sentence]
def self_encode_tags(self, tags):
vectors = tags
# Self attention for every tag:
vectors = run_lstm(self.enc_tag_lstm.initial_state(), tags)
tag_input_mat = dy.concatenate_cols(vectors)
out_vectors = []
for v1 in vectors:
# tag input mat: [tag_emb x seqlen]
# v1: [tag_emb]
unnormalized = dy.transpose(dy.transpose(v1) * tag_input_mat)
self_att_weights = dy.softmax(unnormalized)
to_add = tag_input_mat*self_att_weights
out_vectors.append(v1 + tag_input_mat*self_att_weights)
return out_vectors
def encode_tags(self, tags):
vectors = run_lstm(self.enc_tag_lstm.initial_state(), tags)
return vectors
def encode_sentence(self, sentence):
sentence_rev = list(reversed(sentence))
fwd_vectors = run_lstm(self.enc_fwd_lstm.initial_state(), sentence)
bwd_vectors = run_lstm(self.enc_bwd_lstm.initial_state(), sentence_rev)
bwd_vectors = list(reversed(bwd_vectors))
vectors = [dy.concatenate(list(p)) for p in zip(fwd_vectors, bwd_vectors)]
return vectors
def attend_tags(self, state, w1dt):
# input_mat: (encoder_state x seqlen) => input vecs concatenated as cols
# w1dt: (attdim x seqlen)
# w2dt: (attdim x attdim)
w2dt = self.tag_attention_w2*state
# att_weights: (seqlen,) row vector
unnormalized = dy.transpose(self.tag_attention_v * dy.tanh(dy.colwise_add(w1dt, w2dt)))
att_weights = dy.softmax(unnormalized)
# context: (encoder_state)
return att_weights
def attend(self, state, w1dt):
# input_mat: (encoder_state x seqlen) => input vecs concatenated as cols
# w1dt: (attdim x seqlen)
# w2dt: (attdim x attdim)
w2dt = self.attention_w2*state
# att_weights: (seqlen,) row vector
unnormalized = dy.transpose(self.attention_v * dy.tanh(dy.colwise_add(w1dt, w2dt)))
att_weights = dy.softmax(unnormalized)
return att_weights
def attend_with_prev(self, state, w1dt, prev_att):
# input_mat: (encoder_state x seqlen) => input vecs concatenated as cols
# w1dt: (attdim x seqlen)
# w2dt: (attdim x attdim)
w2dt = self.attention_w2 * state
w3dt = self.attention_w3 * prev_att
# att_weights: (seqlen,) row vector
unnormalized = dy.transpose(self.attention_v * dy.tanh(dy.colwise_add(dy.colwise_add(w1dt, w2dt), w3dt)))
att_weights = dy.softmax(unnormalized)
return att_weights
def decode(self, vectors, tag_vectors, output, lang_id, weight, teacher_prob=1.0):
output = [EOS] + list(output) + [EOS]
output = [char2int[c] for c in output]
N = len(vectors)
input_mat = dy.concatenate_cols(vectors)
w1dt = None
input_mat = dy.dropout(input_mat, DROPOUT_PROB)
tag_input_mat = dy.concatenate_cols(tag_vectors)
tag_w1dt = None
last_output_embeddings = self.output_lookup[char2int[EOS]]
s = self.dec_lstm.initial_state().add_input(dy.concatenate([vectors[-1], tag_vectors[-1], last_output_embeddings]))
loss = []
prev_att = dy.zeros(5)
if USE_ATT_REG:
total_att = dy.zeros(N)
if USE_TAG_ATT_REG:
total_tag_att = dy.zeros(len(tag_vectors))
for char in output:
# w1dt can be computed and cached once for the entire decoding phase
w1dt = w1dt or self.attention_w1 * input_mat
tag_w1dt = tag_w1dt or self.tag_attention_w1 * tag_input_mat
state = dy.concatenate(list(s.s()))
tag_att_weights = self.attend_tags(state, tag_w1dt)
tag_context = tag_input_mat * tag_att_weights
tag_context2 = dy.concatenate([tag_context,tag_context])
new_state = state + tag_context2
att_weights = self.attend_with_prev(new_state, w1dt, prev_att)
context = input_mat * att_weights
best_ic = np.argmax(att_weights.vec_value())
context = input_mat * att_weights
startt = min(best_ic-2,N-6)
if startt < 0:
startt = 0
endd = startt+5
if N < 5:
prev_att = dy.concatenate([att_weights] + [dy.zeros(1)]*(5-N) )
else:
prev_att = att_weights[startt:endd]
if prev_att.dim()[0][0] != 5:
print(prev_att.dim())
if USE_ATT_REG:
total_att = total_att + att_weights
if USE_TAG_ATT_REG:
total_tag_att = total_tag_att + tag_att_weights
vector = dy.concatenate([context, tag_context, last_output_embeddings])
s = s.add_input(vector)
s_out = dy.dropout(s.output(), DROPOUT_PROB)
out_vector = self.decoder_w * s_out + self.decoder_b
probs = dy.softmax(out_vector)
if teacher_prob == 1:
last_output_embeddings = self.output_lookup[char]
else:
if random() > teacher_prob:
out_char = np.argmax(probs.npvalue())
last_output_embeddings = self.output_lookup[out_char]
else:
last_output_embeddings = self.output_lookup[char]
loss.append(-dy.log(dy.pick(probs, char)))
loss = dy.esum(loss)*weight
if PREDICT_LANG:
last_enc_state = vectors[-1]
adv_state = dy.flip_gradient(last_enc_state)
pred_lang = dy.transpose(dy.transpose(adv_state)*self.lang_class_w)
lang_probs = dy.softmax(pred_lang)
lang_loss_1 = -dy.log(dy.pick(lang_probs, lang_id))
first_enc_state = vectors[0]
adv_state2 = dy.flip_gradient(first_enc_state)
pred_lang2 = dy.transpose(dy.transpose(adv_state2)*self.lang_class_w)
lang_probs2 = dy.softmax(pred_lang2)
lang_loss_2 = -dy.log(dy.pick(lang_probs2, lang_id))
loss += lang_loss_1 + lang_loss_2
if USE_ATT_REG:
loss += dy.huber_distance(dy.ones(N),total_att)
if USE_TAG_ATT_REG:
loss += dy.huber_distance(dy.ones(len(tag_vectors)), total_tag_att)
return loss
def generate(self, in_seq, tag_seq, show_att=False, show_tag_att=False, fn=None):
dy.renew_cg()
embedded = self.embed_sentence(in_seq)
encoded = self.encode_sentence(embedded)
embedded_tags = self.embed_tags(tag_seq)
#encoded_tags = self.encode_tags(embedded_tags)
encoded_tags = self.self_encode_tags(embedded_tags)
input_mat = dy.concatenate_cols(encoded)
tag_input_mat = dy.concatenate_cols(encoded_tags)
w1dt = None
tag_w1dt = None
prev_att = dy.zeros(5)
tmpinseq = [EOS] + list(in_seq) + [EOS]
N = len(tmpinseq)
last_output_embeddings = self.output_lookup[char2int[EOS]]
s = self.dec_lstm.initial_state().add_input(dy.concatenate([encoded[-1], encoded_tags[-1], last_output_embeddings]))
out = ''
count_EOS = 0
if show_att:
attt_weights = []
if show_tag_att:
ttt_weights = []
for i in range(len(in_seq)*2):
if count_EOS == 2: break
# w1dt can be computed and cached once for the entire decoding phase
w1dt = w1dt or self.attention_w1 * input_mat
tag_w1dt = tag_w1dt or self.tag_attention_w1 * tag_input_mat
state = dy.concatenate(list(s.s()))
tag_att_weights = self.attend_tags(state, tag_w1dt)
tag_context = tag_input_mat * tag_att_weights
tag_context2 = dy.concatenate([tag_context,tag_context])
new_state = state + tag_context2
att_weights = self.attend_with_prev(new_state, w1dt, prev_att)
best_ic = np.argmax(att_weights.vec_value())
context = input_mat * att_weights
startt = min(best_ic-2, N-6)
if startt < 0:
startt = 0
endd = startt+5
if N < 5:
prev_att = dy.concatenate([att_weights] + [dy.zeros(1)]*(5-N) )
else:
prev_att = att_weights[startt:endd]
if show_att:
attt_weights.append(att_weights.npvalue())
if show_tag_att:
ttt_weights.append(tag_att_weights.npvalue())
vector = dy.concatenate([context, tag_context, last_output_embeddings])
s = s.add_input(vector)
out_vector = self.decoder_w * s.output() + self.decoder_b
probs = dy.softmax(out_vector).npvalue()
next_char = np.argmax(probs)
last_output_embeddings = self.output_lookup[next_char]
if int2char[next_char] == EOS:
count_EOS += 1
continue
out += int2char[next_char]
if (show_att) and len(out) and fn is not None:
arr = np.squeeze(np.array(attt_weights))[1:-1,1:-1]
fig, ax = plt.subplots()
ax = plt.imshow(arr)
x_positions = np.arange(0,len(attt_weights[0])-2)
y_positions = np.arange(0,len(out))
plt.xticks(x_positions, list(in_seq))
plt.yticks(y_positions, list(out))
plt.savefig(fn+'-char.png')
plt.clf()
plt.close()
if (show_tag_att) and len(out) and fn is not None:
arr = np.squeeze(np.array(ttt_weights))[1:-1,:]
fig, ax = plt.subplots()
ax = plt.imshow(arr)
x_positions = np.arange(0,len(ttt_weights[0]))
y_positions = np.arange(0,len(out))
plt.xticks(x_positions, list(tag_seq))
plt.yticks(y_positions, list(out))
plt.savefig(fn+'-tag.png')
plt.clf()
plt.close()
return out
def draw_decode(self, in_seq, tag_seq, out_seq, show_att=False, show_tag_att=False, fn=None):
dy.renew_cg()
embedded = self.embed_sentence(in_seq)
encoded = self.encode_sentence(embedded)
N = len(encoded)
embedded_tags = self.embed_tags(tag_seq)
encoded_tags = self.self_encode_tags(embedded_tags)
output = [EOS] + list(out_seq) + [EOS]
output = [char2int[c] for c in output]
input_mat = dy.concatenate_cols(encoded)
w1dt = None
tag_input_mat = dy.concatenate_cols(encoded_tags)
tag_w1dt = None
last_output_embeddings = self.output_lookup[char2int[EOS]]
s = self.dec_lstm.initial_state().add_input(dy.concatenate([encoded[-1], encoded_tags[-1], last_output_embeddings]))
prev_att = dy.zeros(5)
attt_weights = []
ttt_weights = []
for char in output:
# w1dt can be computed and cached once for the entire decoding phase
w1dt = w1dt or self.attention_w1 * input_mat
tag_w1dt = tag_w1dt or self.tag_attention_w1 * tag_input_mat
state = dy.concatenate(list(s.s()))
tag_att_weights = self.attend_tags(state, tag_w1dt)
tag_context = tag_input_mat * tag_att_weights
tag_context2 = dy.concatenate([tag_context,tag_context])
new_state = state + tag_context2
att_weights = self.attend_with_prev(new_state, w1dt, prev_att)
best_ic = np.argmax(att_weights.vec_value())
context = input_mat * att_weights
startt = min(best_ic-2,N-6)
if startt < 0:
startt = 0
endd = startt+5
if N < 5:
prev_att = dy.concatenate([att_weights] + [dy.zeros(1)]*(5-N) )
else:
prev_att = att_weights[startt:endd]
if prev_att.dim()[0][0] != 5:
print(prev_att.dim())
if show_att:
attt_weights.append(att_weights.npvalue())
if show_tag_att:
ttt_weights.append(tag_att_weights.npvalue())
vector = dy.concatenate([context, tag_context, last_output_embeddings])
s = s.add_input(vector)
s_out = dy.dropout(s.output(), DROPOUT_PROB)
out_vector = self.decoder_w * s_out + self.decoder_b
probs = dy.softmax(out_vector)
last_output_embeddings = self.output_lookup[char]
outputchars = [int2char[c] for c in output[1:-1]]
if (show_att) and fn is not None:
arr = np.squeeze(np.array(attt_weights))[1:-1,1:-1]
fig, ax = plt.subplots()
ax = plt.imshow(arr)
x_positions = np.arange(0,len(attt_weights[0])-2)
y_positions = np.arange(0,len(outputchars))
plt.xticks(x_positions, list(in_seq))
plt.yticks(y_positions, list(outputchars))
plt.savefig(fn+'-char.png')
plt.clf()
plt.close()
if (show_tag_att) and fn is not None:
arr = np.squeeze(np.array(ttt_weights))[1:-1,:]
fig, ax = plt.subplots()
ax = plt.imshow(arr)
x_positions = np.arange(0,len(ttt_weights[0]))
y_positions = np.arange(0,len(outputchars))
plt.xticks(x_positions, list(tag_seq))
plt.yticks(y_positions, list(outputchars))
plt.savefig(fn+'-tag.png')
plt.clf()
plt.close()
return
def generate_nbest(self, in_seq, tag_seq, beam_size=4, show_att=False, show_tag_att=False, fn=None):
dy.renew_cg()
try:
embedded = self.embed_sentence(in_seq)
except:
return []
encoded = self.encode_sentence(embedded)
embedded_tags = self.embed_tags(tag_seq)
#encoded_tags = self.encode_tags(embedded_tags)
encoded_tags = self.self_encode_tags(embedded_tags)
input_mat = dy.concatenate_cols(encoded)
tag_input_mat = dy.concatenate_cols(encoded_tags)
prev_att = dy.zeros(5)
tmpinseq = [EOS] + list(in_seq) + [EOS]
N = len(tmpinseq)
last_output_embeddings = self.output_lookup[char2int[EOS]]
init_vector = dy.concatenate([encoded[-1], encoded_tags[-1], last_output_embeddings])
s_0 = self.dec_lstm.initial_state().add_input(init_vector)
w1dt = self.attention_w1 * input_mat
tag_w1dt = self.tag_attention_w1 * tag_input_mat
beam = {0: [(0, s_0.s(), [], prev_att)]}
i = 1
nbest = []
# we'll need this
last_states = {}
MAX_PREDICTION_LEN = max(len(in_seq)*1.5,MAX_PREDICTION_LEN_DEF)
# expand another step if didn't reach max length and there's still beams to expand
while i < MAX_PREDICTION_LEN and len(beam[i - 1]) > 0:
# create all expansions from the previous beam:
next_beam_id = []
for hyp_id, hypothesis in enumerate(beam[i - 1]):
# expand hypothesis tuple
#prefix_seq, prefix_prob, prefix_decoder, prefix_context, prefix_tag_context = hypothesis
prefix_prob, prefix_decoder, prefix_seq, prefix_att = hypothesis
if i > 1:
last_hypo_symbol = prefix_seq[-1]
else:
last_hypo_symbol = EOS
# cant expand finished sequences
if last_hypo_symbol == EOS and i > 3:
continue
# expand from the last symbol of the hypothesis
last_output_embeddings = self.output_lookup[char2int[last_hypo_symbol]]
# Perform the forward step on the decoder
# First, set the decoder's parameters to what they were in the previous step
if (i == 1):
s = self.dec_lstm.initial_state().add_input(init_vector)
else:
s = self.dec_lstm.initial_state(prefix_decoder)
state = dy.concatenate(list(s.s()))
tag_att_weights = self.attend_tags(state, tag_w1dt)
tag_context = tag_input_mat * tag_att_weights
tag_context2 = dy.concatenate([tag_context,tag_context])
new_state = state + tag_context2
att_weights = self.attend_with_prev(new_state, w1dt, prefix_att)
best_ic = np.argmax(att_weights.vec_value())
startt = min(best_ic-2, N-6)
if startt < 0:
startt = 0
endd = startt+5
if N < 5:
prev_att = dy.concatenate([att_weights] + [dy.zeros(1)]*(5-N) )
else:
prev_att = att_weights[startt:endd]
if prev_att.dim()[0][0] != 5:
print(prev_att.dim())
context = input_mat * att_weights
vector = dy.concatenate([context, tag_context, last_output_embeddings])
s_0 = s.add_input(vector)
out_vector = self.decoder_w * s_0.output() + self.decoder_b
probs = dy.softmax(out_vector).npvalue()
# Add length norm
length_norm = np.power(5 + i, LENGTH_NORM_WEIGHT)/(np.power(6,LENGTH_NORM_WEIGHT))
probs = probs/length_norm
last_states[hyp_id] = s_0.s()
# find best candidate outputs
n_best_indices = myutil.argmax(probs, beam_size)
for index in n_best_indices:
this_score = prefix_prob + np.log(probs[index])
next_beam_id.append((this_score, hyp_id, index, prev_att))
next_beam_id.sort(key=itemgetter(0), reverse=True)
next_beam_id = next_beam_id[:beam_size]
# Create the most probable hypotheses
# add the most probable expansions from all hypotheses to the beam
new_hypos = []
for item in next_beam_id:
hypid = item[1]
this_prob = item[0]
char_id = item[2]
next_sentence = beam[i - 1][hypid][2] + [int2char[char_id]]
new_hyp = (this_prob, last_states[hypid], next_sentence, item[3])
new_hypos.append(new_hyp)
if next_sentence[-1] == EOS or i == MAX_PREDICTION_LEN-1:
if ''.join(next_sentence) != "<EOS>" and ''.join(next_sentence) != "<EOS><EOS>" and ''.join(next_sentence) != "<EOS><EOS><EOS>":
nbest.append(new_hyp)
beam[i] = new_hypos
i += 1
if len(nbest) > 0:
nbest.sort(key=itemgetter(0), reverse=True)
nbest = nbest[:beam_size]
if len(nbest) == beam_size and (len(new_hypos) == 0 or (nbest[-1][0] >= new_hypos[0][0])):
break
return nbest
def get_loss(self, input_sentence, input_tags, output_sentence, lang_id, weight=1, tf_prob=1.0):
embedded = self.embed_sentence(input_sentence)
encoded = self.encode_sentence(embedded)
#encoded = dy.dropout(encoded, DROPOUT_PROB)
embedded_tags = self.embed_tags(input_tags)
#encoded_tags = self.encode_tags(enc_tag_lstm, embedded_tags)
encoded_tags = self.self_encode_tags(embedded_tags)
return self.decode(encoded, encoded_tags, output_sentence, lang_id, weight, tf_prob)
def ensemble_generate_nbest(inf_models, ensemble_weights, in_seq, tag_seq, beam_size=4):
dy.renew_cg()
n_models = len(inf_models)
embedded = {}
encoded = {}
embedded_tags = {}
encoded_tags = {}
input_mat = {}
tag_input_mat = {}
prev_att = {}
for i in range(n_models):
embedded[i] = inf_models[i].embed_sentence(in_seq)
encoded[i] = inf_models[i].encode_sentence(embedded[i])
embedded_tags[i] = inf_models[i].embed_tags(tag_seq)
#encoded_tags[i] = inf_models[i].encode_tags(embedded_tags[i])
encoded_tags[i] = inf_models[i].self_encode_tags(embedded_tags[i])
input_mat[i] = dy.concatenate_cols(encoded[i])
tag_input_mat[i] = dy.concatenate_cols(encoded_tags[i])
prev_att[i] = dy.zeros(5)
tmpinseq = [EOS] + list(in_seq) + [EOS]
N = len(tmpinseq)
last_output_embeddings = {}
init_vector = {}
s_0 = {}
w1dt = {}
tag_w1dt = {}
for i in range(n_models):
last_output_embeddings[i] = inf_models[i].output_lookup[char2int[EOS]]
init_vector[i] = dy.concatenate([encoded[i][-1], encoded_tags[i][-1], last_output_embeddings[i]])
s_0[i] = inf_models[i].dec_lstm.initial_state().add_input(init_vector[i])
w1dt[i] = inf_models[i].attention_w1 * input_mat[i]
tag_w1dt[i] = inf_models[i].tag_attention_w1 * tag_input_mat[i]
beam = {0: [(0, [s_0[i].s() for i in range(n_models)] , [], [prev_att[i] for i in range(n_models)] )]}
i = 1
nbest = []
# we'll need this
last_states = {}
MAX_PREDICTION_LEN = max(len(in_seq)*1.5,MAX_PREDICTION_LEN_DEF)
# expand another step if didn't reach max length and there's still beams to expand
while i < MAX_PREDICTION_LEN and len(beam[i - 1]) > 0:
# create all expansions from the previous beam:
next_beam_id = []
for hyp_id, hypothesis in enumerate(beam[i - 1]):
# expand hypothesis tuple
#prefix_seq, prefix_prob, prefix_decoder, prefix_context, prefix_tag_context = hypothesis
prefix_prob, prefix_decoders, prefix_seq, prefix_atts = hypothesis
if i > 1:
last_hypo_symbol = prefix_seq[-1]
else:
last_hypo_symbol = EOS
# cant expand finished sequences
if last_hypo_symbol == EOS and i > 3:
continue
# expand from the last symbol of the hypothesis
last_output_embeddings = {}
for k in range(n_models):
last_output_embeddings[k] = inf_models[k].output_lookup[char2int[last_hypo_symbol]]
# Perform the forward step on the decoder
# First, set the decoder's parameters to what they were in the previous step
s = {}
if (i == 1):
for k in range(n_models):
s[k] = inf_models[k].dec_lstm.initial_state().add_input(init_vector[k])
else:
for k in range(n_models):
s[k] = inf_models[k].dec_lstm.initial_state(prefix_decoders[k])
s_0 = {}
probs = {}
state = {}
tag_att_weights = {}
tag_context = {}
tag_context2 = {}
new_state = {}
att_weights = {}
prev_att2 = {}
context = {}
vector = {}
out_vector = {}
for k in range(n_models):
state[k] = dy.concatenate(list(s[k].s()))
tag_att_weights[k] = inf_models[k].attend_tags(state[k], tag_w1dt[k])
tag_context[k] = tag_input_mat[k] * tag_att_weights[k]
tag_context2[k] = dy.concatenate([tag_context[k],tag_context[k]])
new_state[k] = state[k] + tag_context2[k]
att_weights[k] = inf_models[k].attend_with_prev(new_state[k], w1dt[k], prefix_atts[k])
best_ic = np.argmax(att_weights[k].vec_value())
startt = min(best_ic-2, N-6)
if startt < 0: