-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnmt.py
executable file
·1215 lines (1029 loc) · 53.4 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Build a neural machine translation model with soft attention
'''
import theano
import theano.tensor as tensor
from theano.sandbox.rng_mrg import MRG_RandomStreams as RandomStreams
import cPickle as pkl
import json
import ipdb
import numpy
import copy
import argparse
import os
import warnings
import sys
import time
# from subprocess import Popen
import subprocess
from collections import OrderedDict
profile = False
from data_iterator import TextIterator
from util import *
from theano_util import *
from alignment_util import *
from layers import *
from initializers import *
from optimizers import *
from domain_interpolation_data_iterator import DomainInterpolatorTextIterator
# batch preparation
def prepare_data(seqs_x, seqs_y, maxlen=None, n_words_src=30000,
n_words=30000):
# x: a list of sentences
lengths_x = [len(s) for s in seqs_x]
lengths_y = [len(s) for s in seqs_y]
if maxlen is not None:
new_seqs_x = []
new_seqs_y = []
new_lengths_x = []
new_lengths_y = []
for l_x, s_x, l_y, s_y in zip(lengths_x, seqs_x, lengths_y, seqs_y):
if l_x < maxlen and l_y < maxlen:
new_seqs_x.append(s_x)
new_lengths_x.append(l_x)
new_seqs_y.append(s_y)
new_lengths_y.append(l_y)
lengths_x = new_lengths_x
seqs_x = new_seqs_x
lengths_y = new_lengths_y
seqs_y = new_seqs_y
if len(lengths_x) < 1 or len(lengths_y) < 1:
return None, None, None, None
n_samples = len(seqs_x)
n_factors = len(seqs_x[0][0])
maxlen_x = numpy.max(lengths_x) + 1
maxlen_y = numpy.max(lengths_y) + 1
x = numpy.zeros((n_factors, maxlen_x, n_samples)).astype('int64')
y = numpy.zeros((maxlen_y, n_samples)).astype('int64')
x_mask = numpy.zeros((maxlen_x, n_samples)).astype('float32')
y_mask = numpy.zeros((maxlen_y, n_samples)).astype('float32')
for idx, [s_x, s_y] in enumerate(zip(seqs_x, seqs_y)):
x[:, :lengths_x[idx], idx] = zip(*s_x)
x_mask[:lengths_x[idx]+1, idx] = 1.
y[:lengths_y[idx], idx] = s_y
y_mask[:lengths_y[idx]+1, idx] = 1.
return x, x_mask, y, y_mask
# initialize all parameters
def init_params(options):
params = OrderedDict()
# embedding
for factor in range(options['factors']):
params[embedding_name(factor)] = norm_weight(options['n_words_src'], options['dim_per_factor'][factor])
params['Wemb_dec'] = norm_weight(options['n_words'], options['dim_word'])
# encoder: bidirectional RNN
params = get_layer_param(options['encoder'])(options, params,
prefix='encoder',
nin=options['dim_word'],
dim=options['dim'])
params = get_layer_param(options['encoder'])(options, params,
prefix='encoder_r',
nin=options['dim_word'],
dim=options['dim'])
ctxdim = 2 * options['dim']
# init_state, init_cell
params = get_layer_param('ff')(options, params, prefix='ff_state',
nin=ctxdim, nout=options['dim'])
# decoder
params = get_layer_param(options['decoder'])(options, params,
prefix='decoder',
nin=options['dim_word'],
dim=options['dim'],
dimctx=ctxdim)
# readout
params = get_layer_param('ff')(options, params, prefix='ff_logit_lstm',
nin=options['dim'], nout=options['dim_word'],
ortho=False)
params = get_layer_param('ff')(options, params, prefix='ff_logit_prev',
nin=options['dim_word'],
nout=options['dim_word'], ortho=False)
params = get_layer_param('ff')(options, params, prefix='ff_logit_ctx',
nin=ctxdim, nout=options['dim_word'],
ortho=False)
params = get_layer_param('ff')(options, params, prefix='ff_logit',
nin=options['dim_word'],
nout=options['n_words'])
return params
# bidirectional RNN encoder: take input x (optionally with mask), and produce sequence of context vectors (ctx)
def build_encoder(tparams, options, trng, use_noise, x_mask=None, sampling=False):
x = tensor.tensor3('x', dtype='int64')
x.tag.test_value = (numpy.random.rand(1, 5, 10)*100).astype('int64')
# for the backward rnn, we just need to invert x
xr = x[:,::-1]
if x_mask is None:
xr_mask = None
else:
xr_mask = x_mask[::-1]
n_timesteps = x.shape[1]
n_samples = x.shape[2]
if options['use_dropout']:
retain_probability_emb = 1-options['dropout_embedding']
retain_probability_hidden = 1-options['dropout_hidden']
retain_probability_source = 1-options['dropout_source']
if sampling:
if options['model_version'] < 0.1:
rec_dropout = theano.shared(numpy.array([retain_probability_hidden]*2, dtype='float32'))
rec_dropout_r = theano.shared(numpy.array([retain_probability_hidden]*2, dtype='float32'))
emb_dropout = theano.shared(numpy.array([retain_probability_emb]*2, dtype='float32'))
emb_dropout_r = theano.shared(numpy.array([retain_probability_emb]*2, dtype='float32'))
source_dropout = theano.shared(numpy.float32(retain_probability_source))
else:
rec_dropout = theano.shared(numpy.array([1.]*2, dtype='float32'))
rec_dropout_r = theano.shared(numpy.array([1.]*2, dtype='float32'))
emb_dropout = theano.shared(numpy.array([1.]*2, dtype='float32'))
emb_dropout_r = theano.shared(numpy.array([1.]*2, dtype='float32'))
source_dropout = theano.shared(numpy.float32(1.))
else:
if options['model_version'] < 0.1:
scaled = False
else:
scaled = True
rec_dropout = shared_dropout_layer((2, n_samples, options['dim']), use_noise, trng, retain_probability_hidden, scaled)
rec_dropout_r = shared_dropout_layer((2, n_samples, options['dim']), use_noise, trng, retain_probability_hidden, scaled)
emb_dropout = shared_dropout_layer((2, n_samples, options['dim_word']), use_noise, trng, retain_probability_emb, scaled)
emb_dropout_r = shared_dropout_layer((2, n_samples, options['dim_word']), use_noise, trng, retain_probability_emb, scaled)
source_dropout = shared_dropout_layer((n_timesteps, n_samples, 1), use_noise, trng, retain_probability_source, scaled)
source_dropout = tensor.tile(source_dropout, (1,1,options['dim_word']))
else:
rec_dropout = theano.shared(numpy.array([1.]*2, dtype='float32'))
rec_dropout_r = theano.shared(numpy.array([1.]*2, dtype='float32'))
emb_dropout = theano.shared(numpy.array([1.]*2, dtype='float32'))
emb_dropout_r = theano.shared(numpy.array([1.]*2, dtype='float32'))
# word embedding for forward rnn (source)
emb = []
for factor in range(options['factors']):
emb.append(tparams[embedding_name(factor)][x[factor].flatten()])
emb = concatenate(emb, axis=1)
emb = emb.reshape([n_timesteps, n_samples, options['dim_word']])
if options['use_dropout']:
emb *= source_dropout
proj = get_layer_constr(options['encoder'])(tparams, emb, options,
prefix='encoder',
mask=x_mask,
emb_dropout=emb_dropout,
rec_dropout=rec_dropout,
profile=profile)
# word embedding for backward rnn (source)
embr = []
for factor in range(options['factors']):
embr.append(tparams[embedding_name(factor)][xr[factor].flatten()])
embr = concatenate(embr, axis=1)
embr = embr.reshape([n_timesteps, n_samples, options['dim_word']])
if options['use_dropout']:
if sampling:
embr *= source_dropout
else:
# we drop out the same words in both directions
embr *= source_dropout[::-1]
projr = get_layer_constr(options['encoder'])(tparams, embr, options,
prefix='encoder_r',
mask=xr_mask,
emb_dropout=emb_dropout_r,
rec_dropout=rec_dropout_r,
profile=profile)
# context will be the concatenation of forward and backward rnns
ctx = concatenate([proj[0], projr[0][::-1]], axis=proj[0].ndim-1)
return x, ctx
# build a training model
def build_model(tparams, options):
opt_ret = dict()
trng = RandomStreams(1234)
use_noise = theano.shared(numpy.float32(0.))
x_mask = tensor.matrix('x_mask', dtype='float32')
x_mask.tag.test_value = numpy.ones(shape=(5, 10)).astype('float32')
y = tensor.matrix('y', dtype='int64')
y.tag.test_value = (numpy.random.rand(8, 10)*100).astype('int64')
y_mask = tensor.matrix('y_mask', dtype='float32')
y_mask.tag.test_value = numpy.ones(shape=(8, 10)).astype('float32')
x, ctx = build_encoder(tparams, options, trng, use_noise, x_mask, sampling=False)
n_samples = x.shape[2]
n_timesteps_trg = y.shape[0]
if options['use_dropout']:
retain_probability_emb = 1-options['dropout_embedding']
retain_probability_hidden = 1-options['dropout_hidden']
retain_probability_target = 1-options['dropout_target']
if options['model_version'] < 0.1:
scaled = False
else:
scaled = True
rec_dropout_d = shared_dropout_layer((5, n_samples, options['dim']), use_noise, trng, retain_probability_hidden, scaled)
emb_dropout_d = shared_dropout_layer((2, n_samples, options['dim_word']), use_noise, trng, retain_probability_emb, scaled)
ctx_dropout_d = shared_dropout_layer((4, n_samples, 2*options['dim']), use_noise, trng, retain_probability_hidden, scaled)
target_dropout = shared_dropout_layer((n_timesteps_trg, n_samples, 1), use_noise, trng, retain_probability_target, scaled)
target_dropout = tensor.tile(target_dropout, (1,1,options['dim_word']))
else:
rec_dropout_d = theano.shared(numpy.array([1.]*5, dtype='float32'))
emb_dropout_d = theano.shared(numpy.array([1.]*2, dtype='float32'))
ctx_dropout_d = theano.shared(numpy.array([1.]*4, dtype='float32'))
# mean of the context (across time) will be used to initialize decoder rnn
ctx_mean = (ctx * x_mask[:, :, None]).sum(0) / x_mask.sum(0)[:, None]
# or you can use the last state of forward + backward encoder rnns
# ctx_mean = concatenate([proj[0][-1], projr[0][-1]], axis=proj[0].ndim-2)
if options['use_dropout']:
ctx_mean *= shared_dropout_layer((n_samples, 2*options['dim']), use_noise, trng, retain_probability_hidden, scaled)
# initial decoder state
init_state = get_layer_constr('ff')(tparams, ctx_mean, options,
prefix='ff_state', activ='tanh')
# word embedding (target), we will shift the target sequence one time step
# to the right. This is done because of the bi-gram connections in the
# readout and decoder rnn. The first target will be all zeros and we will
# not condition on the last output.
emb = tparams['Wemb_dec'][y.flatten()]
emb = emb.reshape([n_timesteps_trg, n_samples, options['dim_word']])
emb_shifted = tensor.zeros_like(emb)
emb_shifted = tensor.set_subtensor(emb_shifted[1:], emb[:-1])
emb = emb_shifted
if options['use_dropout']:
emb *= target_dropout
# decoder - pass through the decoder conditional gru with attention
proj = get_layer_constr(options['decoder'])(tparams, emb, options,
prefix='decoder',
mask=y_mask, context=ctx,
context_mask=x_mask,
one_step=False,
init_state=init_state,
emb_dropout=emb_dropout_d,
ctx_dropout=ctx_dropout_d,
rec_dropout=rec_dropout_d,
profile=profile)
# hidden states of the decoder gru
proj_h = proj[0]
# weighted averages of context, generated by attention module
ctxs = proj[1]
if options['use_dropout']:
proj_h *= shared_dropout_layer((n_samples, options['dim']), use_noise, trng, retain_probability_hidden, scaled)
emb *= shared_dropout_layer((n_samples, options['dim_word']), use_noise, trng, retain_probability_emb, scaled)
ctxs *= shared_dropout_layer((n_samples, 2*options['dim']), use_noise, trng, retain_probability_hidden, scaled)
# weights (alignment matrix) #####LIUCAN: this is where the attention vector is.
# why attention vector? why not attention method?
opt_ret['dec_alphas'] = proj[2]
# compute word probabilities
logit_lstm = get_layer_constr('ff')(tparams, proj_h, options,
prefix='ff_logit_lstm', activ='linear')
logit_prev = get_layer_constr('ff')(tparams, emb, options,
prefix='ff_logit_prev', activ='linear')
logit_ctx = get_layer_constr('ff')(tparams, ctxs, options,
prefix='ff_logit_ctx', activ='linear')
logit = tensor.tanh(logit_lstm+logit_prev+logit_ctx)
if options['use_dropout']:
logit *= shared_dropout_layer((n_samples, options['dim_word']), use_noise, trng, retain_probability_hidden, scaled)
logit = get_layer_constr('ff')(tparams, logit, options,
prefix='ff_logit', activ='linear')
logit_shp = logit.shape
probs = tensor.nnet.softmax(logit.reshape([logit_shp[0]*logit_shp[1],
logit_shp[2]]))
# cost
y_flat = y.flatten()
y_flat_idx = tensor.arange(y_flat.shape[0]) * options['n_words'] + y_flat
cost = -tensor.log(probs.flatten()[y_flat_idx])
cost = cost.reshape([y.shape[0], y.shape[1]])
cost = (cost * y_mask).sum(0)
# cost for alignment
#print "Print out in build_model()"
#print opt_ret
return trng, use_noise, x, x_mask, y, y_mask, opt_ret, cost
# build a sampler
def build_sampler(tparams, options, use_noise, trng, return_alignment=False):
if options['use_dropout'] and options['model_version'] < 0.1:
retain_probability_emb = 1-options['dropout_embedding']
retain_probability_hidden = 1-options['dropout_hidden']
retain_probability_source = 1-options['dropout_source']
retain_probability_target = 1-options['dropout_target']
rec_dropout_d = theano.shared(numpy.array([retain_probability_hidden]*5, dtype='float32'))
emb_dropout_d = theano.shared(numpy.array([retain_probability_emb]*2, dtype='float32'))
ctx_dropout_d = theano.shared(numpy.array([retain_probability_hidden]*4, dtype='float32'))
target_dropout = theano.shared(numpy.float32(retain_probability_target))
else:
rec_dropout_d = theano.shared(numpy.array([1.]*5, dtype='float32'))
emb_dropout_d = theano.shared(numpy.array([1.]*2, dtype='float32'))
ctx_dropout_d = theano.shared(numpy.array([1.]*4, dtype='float32'))
x, ctx = build_encoder(tparams, options, trng, use_noise, x_mask=None, sampling=True)
n_samples = x.shape[2]
# get the input for decoder rnn initializer mlp
ctx_mean = ctx.mean(0)
# ctx_mean = concatenate([proj[0][-1],projr[0][-1]], axis=proj[0].ndim-2)
if options['use_dropout'] and options['model_version'] < 0.1:
ctx_mean *= retain_probability_hidden
init_state = get_layer_constr('ff')(tparams, ctx_mean, options,
prefix='ff_state', activ='tanh')
print >>sys.stderr, 'Building f_init...',
outs = [init_state, ctx]
f_init = theano.function([x], outs, name='f_init', profile=profile)
print >>sys.stderr, 'Done'
# x: 1 x 1
y = tensor.vector('y_sampler', dtype='int64')
init_state = tensor.matrix('init_state', dtype='float32')
# if it's the first word, emb should be all zero and it is indicated by -1
emb = tensor.switch(y[:, None] < 0,
tensor.alloc(0., 1, tparams['Wemb_dec'].shape[1]),
tparams['Wemb_dec'][y])
if options['use_dropout'] and options['model_version'] < 0.1:
emb *= target_dropout
# apply one step of conditional gru with attention
proj = get_layer_constr(options['decoder'])(tparams, emb, options,
prefix='decoder',
mask=None, context=ctx,
one_step=True,
init_state=init_state,
emb_dropout=emb_dropout_d,
ctx_dropout=ctx_dropout_d,
rec_dropout=rec_dropout_d,
profile=profile)
# get the next hidden state
next_state = proj[0]
# get the weighted averages of context for this target word y
ctxs = proj[1]
# alignment matrix (attention model)
dec_alphas = proj[2]
if options['use_dropout'] and options['model_version'] < 0.1:
next_state_up = next_state * retain_probability_hidden
emb *= retain_probability_emb
ctxs *= retain_probability_hidden
else:
next_state_up = next_state
logit_lstm = get_layer_constr('ff')(tparams, next_state_up, options,
prefix='ff_logit_lstm', activ='linear')
logit_prev = get_layer_constr('ff')(tparams, emb, options,
prefix='ff_logit_prev', activ='linear')
logit_ctx = get_layer_constr('ff')(tparams, ctxs, options,
prefix='ff_logit_ctx', activ='linear')
logit = tensor.tanh(logit_lstm+logit_prev+logit_ctx)
if options['use_dropout'] and options['model_version'] < 0.1:
logit *= retain_probability_hidden
logit = get_layer_constr('ff')(tparams, logit, options,
prefix='ff_logit', activ='linear')
# compute the softmax probability
next_probs = tensor.nnet.softmax(logit)
# sample from softmax distribution to get the sample
next_sample = trng.multinomial(pvals=next_probs).argmax(1)
# compile a function to do the whole thing above, next word probability,
# sampled word for the next target, next hidden state to be used
print >>sys.stderr, 'Building f_next..',
inps = [y, ctx, init_state]
outs = [next_probs, next_sample, next_state]
if return_alignment:
outs.append(dec_alphas)
f_next = theano.function(inps, outs, name='f_next', profile=profile)
print >>sys.stderr, 'Done'
return f_init, f_next
# generate sample, either with stochastic sampling or beam search. Note that,
# this function iteratively calls f_init and f_next functions.
def gen_sample(f_init, f_next, x, trng=None, k=1, maxlen=30,
stochastic=True, argmax=False, return_alignment=False, suppress_unk=False,
return_hyp_graph=False):
# k is the beam size we have
if k > 1:
assert not stochastic, \
'Beam search does not support stochastic sampling'
sample = []
sample_score = []
sample_word_probs = []
alignment = []
hyp_graph = None
if stochastic:
sample_score = 0
if return_hyp_graph:
from hypgraph import HypGraph
hyp_graph = HypGraph()
live_k = 1
dead_k = 0
hyp_samples = [[]] * live_k
word_probs = [[]] * live_k
hyp_scores = numpy.zeros(live_k).astype('float32')
hyp_states = []
if return_alignment:
hyp_alignment = [[] for _ in xrange(live_k)]
# for ensemble decoding, we keep track of states and probability distribution
# for each model in the ensemble
num_models = len(f_init)
next_state = [None]*num_models
ctx0 = [None]*num_models
next_p = [None]*num_models
dec_alphas = [None]*num_models
# get initial state of decoder rnn and encoder context
for i in xrange(num_models):
ret = f_init[i](x)
next_state[i] = ret[0]
ctx0[i] = ret[1]
next_w = -1 * numpy.ones((1,)).astype('int64') # bos indicator
# x is a sequence of word ids followed by 0, eos id
for ii in xrange(maxlen):
for i in xrange(num_models):
ctx = numpy.tile(ctx0[i], [live_k, 1])
inps = [next_w, ctx, next_state[i]]
ret = f_next[i](*inps)
# dimension of dec_alpha (k-beam-size, number-of-input-hidden-units)
next_p[i], next_w_tmp, next_state[i] = ret[0], ret[1], ret[2]
if return_alignment:
dec_alphas[i] = ret[3]
if suppress_unk:
next_p[i][:,1] = -numpy.inf
if stochastic:
if argmax:
nw = sum(next_p)[0].argmax()
else:
nw = next_w_tmp[0]
sample.append(nw)
sample_score += numpy.log(next_p[0][0, nw])
if nw == 0:
break
else:
cand_scores = hyp_scores[:, None] - sum(numpy.log(next_p))
probs = sum(next_p)/num_models
cand_flat = cand_scores.flatten()
probs_flat = probs.flatten()
ranks_flat = cand_flat.argpartition(k-dead_k-1)[:(k-dead_k)]
#averaging the attention weights accross models
if return_alignment:
mean_alignment = sum(dec_alphas)/num_models
voc_size = next_p[0].shape[1]
# index of each k-best hypothesis
trans_indices = ranks_flat / voc_size
word_indices = ranks_flat % voc_size
costs = cand_flat[ranks_flat]
new_hyp_samples = []
new_hyp_scores = numpy.zeros(k-dead_k).astype('float32')
new_word_probs = []
new_hyp_states = []
if return_alignment:
# holds the history of attention weights for each time step for each of the surviving hypothesis
# dimensions (live_k * target_words * source_hidden_units]
# at each time step we append the attention weights corresponding to the current target word
new_hyp_alignment = [[] for _ in xrange(k-dead_k)]
# ti -> index of k-best hypothesis
for idx, [ti, wi] in enumerate(zip(trans_indices, word_indices)):
new_hyp_samples.append(hyp_samples[ti]+[wi])
new_word_probs.append(word_probs[ti] + [probs_flat[ranks_flat[idx]].tolist()])
new_hyp_scores[idx] = copy.copy(costs[idx])
new_hyp_states.append([copy.copy(next_state[i][ti]) for i in xrange(num_models)])
if return_alignment:
# get history of attention weights for the current hypothesis
new_hyp_alignment[idx] = copy.copy(hyp_alignment[ti])
# extend the history with current attention weights
new_hyp_alignment[idx].append(mean_alignment[ti])
# check the finished samples
new_live_k = 0
hyp_samples = []
hyp_scores = []
hyp_states = []
word_probs = []
if return_alignment:
hyp_alignment = []
# sample and sample_score hold the k-best translations and their scores
for idx in xrange(len(new_hyp_samples)):
if return_hyp_graph:
word, history = new_hyp_samples[idx][-1], new_hyp_samples[idx][:-1]
score = new_hyp_scores[idx]
word_prob = new_word_probs[idx][-1]
hyp_graph.add(word, history, word_prob=word_prob, cost=score)
if new_hyp_samples[idx][-1] == 0:
sample.append(new_hyp_samples[idx])
sample_score.append(new_hyp_scores[idx])
sample_word_probs.append(new_word_probs[idx])
if return_alignment:
alignment.append(new_hyp_alignment[idx])
dead_k += 1
else:
new_live_k += 1
hyp_samples.append(new_hyp_samples[idx])
hyp_scores.append(new_hyp_scores[idx])
hyp_states.append(new_hyp_states[idx])
word_probs.append(new_word_probs[idx])
if return_alignment:
hyp_alignment.append(new_hyp_alignment[idx])
hyp_scores = numpy.array(hyp_scores)
live_k = new_live_k
if new_live_k < 1:
break
if dead_k >= k:
break
next_w = numpy.array([w[-1] for w in hyp_samples])
next_state = [numpy.array(state) for state in zip(*hyp_states)]
if not stochastic:
# dump every remaining one
if live_k > 0:
for idx in xrange(live_k):
sample.append(hyp_samples[idx])
sample_score.append(hyp_scores[idx])
sample_word_probs.append(word_probs[idx])
if return_alignment:
alignment.append(hyp_alignment[idx])
if not return_alignment:
alignment = [None for i in range(len(sample))]
return sample, sample_score, sample_word_probs, alignment, hyp_graph
# calculate the log probablities on a given corpus using translation model
def pred_probs(f_log_probs, prepare_data, options, iterator, verbose=True, normalize=False, alignweights=False):
probs = []
n_done = 0
alignments_json = []
for x, y in iterator:
#ensure consistency in number of factors
if len(x[0][0]) != options['factors']:
sys.stderr.write('Error: mismatch between number of factors in settings ({0}), and number in validation corpus ({1})\n'.format(options['factors'], len(x[0][0])))
sys.exit(1)
n_done += len(x)
x, x_mask, y, y_mask = prepare_data(x, y,
n_words_src=options['n_words_src'],
n_words=options['n_words'])
### in optional save weights mode.
if alignweights:
pprobs, attention = f_log_probs(x, x_mask, y, y_mask)
for jdata in get_alignments(attention, x_mask, y_mask):
alignments_json.append(jdata)
else:
pprobs = f_log_probs(x, x_mask, y, y_mask)
# normalize scores according to output length
if normalize:
lengths = numpy.array([numpy.count_nonzero(s) for s in y_mask.T])
pprobs /= lengths
for pp in pprobs:
probs.append(pp)
if numpy.isnan(numpy.mean(probs)):
ipdb.set_trace()
if verbose:
print >>sys.stderr, '%d samples computed' % (n_done)
return numpy.array(probs), alignments_json
def train(dim_word=100, # word vector dimensionality
dim=1000, # the number of LSTM units
factors=1, # input factors
dim_per_factor=None, # list of word vector dimensionalities (one per factor): [250,200,50] for total dimensionality of 500
encoder='gru',
decoder='gru_cond',
patience=10, # early stopping patience
max_epochs=5000,
finish_after=10000000, # finish after this many updates
dispFreq=100,
decay_c=0., # L2 regularization penalty
map_decay_c=0., # L2 regularization penalty towards original weights
alpha_c=0., # alignment regularization
clip_c=-1., # gradient clipping threshold
lrate=0.01, # learning rate
n_words_src=None, # source vocabulary size
n_words=None, # target vocabulary size
maxlen=100, # maximum length of the description
optimizer='rmsprop',
batch_size=16,
valid_batch_size=16,
saveto='model.npz',
validFreq=1000,
saveFreq=1000, # save the parameters after every saveFreq updates
sampleFreq=100, # generate some samples after every sampleFreq
datasets=[
'/data/lisatmp3/chokyun/europarl/europarl-v7.fr-en.en.tok',
'/data/lisatmp3/chokyun/europarl/europarl-v7.fr-en.fr.tok'],
valid_datasets=['../data/dev/newstest2011.en.tok',
'../data/dev/newstest2011.fr.tok'],
dictionaries=[
'/data/lisatmp3/chokyun/europarl/europarl-v7.fr-en.en.tok.pkl',
'/data/lisatmp3/chokyun/europarl/europarl-v7.fr-en.fr.tok.pkl'],
use_dropout=False,
dropout_embedding=0.2, # dropout for input embeddings (0: no dropout)
dropout_hidden=0.5, # dropout for hidden layers (0: no dropout)
dropout_source=0, # dropout source words (0: no dropout)
dropout_target=0, # dropout target words (0: no dropout)
reload_=False,
overwrite=False,
external_validation_script=None,
shuffle_each_epoch=True,
finetune=False,
finetune_only_last=False,
sort_by_length=True,
use_domain_interpolation=False,
domain_interpolation_min=0.1,
domain_interpolation_inc=0.1,
domain_interpolation_indomain_datasets=['indomain.en', 'indomain.fr'],
maxibatch_size=20, #How many minibatches to load at one time
model_version=0.1, #store version used for training for compatibility
):
###################### Model options
model_options = locals().copy()
if model_options['dim_per_factor'] == None: # if no-specific assignment for specific embedding, make it uniform length of ['dim_word']
if factors == 1:
model_options['dim_per_factor'] = [model_options['dim_word']]
else:
sys.stderr.write('Error: if using factored input, you must specify \'dim_per_factor\'\n')
sys.exit(1)
assert(len(dictionaries) == factors + 1) # one dictionary per source factor + 1 for target factor
assert(len(model_options['dim_per_factor']) == factors) # each factor embedding has its own dimensionality
assert(sum(model_options['dim_per_factor']) == model_options['dim_word']) # dimensionality of factor embeddings sums up to total dimensionality of input embedding vector
##################### load dictionaries and invert them
worddicts = [None] * len(dictionaries)
worddicts_r = [None] * len(dictionaries)
for ii, dd in enumerate(dictionaries):
worddicts[ii] = load_dict(dd)
worddicts_r[ii] = dict()
for kk, vv in worddicts[ii].iteritems():
worddicts_r[ii][vv] = kk
if n_words_src is None:
n_words_src = len(worddicts[0])
model_options['n_words_src'] = n_words_src
if n_words is None:
n_words = len(worddicts[1])
model_options['n_words'] = n_words
#################### loading data
print 'Loading data'
domain_interpolation_cur = None
if use_domain_interpolation:
print 'Using domain interpolation with initial ratio %s, increase rate %s' % (domain_interpolation_min, domain_interpolation_inc)
domain_interpolation_cur = domain_interpolation_min
train = DomainInterpolatorTextIterator(datasets[0], datasets[1],
dictionaries[:-1], dictionaries[1],
n_words_source=n_words_src, n_words_target=n_words,
batch_size=batch_size,
maxlen=maxlen,
shuffle_each_epoch=shuffle_each_epoch,
sort_by_length=sort_by_length,
indomain_source=domain_interpolation_indomain_datasets[0],
indomain_target=domain_interpolation_indomain_datasets[1],
interpolation_rate=domain_interpolation_cur,
maxibatch_size=maxibatch_size)
else:
train = TextIterator(datasets[0], datasets[1],
dictionaries[:-1], dictionaries[-1],
n_words_source=n_words_src, n_words_target=n_words,
batch_size=batch_size,
maxlen=maxlen,
skip_empty=True,
shuffle_each_epoch=shuffle_each_epoch,
sort_by_length=sort_by_length,
maxibatch_size=maxibatch_size)
if valid_datasets and validFreq:
valid = TextIterator(valid_datasets[0], valid_datasets[1],
dictionaries[:-1], dictionaries[-1],
n_words_source=n_words_src, n_words_target=n_words,
batch_size=valid_batch_size,
maxlen=maxlen)
else:
valid = None
########### Start computing - timestamp
comp_start = time.time()
print 'Building model'
params = init_params(model_options)
########### reload parameters
if reload_ and os.path.exists(saveto):
print 'Reloading model parameters'
params = load_params(saveto, params)
########### Theano parameters
tparams = init_theano_params(params)
trng, use_noise, \
x, x_mask, y, y_mask, \
opt_ret, \
cost = \
build_model(tparams, model_options)
inps = [x, x_mask, y, y_mask]
if validFreq or sampleFreq:
print 'Building sampler'
f_init, f_next = build_sampler(tparams, model_options, use_noise, trng)
# before any regularizer
print 'Building f_log_probs...',
f_log_probs = theano.function(inps, cost, profile=profile)
print 'Done'
cost = cost.mean()
# apply L2 regularization on weights
if decay_c > 0.:
decay_c = theano.shared(numpy.float32(decay_c), name='decay_c')
weight_decay = 0.
for kk, vv in tparams.iteritems():
weight_decay += (vv ** 2).sum()
weight_decay *= decay_c
cost += weight_decay
# regularize the alpha weights
if alpha_c > 0. and not model_options['decoder'].endswith('simple'):
alpha_c = theano.shared(numpy.float32(alpha_c), name='alpha_c')
alpha_reg = alpha_c * (
(tensor.cast(y_mask.sum(0)//x_mask.sum(0), 'float32')[:, None] -
opt_ret['dec_alphas'].sum(0))**2).sum(1).mean()
cost += alpha_reg
# apply L2 regularisation to loaded model (map training)
if map_decay_c > 0:
map_decay_c = theano.shared(numpy.float32(map_decay_c), name="map_decay_c")
weight_map_decay = 0.
for kk, vv in tparams.iteritems():
init_value = theano.shared(vv.get_value(), name= kk + "_init")
weight_map_decay += ((vv -init_value) ** 2).sum()
weight_map_decay *= map_decay_c
cost += weight_map_decay
# allow finetuning with fixed embeddings
if finetune:
updated_params = OrderedDict([(key,value) for (key,value) in tparams.iteritems() if not key.startswith('Wemb')])
else:
updated_params = tparams
# allow finetuning of only last layer (becomes a linear model training problem)
if finetune_only_last:
updated_params = OrderedDict([(key,value) for (key,value) in tparams.iteritems() if key in ['ff_logit_W', 'ff_logit_b']])
else:
updated_params = tparams
print 'Computing gradient...',
grads = tensor.grad(cost, wrt=itemlist(updated_params))
print 'Done'
# apply gradient clipping here
if clip_c > 0.:
g2 = 0.
for g in grads:
g2 += (g**2).sum()
new_grads = []
for g in grads:
new_grads.append(tensor.switch(g2 > (clip_c**2),
g / tensor.sqrt(g2) * clip_c,
g))
grads = new_grads
# compile the optimizer, the actual computational graph is compiled here
lr = tensor.scalar(name='lr')
print 'Building optimizers...',
f_grad_shared, f_update = eval(optimizer)(lr, updated_params, grads, inps, cost, profile=profile)
print 'Done'
print 'Total compilation time: {0:.1f}s'.format(time.time() - comp_start)
print 'Optimization'
best_p = None
bad_counter = 0
uidx = 0
estop = False
history_errs = []
# reload history
if reload_ and os.path.exists(saveto):
rmodel = numpy.load(saveto)
history_errs = list(rmodel['history_errs'])
if 'uidx' in rmodel:
uidx = rmodel['uidx']
#save model options
json.dump(model_options, open('%s.json' % saveto, 'wb'), indent=2)
if validFreq == -1:
validFreq = len(train[0])/batch_size
if saveFreq == -1:
saveFreq = len(train[0])/batch_size
if sampleFreq == -1:
sampleFreq = len(train[0])/batch_size
valid_err = None
last_disp_samples = 0
ud_start = time.time()
p_validation = None
for eidx in xrange(max_epochs):
n_samples = 0
for x, y in train:
n_samples += len(x)
last_disp_samples += len(x)
uidx += 1
use_noise.set_value(1.)
#ensure consistency in number of factors
if len(x) and len(x[0]) and len(x[0][0]) != factors:
sys.stderr.write('Error: mismatch between number of factors in settings ({0}), and number in training corpus ({1})\n'.format(factors, len(x[0][0])))
sys.exit(1)
x, x_mask, y, y_mask = prepare_data(x, y, maxlen=maxlen,
n_words_src=n_words_src,
n_words=n_words)
if x is None:
print 'Minibatch with zero sample under length ', maxlen
uidx -= 1
continue
# compute cost, grads and copy grads to shared variables
cost = f_grad_shared(x, x_mask, y, y_mask)
# do the update on parameters
f_update(lrate)
# check for bad numbers, usually we remove non-finite elements
# and continue training - but not done here
if numpy.isnan(cost) or numpy.isinf(cost):
print 'NaN detected'
return 1., 1., 1.
# verbose
if numpy.mod(uidx, dispFreq) == 0:
ud = time.time() - ud_start
wps = (last_disp_samples) / float(ud)
print 'Epoch ', eidx, 'Update ', uidx, 'Cost ', cost, 'UD ', ud, "{0:.2f} sentences/s".format(wps)
ud_start = time.time()
last_disp_samples = 0
# save the best model so far, in addition, save the latest model
# into a separate file with the iteration number for external eval
if numpy.mod(uidx, saveFreq) == 0:
print 'Saving the best model...',
if best_p is not None:
params = best_p
else:
params = unzip_from_theano(tparams)
numpy.savez(saveto, history_errs=history_errs, uidx=uidx, **params)
print 'Done'
# save with uidx
if not overwrite:
print 'Saving the model at iteration {}...'.format(uidx),
saveto_uidx = '{}.iter{}.npz'.format(
os.path.splitext(saveto)[0], uidx)
numpy.savez(saveto_uidx, history_errs=history_errs,
uidx=uidx, **unzip_from_theano(tparams))
print 'Done'
# generate some samples with the model and display them
if sampleFreq and numpy.mod(uidx, sampleFreq) == 0:
# FIXME: random selection?
for jj in xrange(numpy.minimum(5, x.shape[2])):
stochastic = True
x_current = x[:, :, jj][:, :, None]
# remove padding
x_current = x_current[:,:x_mask.astype('int64')[:, jj].sum(),:]
sample, score, sample_word_probs, alignment, hyp_graph = gen_sample([f_init], [f_next],
x_current,
trng=trng, k=1,
maxlen=30,
stochastic=stochastic,
argmax=False,
suppress_unk=False,
return_hyp_graph=False)
print 'Source ', jj, ': ',
for pos in range(x.shape[1]):
if x[0, pos, jj] == 0:
break
for factor in range(factors):
vv = x[factor, pos, jj]
if vv in worddicts_r[factor]:
sys.stdout.write(worddicts_r[factor][vv])
else:
sys.stdout.write('UNK')