forked from sordonia/hed-dlg
-
Notifications
You must be signed in to change notification settings - Fork 43
/
dialog_encdec.py
1283 lines (1044 loc) · 61.5 KB
/
dialog_encdec.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
"""
Dialog hierarchical encoder-decoder code.
The code is inspired from nmt encdec code in groundhog
but we do not rely on groundhog infrastructure.
"""
__docformat__ = 'restructedtext en'
__authors__ = ("Alessandro Sordoni, Iulian Vlad Serban")
__contact__ = "Alessandro Sordoni <[email protected]>"
import theano
import theano.tensor as T
import numpy as np
import cPickle
import logging
logger = logging.getLogger(__name__)
from theano.sandbox.scan import scan
from theano.sandbox.rng_mrg import MRG_RandomStreams
from theano.tensor.nnet.conv3d2d import *
from collections import OrderedDict
from model import *
from utils import *
import operator
# Theano speed-up
theano.config.scan.allow_gc = False
#
def add_to_params(params, new_param):
params.append(new_param)
return new_param
class EncoderDecoderBase():
def __init__(self, state, rng, parent):
self.rng = rng
self.parent = parent
self.state = state
self.__dict__.update(state)
self.triple_rec_activation = eval(self.triple_rec_activation)
self.sent_rec_activation = eval(self.sent_rec_activation)
self.params = []
class DCGMEncoder(EncoderDecoderBase):
def init_params(self, word_embedding_param):
# Initialzie W_emb to given word embeddings
assert(word_embedding_param != None)
self.W_emb = word_embedding_param
self.Wq_in = add_to_params(self.params, \
theano.shared(value=NormalInit(self.rng, self.rankdim, self.qdim), name='dcgm_Wq_in'))
self.bq_in = add_to_params(self.params, \
theano.shared(value=np.zeros((self.qdim,), dtype='float32'), name='dcgm_bq_in'))
self.Ws_in = add_to_params(self.params, \
theano.shared(value=NormalInit(self.rng, self.qdim, self.sdim), name='dcgm_Ws_in'))
self.bs_in = add_to_params(self.params, \
theano.shared(value=np.zeros((self.sdim,), dtype='float32'), name='dcgm_bs_in'))
def mean_step(self, x_t, m_t, *args):
args = iter(args)
# already computed avg
avg_past = next(args)
n_past = next(args)
m_t = m_t
# reset avg
avg_past_r = m_t[:, None] * avg_past
n_past_r = m_t * n_past
n = n_past_r + 1.
avg = (avg_past_r * (n[:, None] - 1) + x_t) / n[:, None]
# return state and pooled state
return avg, n
def approx_embedder(self, x):
return self.W_emb[x]
def build_encoder(self, x, xmask=None, **kwargs):
# One step not supported
assert len(kwargs) == 0
assert x.ndim == 2
batch_size = x.shape[1]
avg_0 = T.alloc(np.float32(0), batch_size, self.rankdim)
n_0 = T.alloc(np.float32(0), batch_size)
# in sampling mode (i.e. one step) we require
xe = self.approx_embedder(x)
if xmask == None:
xmask = T.neq(x, self.eos_sym)
# Here we roll the mask so we avoid the need for separate
# hr and h. The trick is simple: if the original mask is
# 0 1 1 0 1 1 1 0 0 0 0 0 -- batch is filled with eos_sym
# the rolled mask will be
# 0 0 1 1 0 1 1 1 0 0 0 0 -- roll to the right
# ^ ^
# two resets </s> <s>
# the first reset will reset h_init = 0
# the second will reset </s> and update given x_t = <s>
rolled_xmask = T.roll(xmask, 1, axis=0)
f_enc = self.mean_step
o_enc_info = [avg_0, n_0]
_res, _ = theano.scan(f_enc,
sequences=[xe, rolled_xmask],\
outputs_info=o_enc_info)
avg = _res[0]
# Linear activation
avg_q = T.dot(avg, self.Wq_in) + self.bq_in
avg_s = self.sent_rec_activation(T.dot(avg_q, self.Ws_in) + self.bs_in)
return avg_s
def __init__(self, state, rng, word_embedding_param, parent):
EncoderDecoderBase.__init__(self, state, rng, parent)
self.init_params(word_embedding_param)
class UtteranceEncoder(EncoderDecoderBase):
def init_params(self, word_embedding_param):
# Initialzie W_emb to given word embeddings
assert(word_embedding_param != None)
self.W_emb = word_embedding_param
""" sent weights """
self.W_in = add_to_params(self.params, theano.shared(value=NormalInit(self.rng, self.rankdim, self.qdim), name='W_in'+self.name))
self.W_hh = add_to_params(self.params, theano.shared(value=OrthogonalInit(self.rng, self.qdim, self.qdim), name='W_hh'+self.name))
self.b_hh = add_to_params(self.params, theano.shared(value=np.zeros((self.qdim,), dtype='float32'), name='b_hh'+self.name))
if self.sent_step_type == "gated":
self.W_in_r = add_to_params(self.params, theano.shared(value=NormalInit(self.rng, self.rankdim, self.qdim), name='W_in_r'+self.name))
self.W_in_z = add_to_params(self.params, theano.shared(value=NormalInit(self.rng, self.rankdim, self.qdim), name='W_in_z'+self.name))
self.W_hh_r = add_to_params(self.params, theano.shared(value=OrthogonalInit(self.rng, self.qdim, self.qdim), name='W_hh_r'+self.name))
self.W_hh_z = add_to_params(self.params, theano.shared(value=OrthogonalInit(self.rng, self.qdim, self.qdim), name='W_hh_z'+self.name))
self.b_z = add_to_params(self.params, theano.shared(value=np.zeros((self.qdim,), dtype='float32'), name='b_z'+self.name))
self.b_r = add_to_params(self.params, theano.shared(value=np.zeros((self.qdim,), dtype='float32'), name='b_r'+self.name))
def plain_sent_step(self, x_t, m_t, *args):
do_pool = False
if len(args) > 1:
do_pool = True
args = iter(args)
h_tm1 = next(args)
if m_t.ndim >= 1:
m_t = m_t.dimshuffle(0, 'x')
hr_tm1 = m_t * h_tm1
h_t = self.sent_rec_activation(T.dot(x_t, self.W_in) + T.dot(hr_tm1, self.W_hh) + self.b_hh)
if do_pool:
# perform pooling with a moving average
h_tm1_pooled = next(args)
pool_len = next(args)
new_pool_len = pool_len * m_t + 1
h_t_pooled = ((new_pool_len - 1) * h_tm1_pooled + h_t**2) / new_pool_len
# return state and pooled state
return [h_t, h_t_pooled, new_pool_len]
else:
# Return hidden state only
return [h_t]
def gated_sent_step(self, x_t, m_t, *args):
do_pool = False
if len(args) >= 3:
do_pool = True
args = iter(args)
h_tm1 = next(args)
if m_t.ndim >= 1:
m_t = m_t.dimshuffle(0, 'x')
hr_tm1 = m_t * h_tm1
r_t = T.nnet.sigmoid(T.dot(x_t, self.W_in_r) + T.dot(hr_tm1, self.W_hh_r) + self.b_r)
z_t = T.nnet.sigmoid(T.dot(x_t, self.W_in_z) + T.dot(hr_tm1, self.W_hh_z) + self.b_z)
h_tilde = self.sent_rec_activation(T.dot(x_t, self.W_in) + T.dot(r_t * hr_tm1, self.W_hh) + self.b_hh)
h_t = (np.float32(1.0) - z_t) * hr_tm1 + z_t * h_tilde
if do_pool:
# perform pooling with a moving average
h_tm1_pooled = next(args)
pool_len = next(args)
new_pool_len = pool_len * m_t + 1
h_t_pooled = ((new_pool_len - 1) * h_tm1_pooled + h_t**2) / new_pool_len
# return both reset state and non-reset state
return [h_t, h_t_pooled, new_pool_len, r_t, z_t, h_tilde]
else:
# return both reset state and non-reset state
return [h_t, r_t, z_t, h_tilde]
def approx_embedder(self, x):
return self.W_emb[x]
def build_encoder(self, x, xmask=None, return_L2_pooling = False, **kwargs):
one_step = False
if len(kwargs):
one_step = True
# if x.ndim == 2 then
# x = (n_steps, batch_size)
if x.ndim == 2:
batch_size = x.shape[1]
# else x = (word_1, word_2, word_3, ...)
# or x = (last_word_1, last_word_2, last_word_3, ..)
# in this case batch_size is
else:
batch_size = 1
# if it is not one_step then we initialize everything to 0
if not one_step:
h_0 = T.alloc(np.float32(0), batch_size, self.qdim)
if return_L2_pooling:
h_0_pooled = T.alloc(np.float32(0), batch_size, self.qdim)
# If we made pool_len a vector, we could theoretically speed up pooling computations,
# but I suspect that theano (e.g. T.tensordot) automatically converts it into a matrix before
# multiplying anyway so it wouldn't make a big difference...
pool_len_0 = T.alloc(np.float32(0), batch_size, self.qdim)
# in sampling mode (i.e. one step) we require
else:
# in this case x.ndim != 2
assert x.ndim != 2
assert 'prev_h' in kwargs
h_0 = kwargs['prev_h']
if return_L2_pooling:
assert 'prev_h_pooled' in kwargs
h_0_pooled = kwargs['prev_h_pooled']
pool_len_0 = T.alloc(np.float32(0), batch_size, self.qdim)
xe = self.approx_embedder(x)
if xmask == None:
xmask = T.neq(x, self.eos_sym)
# Here we roll the mask so we avoid the need for separate
# hr and h. The trick is simple: if the original mask is
# 0 1 1 0 1 1 1 0 0 0 0 0 -- batch is filled with eos_sym
# the rolled mask will be
# 0 0 1 1 0 1 1 1 0 0 0 0 -- roll to the right
# ^ ^
# two resets </s> <s>
# the first reset will reset h_init = 0
# the second will reset </s> and update given x_t = <s>
if xmask.ndim == 2:
rolled_xmask = T.roll(xmask, 1, axis=0)
else:
rolled_xmask = T.roll(xmask, 1)
# Gated Encoder
if self.sent_step_type == "gated":
f_enc = self.gated_sent_step
if return_L2_pooling:
o_enc_info = [h_0, h_0_pooled, pool_len_0, None, None, None]
else:
o_enc_info = [h_0, None, None, None]
else:
f_enc = self.plain_sent_step
if return_L2_pooling:
o_enc_info = [h_0, h_0_pooled, pool_len_0]
else:
o_enc_info = [h_0]
# Run through all the sentence (encode everything)
if not one_step:
_res, _ = theano.scan(f_enc,
sequences=[xe, rolled_xmask],\
outputs_info=o_enc_info)
else: # Make just one step further
if return_L2_pooling:
_res = f_enc(xe, rolled_xmask, [h_0, h_0_pooled, pool_len_0])[0]
else:
_res = f_enc(xe, rolled_xmask, [h_0])[0]
# Get the hidden state sequence
if return_L2_pooling:
h = _res[0]
h_pooled = T.sqrt(_res[1])
return [h, h_pooled]
else:
h = _res[0]
return h
def __init__(self, state, rng, word_embedding_param, parent, name):
EncoderDecoderBase.__init__(self, state, rng, parent)
self.name = name
self.init_params(word_embedding_param)
class DialogEncoder(EncoderDecoderBase):
def init_params(self):
""" Context weights """
if self.bidirectional_utterance_encoder:
# With the bidirectional flag, the dialog encoder gets input
# from both the forward and backward utterance encoders, hence it is double qdim
input_dim = self.qdim * 2
else:
# Without the bidirectional flag, the dialog encoder only gets input
# from the forward utterance encoder, which has dim self.qdim
input_dim = self.qdim
self.Ws_in = add_to_params(self.params, theano.shared(value=NormalInit(self.rng, input_dim, self.sdim), name='Ws_in'))
self.Ws_hh = add_to_params(self.params, theano.shared(value=OrthogonalInit(self.rng, self.sdim, self.sdim), name='Ws_hh'))
self.bs_hh = add_to_params(self.params, theano.shared(value=np.zeros((self.sdim,), dtype='float32'), name='bs_hh'))
if self.triple_step_type == "gated":
self.Ws_in_r = add_to_params(self.params, theano.shared(value=NormalInit(self.rng, input_dim, self.sdim), name='Ws_in_r'))
self.Ws_in_z = add_to_params(self.params, theano.shared(value=NormalInit(self.rng, input_dim, self.sdim), name='Ws_in_z'))
self.Ws_hh_r = add_to_params(self.params, theano.shared(value=OrthogonalInit(self.rng, self.sdim, self.sdim), name='Ws_hh_r'))
self.Ws_hh_z = add_to_params(self.params, theano.shared(value=OrthogonalInit(self.rng, self.sdim, self.sdim), name='Ws_hh_z'))
self.bs_z = add_to_params(self.params, theano.shared(value=np.zeros((self.sdim,), dtype='float32'), name='bs_z'))
self.bs_r = add_to_params(self.params, theano.shared(value=np.zeros((self.sdim,), dtype='float32'), name='bs_r'))
def plain_triple_step(self, h_t, m_t, hs_tm1):
if m_t.ndim >= 1:
m_t = m_t.dimshuffle(0, 'x')
hs_tilde = self.triple_rec_activation(T.dot(h_t, self.Ws_in) + T.dot(hs_tm1, self.Ws_hh) + self.bs_hh)
hs_t = (m_t) * hs_tm1 + (1 - m_t) * hs_tilde
return hs_t
def gated_triple_step(self, h_t, m_t, hs_tm1):
rs_t = T.nnet.sigmoid(T.dot(h_t, self.Ws_in_r) + T.dot(hs_tm1, self.Ws_hh_r) + self.bs_r)
zs_t = T.nnet.sigmoid(T.dot(h_t, self.Ws_in_z) + T.dot(hs_tm1, self.Ws_hh_z) + self.bs_z)
hs_tilde = self.triple_rec_activation(T.dot(h_t, self.Ws_in) + T.dot(rs_t * hs_tm1, self.Ws_hh) + self.bs_hh)
hs_update = (np.float32(1.) - zs_t) * hs_tm1 + zs_t * hs_tilde
if m_t.ndim >= 1:
m_t = m_t.dimshuffle(0, 'x')
hs_t = (m_t) * hs_tm1 + (1 - m_t) * hs_update
return hs_t, hs_tilde, rs_t, zs_t
def build_encoder(self, h, x, xmask=None, **kwargs):
one_step = False
if len(kwargs):
one_step = True
# if x.ndim == 2 then
# x = (n_steps, batch_size)
if x.ndim == 2:
batch_size = x.shape[1]
# else x = (word_1, word_2, word_3, ...)
# or x = (last_word_1, last_word_2, last_word_3, ..)
# in this case batch_size is
else:
batch_size = 1
# if it is not one_step then we initialize everything to 0
if not one_step:
hs_0 = T.alloc(np.float32(0), batch_size, self.sdim)
# in sampling mode (i.e. one step) we require
else:
# in this case x.ndim != 2
assert x.ndim != 2
assert 'prev_hs' in kwargs
hs_0 = kwargs['prev_hs']
if xmask == None:
xmask = T.neq(x, self.eos_sym)
# Here we roll the mask so we avoid the need for separate
# hr and h. The trick is simple: if the original mask is
# 0 1 1 0 1 1 1 0 0 0 0 0 -- batch is filled with eos_sym
# the rolled mask will be
# 0 0 1 1 0 1 1 1 0 0 0 0 -- roll to the right
# ^ ^
# two resets </s> <s>
# the first reset will reset h_init = 0
# the second will reset </s> and update given x_t = <s>
if xmask.ndim == 2:
rolled_xmask = T.roll(xmask, 1, axis=0)
else:
rolled_xmask = T.roll(xmask, 1)
if self.triple_step_type == "gated":
f_hier = self.gated_triple_step
o_hier_info = [hs_0, None, None, None]
else:
f_hier = self.plain_triple_step
o_hier_info = [hs_0]
# All hierarchical sentence
# The hs sequence is based on the original mask
if not one_step:
_res, _ = theano.scan(f_hier,\
sequences=[h, xmask],\
outputs_info=o_hier_info)
# Just one step further
else:
_res = f_hier(h, xmask, hs_0)
if isinstance(_res, list) or isinstance(_res, tuple):
hs = _res[0]
else:
hs = _res
return hs
def __init__(self, state, rng, parent):
EncoderDecoderBase.__init__(self, state, rng, parent)
self.init_params()
class DialogDummyEncoder(EncoderDecoderBase):
# This dialogue encoder behaves like a DialogEncoder with the identity function.
# At the end of each utterance, the input from the utterance encoder(s) is transferred
# to its hidden state, which can then be transfered to the decoder.
def plain_triple_step(self, h_t, m_t, hs_tm1):
if m_t.ndim >= 1:
m_t = m_t.dimshuffle(0, 'x')
hs_tilde = h_t
hs_t = (m_t) * hs_tm1 + (1 - m_t) * hs_tilde
return hs_t
def build_encoder(self, h, x, xmask=None, **kwargs):
one_step = False
if len(kwargs):
one_step = True
# if x.ndim == 2 then
# x = (n_steps, batch_size)
if x.ndim == 2:
batch_size = x.shape[1]
# else x = (word_1, word_2, word_3, ...)
# or x = (last_word_1, last_word_2, last_word_3, ..)
# in this case batch_size is
else:
batch_size = 1
# if it is not one_step then we initialize everything to 0
if not one_step:
hs_0 = T.alloc(np.float32(0), batch_size, self.inp_dim)
# in sampling mode (i.e. one step) we require
else:
# in this case x.ndim != 2
assert x.ndim != 2
assert 'prev_hs' in kwargs
hs_0 = kwargs['prev_hs']
if xmask == None:
xmask = T.neq(x, self.eos_sym)
# Here we roll the mask so we avoid the need for separate
# hr and h. The trick is simple: if the original mask is
# 0 1 1 0 1 1 1 0 0 0 0 0 -- batch is filled with eos_sym
# the rolled mask will be
# 0 0 1 1 0 1 1 1 0 0 0 0 -- roll to the right
# ^ ^
# two resets </s> <s>
# the first reset will reset h_init = 0
# the second will reset </s> and update given x_t = <s>
if xmask.ndim == 2:
rolled_xmask = T.roll(xmask, 1, axis=0)
else:
rolled_xmask = T.roll(xmask, 1)
f_hier = self.plain_triple_step
o_hier_info = [hs_0]
# All hierarchical sentence
# The hs sequence is based on the original mask
if not one_step:
_res, _ = theano.scan(f_hier,\
sequences=[h, xmask],\
outputs_info=o_hier_info)
# Just one step further
else:
_res = f_hier(h, xmask, hs_0)
if isinstance(_res, list) or isinstance(_res, tuple):
hs = _res[0]
else:
hs = _res
return hs
def __init__(self, state, rng, parent, inp_dim):
self.inp_dim = inp_dim
EncoderDecoderBase.__init__(self, state, rng, parent)
class UtteranceDecoder(EncoderDecoderBase):
NCE = 0
EVALUATION = 1
SAMPLING = 2
BEAM_SEARCH = 3
def __init__(self, state, rng, parent, dialog_encoder, word_embedding_param):
EncoderDecoderBase.__init__(self, state, rng, parent)
# Take as input the encoder instance for the embeddings..
# To modify in the future
assert(word_embedding_param != None)
self.word_embedding_param = word_embedding_param
self.dialog_encoder = dialog_encoder
self.trng = MRG_RandomStreams(self.seed)
self.init_params()
def init_params(self):
if self.direct_connection_between_encoders_and_decoder:
# When there is a direct connection between encoder and decoder,
# the input has dimensionality sdim + qdim if forward encoder, and
# sdim + 2 x qdim for bidirectional encoder
if self.bidirectional_utterance_encoder:
self.input_dim = self.sdim + self.qdim*2
else:
self.input_dim = self.sdim + self.qdim
else:
# When there is no connection between encoder and decoder,
# the input has dimensionality sdim
self.input_dim = self.sdim
if self.add_semantic_information_to_utterance_decoder:
self.input_dim = self.input_dim + self.semantic_information_dim
""" Decoder weights """
self.bd_out = add_to_params(self.params, theano.shared(value=np.zeros((self.idim,), dtype='float32'), name='bd_out'))
self.Wd_emb = add_to_params(self.params, theano.shared(value=NormalInit(self.rng, self.idim, self.rankdim), name='Wd_emb'))
self.Wd_hh = add_to_params(self.params, theano.shared(value=OrthogonalInit(self.rng, self.qdim, self.qdim), name='Wd_hh'))
self.bd_hh = add_to_params(self.params, theano.shared(value=np.zeros((self.qdim,), dtype='float32'), name='bd_hh'))
self.Wd_in = add_to_params(self.params, theano.shared(value=NormalInit(self.rng, self.rankdim, self.qdim), name='Wd_in'))
self.Wd_s_0 = add_to_params(self.params, theano.shared(value=NormalInit(self.rng, self.input_dim, self.qdim), name='Wd_s_0'))
self.bd_s_0 = add_to_params(self.params, theano.shared(value=np.zeros((self.qdim,), dtype='float32'), name='bd_s_0'))
if self.sent_step_type == "gated":
self.Wd_in_r = add_to_params(self.params, theano.shared(value=NormalInit(self.rng, self.rankdim, self.qdim), name='Wd_in_r'))
self.Wd_in_z = add_to_params(self.params, theano.shared(value=NormalInit(self.rng, self.rankdim, self.qdim), name='Wd_in_z'))
self.Wd_hh_r = add_to_params(self.params, theano.shared(value=OrthogonalInit(self.rng, self.qdim, self.qdim), name='Wd_hh_r'))
self.Wd_hh_z = add_to_params(self.params, theano.shared(value=OrthogonalInit(self.rng, self.qdim, self.qdim), name='Wd_hh_z'))
self.bd_r = add_to_params(self.params, theano.shared(value=np.zeros((self.qdim,), dtype='float32'), name='bd_r'))
self.bd_z = add_to_params(self.params, theano.shared(value=np.zeros((self.qdim,), dtype='float32'), name='bd_z'))
if self.decoder_bias_type == 'all':
self.Wd_s_q = add_to_params(self.params, theano.shared(value=NormalInit(self.rng, self.input_dim, self.qdim), name='Wd_s_q'))
self.Wd_s_z = add_to_params(self.params, theano.shared(value=NormalInit(self.rng, self.input_dim, self.qdim), name='Wd_s_z'))
self.Wd_s_r = add_to_params(self.params, theano.shared(value=NormalInit(self.rng, self.input_dim, self.qdim), name='Wd_s_r'))
if self.decoder_bias_type == 'selective':
self.bd_sel = add_to_params(self.params, theano.shared(value=np.zeros((self.input_dim,), dtype='float32'), name='bd_sel'))
self.Wd_s_q = add_to_params(self.params, theano.shared(value=NormalInit(self.rng, self.input_dim, self.qdim), name='Wd_s_q'))
# s -> g_r
self.Wd_sel_s = add_to_params(self.params, \
theano.shared(value=NormalInit(self.rng, self.input_dim, self.input_dim), \
name='Wd_sel_s'))
# x_{n-1} -> g_r
self.Wd_sel_e = add_to_params(self.params, \
theano.shared(value=NormalInit(self.rng, self.rankdim, self.input_dim), \
name='Wd_sel_e'))
# h_{n-1} -> g_r
self.Wd_sel_h = add_to_params(self.params, \
theano.shared(value=NormalInit(self.rng, self.qdim, self.input_dim), \
name='Wd_sel_h'))
######################
# Output layer weights
######################
if self.maxout_out:
if int(self.qdim) != 2*int(self.rankdim):
raise ValueError('Error with maxout configuration in UtteranceDecoder!'
+ 'For maxout to work we need qdim = 2x rankdim')
out_target_dim = self.qdim
if not self.maxout_out:
out_target_dim = self.rankdim
self.Wd_out = add_to_params(self.params, theano.shared(value=NormalInit(self.rng, self.qdim, out_target_dim), name='Wd_out'))
# Set up deep output
if self.deep_out:
self.Wd_e_out = add_to_params(self.params, theano.shared(value=NormalInit(self.rng, self.rankdim, out_target_dim), name='Wd_e_out'))
self.bd_e_out = add_to_params(self.params, theano.shared(value=np.zeros((out_target_dim,), dtype='float32'), name='bd_e_out'))
if self.decoder_bias_type != 'first':
self.Wd_s_out = add_to_params(self.params, theano.shared(value=NormalInit(self.rng, self.input_dim, out_target_dim), name='Wd_s_out'))
def build_output_layer(self, hs, xd, hd):
pre_activ = T.dot(hd, self.Wd_out)
if self.deep_out:
pre_activ += T.dot(xd, self.Wd_e_out) + self.bd_e_out
if self.decoder_bias_type != 'first':
pre_activ += T.dot(hs, self.Wd_s_out)
# ^ if bias all, bias the deep output
if self.maxout_out:
pre_activ = Maxout(2)(pre_activ)
return pre_activ
def build_next_probs_predictor(self, inp, x, prev_hd):
"""
Return output probabilities given prev_words x, hierarchical pass hs, and previous hd
hs should always be the same (and should not be updated).
"""
return self.build_decoder(inp, x, mode=UtteranceDecoder.BEAM_SEARCH, prev_hd=prev_hd)
def approx_embedder(self, x):
# Here we use the same embeddings learnt in the encoder.. !!!
return self.word_embedding_param[x]
def output_softmax(self, pre_activ):
# returns a (timestep, bs, idim) matrix (huge)
return SoftMax(T.dot(pre_activ, self.Wd_emb.T) + self.bd_out)
def output_nce(self, pre_activ, y, y_hat):
# returns a (timestep, bs, pos + neg) matrix (very small)
target_embedding = self.Wd_emb[y]
# ^ target embedding is (timestep x bs, rankdim)
noise_embedding = self.Wd_emb[y_hat]
# ^ noise embedding is (10, timestep x bs, rankdim)
# pre_activ is (timestep x bs x rankdim)
pos_scores = (target_embedding * pre_activ).sum(2)
neg_scores = (noise_embedding * pre_activ).sum(3)
pos_scores += self.bd_out[y]
neg_scores += self.bd_out[y_hat]
pos_noise = self.parent.t_noise_probs[y] * 10
neg_noise = self.parent.t_noise_probs[y_hat] * 10
pos_scores = - T.log(T.nnet.sigmoid(pos_scores - T.log(pos_noise)))
neg_scores = - T.log(1 - T.nnet.sigmoid(neg_scores - T.log(neg_noise))).sum(0)
return pos_scores + neg_scores
def build_decoder(self, decoder_inp, x, xmask=None, y=None, y_neg=None, mode=EVALUATION, prev_hd=None, step_num=None):
# Check parameter consistency
if mode == UtteranceDecoder.EVALUATION or mode == UtteranceDecoder.NCE:
assert not prev_hd
assert y
else:
assert not y
assert prev_hd
# if mode == EVALUATION
# xd = (timesteps, batch_size, qdim)
#
# if mode != EVALUATION
# xd = (n_samples, dim)
xd = self.approx_embedder(x)
if not xmask:
xmask = T.neq(x, self.eos_sym)
# we must zero out the </s> embedding
# i.e. the embedding x_{-1} is the 0 vector
# as well as hd_{-1} which will be reseted in the scan functions
if xd.ndim != 3:
assert mode != UtteranceDecoder.EVALUATION
xd = (xd.dimshuffle((1, 0)) * xmask).dimshuffle((1, 0))
else:
assert mode == UtteranceDecoder.EVALUATION or mode == UtteranceDecoder.NCE
xd = (xd.dimshuffle((2,0,1)) * xmask).dimshuffle((1,2,0))
# Run the decoder
if mode == UtteranceDecoder.EVALUATION or mode == UtteranceDecoder.NCE:
hd_init = T.alloc(np.float32(0), x.shape[1], self.qdim)
else:
hd_init = prev_hd
if self.sent_step_type == "gated":
f_dec = self.gated_step
o_dec_info = [hd_init, None, None, None]
if self.decoder_bias_type == "selective":
o_dec_info += [None, None]
else:
f_dec = self.plain_step
o_dec_info = [hd_init]
if self.decoder_bias_type == "selective":
o_dec_info += [None, None]
# If the mode of the decoder is EVALUATION
# then we evaluate by default all the sentence
# xd - i.e. xd.ndim == 3, xd = (timesteps, batch_size, qdim)
if mode == UtteranceDecoder.EVALUATION or mode == UtteranceDecoder.NCE:
_res, _ = theano.scan(f_dec,
sequences=[xd, xmask, decoder_inp],\
outputs_info=o_dec_info)
# else we evaluate only one step of the recurrence using the
# previous hidden states and the previous computed hierarchical
# states.
else:
_res = f_dec(xd, xmask, decoder_inp, prev_hd)
if isinstance(_res, list) or isinstance(_res, tuple):
hd = _res[0]
else:
hd = _res
# if we are using selective bias, we should update our decoder_inp
# to the step-selective decoder_inp
step_decoder_inp = decoder_inp
if self.decoder_bias_type == "selective":
step_decoder_inp = _res[1]
pre_activ = self.build_output_layer(step_decoder_inp, xd, hd)
# EVALUATION : Return target_probs + all the predicted ranks
# target_probs.ndim == 3
if mode == UtteranceDecoder.EVALUATION:
outputs = self.output_softmax(pre_activ)
target_probs = GrabProbs(outputs, y)
return target_probs, hd, _res, outputs
elif mode == UtteranceDecoder.NCE:
return self.output_nce(pre_activ, y, y_neg), hd
# BEAM_SEARCH : Return output (the softmax layer) + the new hidden states
elif mode == UtteranceDecoder.BEAM_SEARCH:
return self.output_softmax(pre_activ), hd
# SAMPLING : Return a vector of n_sample from the output layer
# + log probabilities + the new hidden states
elif mode == UtteranceDecoder.SAMPLING:
outputs = self.output_softmax(pre_activ)
if outputs.ndim == 1:
outputs = outputs.dimshuffle('x', 0)
sample = self.trng.multinomial(pvals=outputs, dtype='int64').argmax(axis=-1)
if outputs.ndim == 1:
sample = sample[0]
log_prob = -T.log(T.diag(outputs.T[sample]))
return sample, log_prob, hd
def gated_step(self, xd_t, m_t, decoder_inp_t, hd_tm1):
if m_t.ndim >= 1:
m_t = m_t.dimshuffle(0, 'x')
hd_tm1 = (m_t) * hd_tm1 + (1 - m_t) * T.tanh(T.dot(decoder_inp_t, self.Wd_s_0) + self.bd_s_0)
# ^ iff x_{t - 1} = </s> (m_t = 0) then x_{t - 1} = 0
# and hd_{t - 1} = tanh(W_s_0 decoder_inp_t + bd_s_0) else hd_{t - 1} is left unchanged (m_t = 1)
# In the 'selective' decoder bias type each hidden state of the decoder
# RNN receives the decoder_inp_t modified by the selective bias -> decoder_inpr_t
if self.decoder_bias_type == 'selective':
rd_sel_t = T.nnet.sigmoid(T.dot(xd_t, self.Wd_sel_e) + T.dot(hd_tm1, self.Wd_sel_h) + T.dot(decoder_inp_t, self.Wd_sel_s) + self.bd_sel)
decoder_inpr_t = rd_sel_t * decoder_inp_t
rd_t = T.nnet.sigmoid(T.dot(xd_t, self.Wd_in_r) + T.dot(hd_tm1, self.Wd_hh_r) + self.bd_r)
zd_t = T.nnet.sigmoid(T.dot(xd_t, self.Wd_in_z) + T.dot(hd_tm1, self.Wd_hh_z) + self.bd_z)
hd_tilde = self.sent_rec_activation(T.dot(xd_t, self.Wd_in) \
+ T.dot(rd_t * hd_tm1, self.Wd_hh) \
+ T.dot(decoder_inpr_t, self.Wd_s_q) \
+ self.bd_hh)
hd_t = (np.float32(1.) - zd_t) * hd_tm1 + zd_t * hd_tilde
output = (hd_t, decoder_inpr_t, rd_sel_t, rd_t, zd_t, hd_tilde)
# In the 'all' decoder bias type each hidden state of the decoder
# RNN receives the decoder_inp_t vector as bias without modification
elif self.decoder_bias_type == 'all':
rd_t = T.nnet.sigmoid(T.dot(xd_t, self.Wd_in_r) + T.dot(hd_tm1, self.Wd_hh_r) + T.dot(decoder_inp_t, self.Wd_s_r) + self.bd_r)
zd_t = T.nnet.sigmoid(T.dot(xd_t, self.Wd_in_z) + T.dot(hd_tm1, self.Wd_hh_z) + T.dot(decoder_inp_t, self.Wd_s_z) + self.bd_z)
hd_tilde = self.sent_rec_activation(T.dot(xd_t, self.Wd_in) \
+ T.dot(rd_t * hd_tm1, self.Wd_hh) \
+ T.dot(decoder_inp_t, self.Wd_s_q) \
+ self.bd_hh)
hd_t = (np.float32(1.) - zd_t) * hd_tm1 + zd_t * hd_tilde
output = (hd_t, rd_t, zd_t, hd_tilde)
else:
# Do not bias all the decoder (force to store very useful information in the first state)
rd_t = T.nnet.sigmoid(T.dot(xd_t, self.Wd_in_r) + T.dot(hd_tm1, self.Wd_hh_r) + self.bd_r)
zd_t = T.nnet.sigmoid(T.dot(xd_t, self.Wd_in_z) + T.dot(hd_tm1, self.Wd_hh_z) + self.bd_z)
hd_tilde = self.sent_rec_activation(T.dot(xd_t, self.Wd_in) \
+ T.dot(rd_t * hd_tm1, self.Wd_hh) \
+ self.bd_hh)
hd_t = (np.float32(1.) - zd_t) * hd_tm1 + zd_t * hd_tilde
output = (hd_t, rd_t, zd_t, hd_tilde)
return output
def plain_step(self, xd_t, m_t, decoder_inp_t, hd_tm1):
if m_t.ndim >= 1:
m_t = m_t.dimshuffle(0, 'x')
# We already assume that xd are zeroed out
hd_tm1 = (m_t) * hd_tm1 + (1-m_t) * T.tanh(T.dot(decoder_inp_t, self.Wd_s_0) + self.bd_s_0)
# ^ iff x_{t - 1} = </s> (m_t = 0) then x_{t-1} = 0
# and hd_{t - 1} = 0 else hd_{t - 1} is left unchanged (m_t = 1)
if self.decoder_bias_type == 'first':
# Do not bias all the decoder (force to store very useful information in the first state)
hd_t = self.sent_rec_activation( T.dot(xd_t, self.Wd_in) \
+ T.dot(hd_tm1, self.Wd_hh) \
+ self.bd_hh )
output = (hd_t,)
elif self.decoder_bias_type == 'all':
hd_t = self.sent_rec_activation( T.dot(xd_t, self.Wd_in) \
+ T.dot(hd_tm1, self.Wd_hh) \
+ T.dot(decoder_inp_t, self.Wd_s_q) \
+ self.bd_hh )
output = (hd_t,)
elif self.decoder_bias_type == 'selective':
rd_sel_t = T.nnet.sigmoid(T.dot(xd_t, self.Wd_sel_e) + T.dot(hd_tm1, self.Wd_sel_h) + T.dot(decoder_inp_t, self.Wd_sel_s) + self.bd_sel)
decoder_inpr_t = rd_sel_t * decoder_inp_t
hd_tilde = self.sent_rec_activation( T.dot(xd_t, self.Wd_in) \
+ T.dot(hd_tm1, self.Wd_hh) \
+ T.dot(decoder_inpr_t, self.Wd_s_q) \
+ self.bd_hh )
output = (hd_t, decoder_inpr_t, rd_sel_t)
return output
class DialogEncoderDecoder(Model):
def indices_to_words(self, seq, exclude_start_end=True):
"""
Converts a list of words to a list
of word ids. Use unk_sym if a word is not
known.
"""
def convert():
for word_index in seq:
if word_index > len(self.idx_to_str):
raise ValueError('Word index is too large for the model vocabulary!')
if not exclude_start_end or (word_index != self.eos_sym and word_index != self.sos_sym):
yield self.idx_to_str[word_index]
return list(convert())
def words_to_indices(self, seq):
"""
Converts a list of words to a list
of word ids. Use unk_sym if a word is not
known.
"""
return [self.str_to_idx.get(word, self.unk_sym) for word in seq]
def compute_updates(self, training_cost, params):
updates = []
grads = T.grad(training_cost, params)
grads = OrderedDict(zip(params, grads))
# Clip stuff
c = numpy.float32(self.cutoff)
clip_grads = []
norm_gs = T.sqrt(sum(T.sum(g ** 2) for p, g in grads.items()))
normalization = T.switch(T.ge(norm_gs, c), c / norm_gs, np.float32(1.))
notfinite = T.or_(T.isnan(norm_gs), T.isinf(norm_gs))
for p, g in grads.items():
clip_grads.append((p, T.switch(notfinite, numpy.float32(.1) * p, g * normalization)))
grads = OrderedDict(clip_grads)
if self.initialize_from_pretrained_word_embeddings and self.fix_pretrained_word_embeddings:
# Keep pretrained word embeddings fixed
logger.debug("Will use mask to fix pretrained word embeddings")
grads[self.W_emb] = grads[self.W_emb] * self.W_emb_pretrained_mask
else:
logger.debug("Will train all word embeddings")
if self.updater == 'adagrad':
updates = Adagrad(grads, self.lr)
elif self.updater == 'sgd':
raise Exception("Sgd not implemented!")
elif self.updater == 'adadelta':
updates = Adadelta(grads)
elif self.updater == 'rmsprop':
updates = RMSProp(grads, self.lr)
elif self.updater == 'adam':
updates = Adam(grads)
else:
raise Exception("Updater not understood!")
if self.bootstrap_from_semantic_information:
updates.append((self.semantic_information_weight, T.maximum(self.semantic_information_weight - self.semantic_information_decrease_rate, 0)))
return updates
def build_train_function(self):
if not hasattr(self, 'train_fn'):
# Compile functions
if self.bootstrap_from_semantic_information:
logger.debug("Building train function, which will return acc_cost equal to bootstrap cost plus NLL.")
else:
logger.debug("Building train function")
self.train_fn = theano.function(inputs=[self.x_data, self.x_data_reversed, self.x_max_length, self.x_cost_mask, self.x_semantic_targets],
outputs=self.training_cost,
updates=self.updates,
on_unused_input='warn',
name="train_fn")
return self.train_fn
def build_nce_function(self):
if not hasattr(self, 'train_fn'):
# Compile functions
if self.bootstrap_from_semantic_information:
logger.debug("Building NCE train function, which will return acc_cost equal to bootstrap cost plus NLL.")
else:
logger.debug("Building NCE train function")
self.nce_fn = theano.function(inputs=[self.x_data, self.x_data_reversed, self.y_neg, self.x_max_length, self.x_cost_mask, self.x_semantic_targets],
outputs=self.training_cost,
updates=self.updates,
on_unused_input='warn',
name="train_fn")
return self.nce_fn
def build_eval_function(self):
if not hasattr(self, 'eval_fn'):
# Compile functions
logger.debug("Building evaluation function")
self.eval_fn = theano.function(inputs=[self.x_data, self.x_data_reversed, self.x_max_length, self.x_cost_mask, self.x_semantic_targets],
outputs=[self.softmax_cost_acc, self.softmax_cost],
on_unused_input='warn', name="eval_fn")
return self.eval_fn
def build_eval_misclassification_function(self):
if not hasattr(self, 'eval_misclass_fn'):
# Compile functions
logger.debug("Building misclassification evaluation function")
self.eval_misclass_fn = theano.function(inputs=[self.x_data, self.x_data_reversed, self.x_max_length, self.x_cost_mask, self.x_semantic_targets],
outputs=[self.training_misclassification_acc, self.training_misclassification], name="eval_misclass_fn", on_unused_input='ignore')
return self.eval_misclass_fn
def build_semantic_eval_function(self):
if not hasattr(self, 'sem_fn'):
# Compile functions
logger.debug("Building semantic evaluation function")
self.sem_fn = theano.function(inputs=[self.x_data, self.x_data_reversed, self.x_max_length, self.x_cost_mask, self.x_semantic_targets],
outputs=[self.semantic_cost, self.semantic_preds],
on_unused_input='warn',
name="sem_fn")
return self.sem_fn
def build_get_states_function(self):
if not hasattr(self, 'get_states_fn'):
# Compile functions
logger.debug("Building selective function")
outputs = [self.h, self.hs, self.hd] + [x for x in self.utterance_decoder_states]
self.get_states_fn = theano.function(inputs=[self.x_data, self.x_data_reversed, self.x_max_length, self.x_semantic_targets],
outputs=outputs, on_unused_input='warn',
name="get_states_fn")
return self.get_states_fn
def build_next_probs_function(self):
if not hasattr(self, 'next_probs_fn'):
outputs, hd = self.utterance_decoder.build_next_probs_predictor(self.beam_hs, self.beam_source, prev_hd=self.beam_hd)
self.next_probs_fn = theano.function(inputs=[self.beam_hs, self.beam_source, self.beam_hd],
outputs=[outputs, hd],
name="next_probs_fn")
return self.next_probs_fn
def build_encoder_function(self):
if not hasattr(self, 'encoder_fn'):
if self.bidirectional_utterance_encoder:
res_forward = self.utterance_encoder_forward.build_encoder(self.aug_x_data, return_L2_pooling = self.encode_with_l2_pooling)
res_backward = self.utterance_encoder_backward.build_encoder(self.aug_x_data_reversed, return_L2_pooling = self.encode_with_l2_pooling)
# The encoder h embedding is a concatenation of L2 pooling on the forward and backward encoder RNNs
if self.encode_with_l2_pooling:
h = T.concatenate([res_forward[1], res_backward[1]], axis=2)
else: # Each encoder gives a single output vector
h = T.concatenate([res_forward, res_backward], axis=2)
if self.add_semantic_information_to_utterance_decoder:
res_dialog_encoder = self.dialog_encoder.build_encoder(h, self.aug_x_data)
#hs = T.concatenate([res_dialog_encoder, T.cast(self.x_semantic_targets, 'float32').dimshuffle(0, 1, 'x')], axis=2)
res_semantic = T.cast(self.x_semantic_targets, 'float32').dimshuffle('x', 0, 1)
#T.shape(res_dialog_encoder.shape)[0]
#.dimshuffle('x', 0, 1), reps=res_dialog_encoder.shape[0])
hs = T.concatenate([res_dialog_encoder, res_semantic.repeat(repeats=res_dialog_encoder.shape[0], axis=0)], axis=2)
else:
hs = self.dialog_encoder.build_encoder(h, self.aug_x_data)
if self.direct_connection_between_encoders_and_decoder:
hs_dummy = self.dialog_dummy_encoder.build_encoder(h, self.aug_x_data)
hs_and_hs_dummy = T.concatenate([hs, h], axis=2)
self.encoder_fn = theano.function(inputs=[self.x_data, self.x_data_reversed, self.x_max_length, self.x_semantic_targets],
outputs=[h, hs_and_hs_dummy], on_unused_input='warn', name="encoder_fn")
else:
self.encoder_fn = theano.function(inputs=[self.x_data, self.x_data_reversed, self.x_max_length, self.x_semantic_targets],
outputs=[h, hs], on_unused_input='warn', name="encoder_fn")
elif self.dcgm_encoder:
h = self.dcgm_encoder.build_encoder(self.aug_x_data)
hs = self.dialog_dummy_encoder.build_encoder(h, self.aug_x_data)
self.encoder_fn = theano.function(inputs=[self.x_data, self.x_data_reversed, self.x_max_length, self.x_semantic_targets], outputs=[h, hs], \
on_unused_input='warn', name="encoder_fn")
else:
if self.encode_with_l2_pooling: