-
Notifications
You must be signed in to change notification settings - Fork 15
/
model_v2.py
executable file
·1567 lines (1227 loc) · 53.4 KB
/
model_v2.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
# tensorflow model graph
import tensorflow as tf
from utils import flatten, reconstruct, Dataset, exp_mask
import numpy as np
import random, sys
VERY_NEGATIVE_NUMBER = -1e30
def get_model(config):
# implement a multi gpu model?
with tf.name_scope(config.modelname), tf.device("/gpu:0"):
model = Model(config, "model_%s"%config.modelname)
return model
from copy import deepcopy # for C[i].insert(Y[i])
# a flatten and reconstruct version of softmax
def softmax(logits, scope=None):
with tf.name_scope(scope or "softmax"): # noted here is name_scope not variable
flat_logits = flatten(logits, 1)
flat_out = tf.nn.softmax(flat_logits)
out = reconstruct(flat_out, logits, 1)
return out
# softmax selection?
# return target * softmax(logits)
# target: [ ..., J, d]
# logits: [ ..., J]
# so [N, M, dim] * [N, M] -> [N, dim], so [N, M] is the attention for each M
# return: [ ..., d] # so the target vector is attended with logits' softmax
# [N, M, JX, JQ, 2d] * [N, M, JX, JQ] (each context to query's mapping) -> [N, M, JX, 2d] # attened the JQ dimension
def softsel(target, logits, hard=False, hardK=None, scope=None):
with tf.variable_scope(scope or "softsel"): # there is no variable to be learn here
a = softmax(logits) # shape is the same
target_rank = len(target.get_shape().as_list())
# [N, M, JX, JQ, 2d] elem* [N, M, JX, JQ, 1]
return tf.reduce_sum(tf.expand_dims(a, -1)*target, target_rank-2) # second last dim
# x -> [Num, JX, W, embedding dim] # conv2d requires an input of 4d [batch, in_height, in_width, in_channels]
def conv1d(x, filter_size, height, keep_prob, is_train=None, wd=None, scope=None):
with tf.variable_scope(scope or "conv1d"):
num_channels = x.get_shape()[-1] # embedding dim[8]
filter_var = tf.get_variable("filter", shape=[1, height, num_channels, filter_size], dtype="float")
bias = tf.get_variable('bias', shape=[filter_size], dtype='float')
strides = [1, 1, 1, 1]
# add dropout to input
d = tf.nn.dropout(x, keep_prob=keep_prob)
outd = tf.cond(is_train, lambda:d, lambda:x)
#conv
xc = tf.nn.relu(tf.nn.conv2d(outd, filter_var, strides, padding='VALID')+bias)
# simple max pooling?
out = tf.reduce_max(xc, 2) # [-1, JX, num_channel]
if wd is not None:
add_wd(wd)
return out
# fully-connected layer
# simple linear layer, without activatation # remember to add it
# [N, M, JX, JQ, 2d] => x[N*M*JX*JQ, 2d] * W[2d, output_size] ->
def linear(x, output_size, scope, add_tanh=False, wd=None):
with tf.variable_scope(scope):
# since the input here is not two rank, we flat the input while keeping the last dims
keep = 1
#print x.get_shape().as_list()
flat_x = flatten(x, keep) # keeping the last one dim # [N, M, JX, JQ, 2d] => [N*M*JX*JQ, 2d]
#print flat_x.get_shape() # (?, 200) # wd+cwd
bias_start = 0.0
if not (type(output_size) == type(1)): # need to be get_shape()[k].value
output_size = output_size.value
#print [flat_x.get_shape()[-1], output_size]
W = tf.get_variable("W", dtype="float", initializer=tf.truncated_normal([flat_x.get_shape()[-1].value, output_size], stddev=0.1))
bias = tf.get_variable("b", dtype="float", initializer=tf.constant(bias_start, shape=[output_size]))
flat_out = tf.matmul(flat_x, W)+bias
if add_tanh:
flat_out = tf.tanh(flat_out, name="tanh")
if wd is not None:
add_wd(wd)
out = reconstruct(flat_out, x, keep)
return out
def batch_norm(x, is_train=True, epsilon=1e-5, decay=0.9, scope=None):
scope = scope or "batch_norm"
# what about tf.nn.batch_normalization
return tf.contrib.layers.batch_norm(x, decay=decay, updates_collections=None, epsilon=epsilon, scale=True, is_training=is_train, scope=scope)
# hinfo * att(hinfo, hq) -> attended_hinfo / [hinfo;attened_hinfo]
# hq -> [N, JQ, w]
# hinfo -> [N, M, J*, w] / [N, M, J*, JX, w]
# h_info_mask -> [N, M, J*]
# hinfo[N, M, J1, w]/[N, M, J1, J2, w] -> h_a[N, w]
# input: hinfo [N, ..., 2d], hq [N, JQ, 2d] -> output: [N, 2d]
# simiMatrix: what type of similarity matrix we use for the linear transform
# 1: A, B , A*B
# 2:(A-B)^2, A*B
# 3: A, B, (A-B)^2, A*B
# attention(hq, tf.expand_dims(g1_all, 1), self.q_mask, add_tanh=config.add_tanh, simiMatrix=config.simiMatrix, wd=config.wd, bidirect=config.use_bidirection, scope="question_att")
# hq -> [N, JQ, 2d]
# g1_all -> [N, 1, 2d]
# gp -> [N, 2d]
def attention(hinfo, hq, hinfo_mask=None, hq_mask=None, simiMatrix=1, wd=None, add_tanh=False, bidirect=False, scope=None):
with tf.variable_scope(scope or "attention_2vector"):
N = hinfo.get_shape().as_list()[0]
w = hinfo.get_shape().as_list()[-1]
M = tf.shape(hinfo)[1]
JQ = tf.shape(hq)[1]
#hinfo_rank = tf.rank(hinfo)
hinfo = tf.reshape(hinfo, [N, -1, w]) # M*J / M*J*JX # [N, JQ, 2d]
#print hinfo.get_shape().as_list(), hq.get_shape().as_list()
if hinfo_mask is not None:
hinfo_mask = tf.reshape(hinfo_mask, [N, -1]) # [N, JQ]
#vector length for hinfo
V = tf.shape(hinfo)[1]
# so hinfo -> [N, V, w], hinfo_mask -> [N, V]
# change two matrix to be the same [N, V, JQ, 2d]
h_aug = tf.tile(tf.expand_dims(hinfo, 2), [1, 1, JQ, 1])
q_aug = tf.tile(tf.expand_dims(hq, 1), [1, V, 1, 1])
if (hinfo_mask is not None) and (hq_mask is not None):
# change the mask too
h_mask_aug = tf.tile(tf.expand_dims(hinfo_mask, 2), [1, 1, JQ])
q_mask_aug = tf.tile(tf.expand_dims(hq_mask, 1), [1, V, 1])
mask = h_mask_aug & q_mask_aug # [N, V, JQ]
# get [N, V, JQ]
if simiMatrix == 1:
a_logits = linear(tf.concat([h_aug, q_aug, h_aug*q_aug], 3), output_size=1, add_tanh=add_tanh, scope="att_logits")
a_logits = tf.squeeze(a_logits, 3)
elif simiMatrix == 2:
a_logits = linear(tf.concat([h_aug*q_aug, (h_aug-q_aug)*(h_aug-q_aug)], 3), add_tanh=add_tanh, output_size=1, scope="att_logits")
a_logits = tf.squeeze(a_logits, 3)
elif simiMatrix == 3:
a_logits = linear(tf.concat([h_aug, q_aug, (h_aug-q_aug)*(h_aug-q_aug), h_aug*q_aug], 3), output_size=1, add_tanh=add_tanh, scope="att_logits")
a_logits = tf.squeeze(a_logits, 3)
elif simiMatrix == 4:
# cosine simi, [N, V, JQ, 2d] -> [N, V, JQ]
h_aug_norm = tf.nn.l2_normalize(h_aug, -1)
q_aug_norm = tf.nn.l2_normalize(q_aug, -1)
a_logits = tf.reduce_sum(tf.multiply(h_aug_norm, q_aug_norm), 3)
else:
print "similarity matrix not implemented"
sys.exit()
# apply mask
if (hinfo_mask is not None) and (hq_mask is not None):
a_logits = exp_mask(a_logits, mask)
# hinfo -> [N, V, w], * max([N, V, JQ]) [N, V] [so each info "word" 's max prob to the whole question]
# h_a -> [N, w]
#h_a = softsel(hinfo, tf.reduce_max(a_logits, 2), hard=True, hardK=3)
h_a = softsel(hinfo, tf.reduce_max(a_logits, 2), hard=False)
# add a reversed-directional vector here
if bidirect:
# q [N, JQ, w] -> q_aug : [N, V, JQ, w] * [N, V, JQ] -> [N, V, w]
q_a = softsel(q_aug, a_logits, hard=False) # each V attended with query
# here we simply average them
q_a = tf.reduce_mean(q_a, 1) # [N, w]
# concat two direction attended vector
h_a = tf.concat([h_a, q_a], 1) #[N, 2w]
# need output to be [N, 2w]
#h_a = linear(h_a, output_size=w, scope="combine_bidirect")
if wd is not None:
add_wd(wd)
return h_a, a_logits
# hinfo -> [N, K, M, JMAX, 2d], K is the modality
# hq -> [N, JQ, w]
# h_info_mask -> [N, K, M, JMAX]
# simiMatrix: what type of similarity matrix we use for the linear transform
# 1: A, B , A*B
# 2:(A-B)^2, A*B
# 3: A, B, (A-B)^2, A*B
def attention_3d(hinfo, hq, hinfo_mask=None, hq_mask=None, simiMatrix=1, wd=None, add_tanh=False, time_warp_att=False, C=None, bidirect=False, scope=None):
with tf.variable_scope(scope or "attention_2vector"):
# static shape that we now before runtime
N = hinfo.get_shape().as_list()[0]
w = hinfo.get_shape().as_list()[-1]
K = hinfo.get_shape().as_list()[1]
M = tf.shape(hinfo)[2]
JQ = tf.shape(hq)[1]
#hinfo_rank = tf.rank(hinfo)
hinfo = tf.reshape(hinfo, [N, K, -1, w]) # M*JMAX
#print hinfo.get_shape().as_list(), hq.get_shape().as_list()
if hinfo_mask is not None:
hinfo_mask = tf.reshape(hinfo_mask, [N, K, -1])
#vector length for hinfo
T = tf.shape(hinfo)[2]
# so hinfo -> [N, K, T, w], hinfo_mask -> [N, K, T]
# change two matrix to be the same
h_aug = tf.tile(tf.expand_dims(hinfo, 3), [1, 1, 1, JQ, 1])
q_aug = tf.tile(tf.expand_dims(tf.expand_dims(hq, 1), 1), [1, K, T, 1, 1])
if (hinfo_mask is not None) and (hq_mask is not None):
# change the mask too
h_mask_aug = tf.tile(tf.expand_dims(hinfo_mask, 3), [1, 1, 1, JQ])
q_mask_aug = tf.tile(tf.expand_dims(tf.expand_dims(hq_mask, 1), 1), [1, K, T, 1])
mask = h_mask_aug & q_mask_aug # [N, K, T, JQ]
# [N, K, T, JQ, 2d] -> [N, K, T, JQ, 1]
if simiMatrix == 1:
a_logits = linear(tf.concat([h_aug, q_aug, h_aug*q_aug], 4), output_size=1, add_tanh=add_tanh, scope="att_logits")
a_logits = tf.squeeze(a_logits, 4)
elif simiMatrix == 2:
a_logits = linear(tf.concat([h_aug*q_aug, (h_aug-q_aug)*(h_aug-q_aug)], 4), output_size=1, add_tanh=add_tanh, scope="att_logits")
a_logits = tf.squeeze(a_logits, 4)
elif simiMatrix == 3:
a_logits = linear(tf.concat([h_aug, q_aug, (h_aug-q_aug)*(h_aug-q_aug), h_aug*q_aug], 4), output_size=1, add_tanh=add_tanh, scope="att_logits")
a_logits = tf.squeeze(a_logits, 4)
elif simiMatrix == 4:
# cosine simi, [N, K, T, JQ, 2d] -> [N, K, T, JQ]
h_aug_norm = tf.nn.l2_normalize(h_aug, -1)
q_aug_norm = tf.nn.l2_normalize(q_aug, -1)
a_logits = tf.reduce_sum(tf.multiply(h_aug_norm, q_aug_norm), 4)
else:
print "similarity matrix not implemented"
sys.exit()
# [N, K, T, JQ]
# apply mask
if (hinfo_mask is not None) and (hq_mask is not None):
a_logits = exp_mask(a_logits, mask)
# attend on T (timestep), then K (modality)
# hinfo -> [N, K, T, w] -> [N, K, w] -> [N, w] h_a
a_logits_maxed = tf.reduce_max(a_logits, 3) # [N, K, T]
if time_warp_att:
T = tf.shape(C)[-1]
# time_warp with matrix C
# C [N, T, T]
a_temp = tf.tile(tf.expand_dims(a_logits_maxed, 3), [1, 1, 1, T]) #[N, K, T, T]
C_ex = tf.tile(tf.expand_dims(C, 1), [1, K, 1, 1])# [N, K, T, T]
a_logits_maxed = tf.reduce_sum(a_temp*C_ex, -1) #[N, K, T]
h_a = softsel(softsel(hinfo, a_logits_maxed), tf.reduce_max(a_logits, [3, 2]))#, hard=False, hardK=3)
# add a reversed-directional vector here
if bidirect:
# q [N, JQ, w] -> q_aug : [N, V, JQ, w] * [N, V, JQ] -> [N, V, w]
q_a = softsel(q_aug, a_logits, hard=False) # each V attended with query
# here we simply average them
q_a = tf.reduce_mean(q_a, 1) # [N, w]
# concat two direction attended vector
h_a = tf.concat([h_a, q_a], 1) #[N, 2w]
# need output to be [N, 2w]
#h_a = linear(h_a, output_size=w, scope="combine_bidirect")
if wd is not None:
add_wd(wd)
return h_a, a_logits
# time correlation times the indication function, given [N, T, T] get a new [N, T, T]
def time_indication_func(C, warp_type=1, scope=None):
with tf.variable_scope(scope or "time_warp"):
if warp_type==1: # all Time
return C, None
else:
T = tf.shape(C)[-1]
N = C.get_shape().as_list()[0]
one = tf.diag(tf.ones([T], dtype=tf.float32)) # [T, T]
one = tf.tile(tf.expand_dims(one, 0), [N, 1, 1])
if warp_type == 2: # current time??
#one = tf.eye(T, dtype=tf.float32, batch_shape=N)
return one*C, None
elif warp_type == 3: # past
# need T to be constant
#a = np.zeros((T, T), dtype="float")
#a[np.tril_indices(T, 0)] = 1.0
#past = tf.constant(a)
past = tf.matrix_band_part(tf.ones([T, T], dtype=tf.float32), -1, 0) # lower triangular
past = tf.tile(tf.expand_dims(past, 0), [N, 1, 1])
return past*C, None
elif warp_type == 4: # future
#a = np.zeros((T, T), dtype="float")
#a[np.triu_indices(T, 0)] = 1.0
#future = tf.constant(a)
future = tf.matrix_band_part(tf.ones([T, T], dtype=tf.float32), 0, -1) # upper triangular
future = tf.tile(tf.expand_dims(future, 0), [N, 1, 1])
return future*C, None
elif warp_type == 5: # past-future with trainable window
window_t_init=3
window_t = tf.get_variable('time_warp_window_t', shape=[], dtype=tf.float32, initializer=tf.constant_initializer(window_t_init), trainable=True)
window_t_int = tf.cast(tf.ceil(window_t), tf.int64)
windowed_C = tf.matrix_band_part(tf.ones([T, T], dtype=tf.float32), window_t_int, window_t_int)
windowed_C = tf.tile(tf.expand_dims(windowed_C, 0), [N, 1, 1])
return windowed_C*C, window_t
else:
raise Exception("time warping type not implemented")
# add current scope's variable's l2 loss to loss collection
def add_wd(wd, scope=None):
if wd != 0.0:
scope = scope or tf.get_variable_scope().name
vars_ = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=scope)
with tf.variable_scope("weight_decay"):
for var in vars_:
weight_decay = tf.multiply(tf.nn.l2_loss(var), wd, name="%s/wd"%(var.op.name))
tf.add_to_collection("losses", weight_decay)
def get_initializer(matrix):
def _initializer(shape, dtype=None, partition_info=None, **kwargs): return matrix
return _initializer
class Model():
def __init__(self, config, scope):
self.scope = scope
self.config = config
# a step var to keep track of current training process
self.global_step = tf.get_variable('global_step', shape=[], dtype='int32', initializer=tf.constant_initializer(0), trainable=False) # a counter
# get all the dimension here
N = self.N = config.batch_size
VW = self.VW = config.word_vocab_size
VC = self.VC = config.char_vocab_size
W = self.W = config.max_word_size
# embedding dim
self.cd, self.wd, self.cwd = config.char_emb_size, config.word_emb_size, config.char_out_size
# image dimension
self.idim = config.image_feat_dim
self.num_choice = config.num_choice
# these could be used for visualization
self.C = tf.constant(-1) # the time correlation matrix
self.C_win = tf.constant(-1)
self.att_logits = tf.constant(-1) # the 3d attention logits
self.q_att_logits = tf.constant(-1) # the question attention logits if there is
self.JXP = tf.constant(-1)
# for version 1 1-dim attention
self.hat_len = tf.constant(-1)
self.had_len = tf.constant(-1)
self.hwhen_len = tf.constant(-1)
self.hwhere_len = tf.constant(-1)
self.hpis_len = tf.constant(-1)
self.hpts_len = tf.constant(-1)
#for showing the h vectors
self.warp_h = tf.constant(-1)
# step limits
# M -> album max num
# -----JX -> title max words (album title, photo title)
# JXA -> album title max words
# JXP -> photo title max words
# JD -> album description max word
# JT -> album when max word
# JG -> album where max word
# JI -> album max photo
# JA -> max answer (choice) length
# JQ -> max question length
# all the inputs
# album title
# [N, M, JXA]
self.at = tf.placeholder('int32', [N, None, None], name="at")
self.at_c = tf.placeholder("int32", [N, None, None, W], name="at_c")
self.at_mask = tf.placeholder("bool", [N, None, None], name="at_mask") # to get the sequence length
# album description
# [N, M, JD]
self.ad = tf.placeholder('int32', [N, None, None], name="ad")
self.ad_c = tf.placeholder("int32", [N, None, None, W], name="ad_c")
self.ad_mask = tf.placeholder("bool", [N, None, None], name="ad_mask")
# album when, where
# [N, M, JT/JG]
self.when = tf.placeholder("int32", [N, None, None], name="when")
self.when_c = tf.placeholder("int32", [N, None, None, W], name="when_c")
self.when_mask = tf.placeholder("bool", [N, None, None], name="when_mask")
self.where = tf.placeholder("int32", [N, None, None], name="where")
self.where_c = tf.placeholder("int32", [N, None, None, W], name="where_c")
self.where_mask = tf.placeholder("bool", [N, None, None], name="where_mask")
# photo titles
# [N, M, JI, JXP]
self.pts = tf.placeholder('int32', [N, None, None, None], name="pts")
self.pts_c = tf.placeholder("int32", [N, None, None, None, W], name="pts_c")
self.pts_mask = tf.placeholder("bool", [N, None, None, None], name="pts_mask")
# for vis
self.JXP = tf.shape(self.pts)[3]
# photo
# [N, M, JI] # each is a photo index
self.pis = tf.placeholder('int32', [N, None, None], name="pis")
self.pis_mask = tf.placeholder("bool", [N, None, None], name="pis_mask")
# question
self.q = tf.placeholder('int32', [N, None], name="q")
self.q_c = tf.placeholder('int32', [N, None, W], name='q_c')
self.q_mask = tf.placeholder("bool", [N, None], name="q_mask")
# answer + choice words
# [N, 4, JA]
self.choices = tf.placeholder("int32", [N, self.num_choice, None], name="choices")
self.choices_c = tf.placeholder("int32", [N, self.num_choice, None, W], name="choices_c")
self.choices_mask = tf.placeholder("bool", [N, self.num_choice, None], name="choices_mask")
# 4 choice classification
self.y = tf.placeholder('bool', [N, self.num_choice], name='y')
# feed in the pretrain word vectors for all batch
self.existing_emb_mat = tf.placeholder('float', [None, config.word_emb_size], name="pre_emb_mat")
# feed in the image feature for this batch
# [photoNumForThisBatch, image_dim]
self.image_emb_mat = tf.placeholder("float", [None, config.image_feat_dim], name="image_emb_mat")
# used for drop out switch
self.is_train = tf.placeholder('bool', [], name='is_train')
# forward output
# the following will be added in build_forward and build_loss()
self.logits = None
self.yp = None # prob
self.loss = None
self.build_forward()
self.build_loss()
self.summary = tf.summary.merge_all() # for visualize and stuff? # not used now
def build_forward(self):
config = self.config
VW = self.VW
VC = self.VC
W = self.W
N = self.N
# dynamic decide some step, for sequence length
M = tf.shape(self.pis)[1] # photo num
JXA = tf.shape(self.at)[2] # for album title, photo title
JD = tf.shape(self.ad)[2] # description length
JT = tf.shape(self.when)[2]
JG = tf.shape(self.where)[2]
JI = tf.shape(self.pis)[2] # used for photo_title, photo
JXP = tf.shape(self.pts)[3]
JQ = tf.shape(self.q)[1]
JA = tf.shape(self.choices)[2]
# embeding size
cdim, wdim, cwdim = self.cd, self.wd, self.cwd #cwd: char -> word output dimension
# image feature dim
idim = self.idim # image_feat dimension
# all input:
# at, ad, when, where,
# pts, pis
# q, choices
# embedding
with tf.variable_scope('emb'):
# char stuff
if config.use_char:
#with tf.variable_scope("char"):
# [char_vocab_size, char_emb_dim]
with tf.variable_scope("var"): #, tf.device("/cpu:0"):
char_emb = tf.get_variable("char_emb", shape=[VC, cdim], dtype="float")
# the embedding for each of character
# [N, M, JXA, W]
Aat_c = tf.nn.embedding_lookup(char_emb, self.at_c)
# [N, M, JD, W]
Aad_c = tf.nn.embedding_lookup(char_emb, self.ad_c)
# [N, M, JT, W]
Awhen_c = tf.nn.embedding_lookup(char_emb, self.when_c)
# [N, M, JG, W]
Awhere_c = tf.nn.embedding_lookup(char_emb, self.where_c)
# [N, M, JI, JXP, W] -> [N, M, JI, JXP, W, cdim]
Apts_c = tf.nn.embedding_lookup(char_emb, self.pts_c)
# [N, JQ, W]
Aq_c = tf.nn.embedding_lookup(char_emb, self.q_c)
Achoices_c = tf.nn.embedding_lookup(char_emb, self.choices_c)
# flatten for conv2d input like images
Aat_c = tf.reshape(Aat_c, [-1, JXA, W, cdim])
Aad_c = tf.reshape(Aad_c, [-1, JD, W, cdim])
Awhen_c = tf.reshape(Awhen_c, [-1, JT, W, cdim])
Awhere_c = tf.reshape(Awhere_c, [-1, JG, W, cdim])
# [N*M*JI, JXP, W, cdim]
Apts_c = tf.reshape(Apts_c, [-1, JXP, W, cdim])
Aq_c = tf.reshape(Aq_c, [-1, JQ, W, cdim])
# [N*4, ]
Achoices_c = tf.reshape(Achoices_c, [-1, JA, W, cdim])
#char CNN
filter_size = cwdim # output size for each word
filter_height = 5
with tf.variable_scope("conv"):
xat = conv1d(Aat_c, filter_size, filter_height, config.keep_prob, self.is_train, wd=config.wd, scope="conv1d")
tf.get_variable_scope().reuse_variables()
xad = conv1d(Aad_c, filter_size, filter_height, config.keep_prob, self.is_train, wd=config.wd, scope="conv1d")
xwhen = conv1d(Awhen_c, filter_size, filter_height, config.keep_prob, self.is_train, wd=config.wd, scope="conv1d")
xwhere = conv1d(Awhere_c, filter_size, filter_height, config.keep_prob, self.is_train, wd=config.wd, scope="conv1d")
xpts = conv1d(Apts_c, filter_size, filter_height, config.keep_prob, self.is_train, wd=config.wd, scope="conv1d")
qq = conv1d(Aq_c, filter_size, filter_height, config.keep_prob, self.is_train, wd=config.wd, scope="conv1d")
qchoices = conv1d(Achoices_c, filter_size, filter_height, config.keep_prob, self.is_train, wd=config.wd, scope="conv1d")
# reshape them back
xat = tf.reshape(xat, [-1, M, JXA, cwdim])
xad = tf.reshape(xad, [-1, M, JD, cwdim])
xwhen = tf.reshape(xwhen, [-1, M, JT, cwdim])
xwhere = tf.reshape(xwhere, [-1, M, JG, cwdim])
xpts = tf.reshape(xpts, [-1, M, JI, JXP, cwdim])
qq = tf.reshape(qq, [-1, JQ, cwdim])
# [N, num_choice, JA, cwdim]
qchoices = tf.reshape(qchoices, [-1, self.num_choice, JA, cwdim])
# word stuff
with tf.variable_scope('word'):
with tf.variable_scope("var"):
# get the word embedding for new words
if config.is_train:
# for new word
word_emb_mat = tf.get_variable("word_emb_mat", dtype="float", shape=[VW, wdim], initializer=get_initializer(config.emb_mat)) # it's just random initialized
else: # save time for loading the emb during test
word_emb_mat = tf.get_variable("word_emb_mat", dtype="float", shape=[VW, wdim])
# concat with pretrain vector
# so 0 - VW-1 index for new words, the rest for pretrain vector
# and the pretrain vector is fixed
word_emb_mat = tf.concat([word_emb_mat, self.existing_emb_mat], 0)
#[N, M, JXA] -> [N, M, JXA, wdim]
Aat = tf.nn.embedding_lookup(word_emb_mat, self.at)
Aad = tf.nn.embedding_lookup(word_emb_mat, self.ad)
Awhen = tf.nn.embedding_lookup(word_emb_mat, self.when)
Awhere = tf.nn.embedding_lookup(word_emb_mat, self.where)
Apts = tf.nn.embedding_lookup(word_emb_mat, self.pts)
Aq = tf.nn.embedding_lookup(word_emb_mat, self.q)
Achoices = tf.nn.embedding_lookup(word_emb_mat, self.choices)
# concat char and word
if config.use_char:
xat = tf.concat([xat, Aat], 3)
xad = tf.concat([xad, Aad], 3)
xwhen = tf.concat([xwhen, Awhen], 3)
xwhere = tf.concat([xwhere, Awhere], 3)
# [N, M, JI, JX, wdim+cwdim]
xpts = tf.concat([xpts, Apts], 4)
# [N, JQ, wdim+cwdim]
qq = tf.concat([qq, Aq], 2)
qchoices = tf.concat([qchoices, Achoices], 3)
else:
xat = Aat
xad = Aad
xwhen = Awhen
xwhere = Awhere
xpts = Apts
qq = Aq
qchoices = Achoices
# all the above last dim is the same [wdim+cwdim] or just [wdim]
# get the image feature
with tf.variable_scope("image"):
# [N, M, JI] -> [N, M, JI, idim]
xpis = tf.nn.embedding_lookup(self.image_emb_mat, self.pis)
# use image trans, then linearly transform it to lower dim
# TODO: CNN transform?
if config.use_image_trans:
with tf.variable_scope("image_transform"):
#[N, M, JI, idim] -> [N, M, JI, newdim]
xpis = linear(xpis, add_tanh=config.add_tanh, output_size=config.image_trans_dim, wd=config.wd, scope="image_trans_linear")
d = config.hidden_size
# LSTM / GRU?
cell_text = tf.nn.rnn_cell.BasicLSTMCell(d, state_is_tuple=True)
cell_img = tf.nn.rnn_cell.BasicLSTMCell(d, state_is_tuple=True)
#cell_text = tf.nn.rnn_cell.GRUCell(d)
#cell_img = tf.nn.rnn_cell.GRUCell(d)
# add dropout
keep_prob = tf.cond(self.is_train, lambda:tf.constant(config.keep_prob), lambda:tf.constant(1.0))
cell_text = tf.nn.rnn_cell.DropoutWrapper(cell_text, keep_prob)
cell_img = tf.nn.rnn_cell.DropoutWrapper(cell_img, keep_prob)
# it is important to think about which LSTM shared with which?
# sequence length for each
at_len = tf.reduce_sum(tf.cast(self.at_mask, "int32"), 2) # [N, M] # each album's title length
ad_len = tf.reduce_sum(tf.cast(self.ad_mask, "int32"), 2)
when_len = tf.reduce_sum(tf.cast(self.when_mask, "int32"), 2)
where_len = tf.reduce_sum(tf.cast(self.where_mask, "int32"), 2) # [N, M]
pis_len = tf.reduce_sum(tf.cast(self.pis_mask, "int32"), 2) #[N, M, JI] #[N, M]
pts_len = tf.reduce_sum(tf.cast(self.pts_mask, "int32"), 3) # [N, M, JI, JXP] -> [N, M, JI]
q_len = tf.reduce_sum(tf.cast(self.q_mask, "int32"), 1) # [N] # each question 's length
choices_len = tf.reduce_sum(tf.cast(self.choices_mask, "int32"), 2) # [N, 4]
# xat -> [N, M, JXA, wdim+cwdim]
# xad -> [N, M, JD, wdim+cwdim]
# xwhen/xwhere -> [N, M, JT/JG, wdim+cwdim]
# xpts -> [N, M, JI, JXP, wdim+cwdim]
# xpis -> [N, M, JI, idim]
# qq -> [N, JQ, wdim+cwdim]
# qchoices -> [N, 4, JA, wdim+cwdim]
# roll the sentence into lstm for context and question
# from [N, M, JI, JX] -> [N, M, 2d]
with tf.variable_scope("reader"):
with tf.variable_scope("text"):
(fw_hq, bw_hq), (fw_lq, bw_lq) = tf.nn.bidirectional_dynamic_rnn(cell_text, cell_text, qq, sequence_length=q_len, dtype="float", scope="utext")
# concat the fw and backward lstm output
hq = tf.concat([fw_hq, bw_hq], 2)
lq = tf.concat([fw_lq.h, bw_lq.h], 1) #LSTM CELL
#lq = tf.concat([fw_lq, bw_lq], 1) # GRU
tf.get_variable_scope().reuse_variables()
# flat all
# choices
flat_qchoices = flatten(qchoices, 2) # [N, 4, JA, dim] -> [N*4, JA, dim]
# album title
flat_xat = flatten(xat, 2) #[N, M, JXA, dim] -> [N*M, JXA, dim]
flat_xad = flatten(xad, 2)
flat_xwhen = flatten(xwhen, 2)
flat_xwhere = flatten(xwhere, 2)
#print "flat_xpis shape:%s"%(flat_xpis.get_shape())
# photo tiles
flat_xpts = flatten(xpts, 2) # [N, M, JI, JXP, dim] -> [N*M*JI, JXP, dim]
#print "flat_xpts shape:%s"%(flat_xpts.get_shape())
# get the sequence length, all one dim
flat_qchoices_len = flatten(choices_len, 0) # [N*4]
flat_xat_len = flatten(at_len, 0) # [N*M]
flat_xad_len = flatten(ad_len, 0) # [N*M]
flat_xwhen_len = flatten(when_len, 0) # [N*M]
flat_xwhere_len = flatten(where_len, 0) # [N*M]
flat_xpts_len = flatten(pts_len, 0) # [N*M*JI]
# put all through LSTM
# uncomment to use ALL LSTM output or LAST LSTM output
# album title
# [N*M, JXA, d]
(fw_hat_flat, bw_hat_flat), (fw_lat_flat, bw_lat_flat) = tf.nn.bidirectional_dynamic_rnn(cell_text, cell_text, flat_xat, sequence_length=flat_xat_len, dtype="float", scope="utext")
fw_hat = reconstruct(fw_hat_flat, xat, 2) #
bw_hat = reconstruct(bw_hat_flat, xat, 2)
hat = tf.concat([fw_hat, bw_hat], 3) # [N, M, JXA, 2d]
# lstm
fw_lat = tf.reshape(fw_lat_flat.h, [N, M, d]) # [N*M, d] -> [N, M, d]
bw_lat = tf.reshape(bw_lat_flat.h, [N, M, d])
# GRU
#fw_lat = tf.reshape(fw_lat_flat, [N, M, d]) # [N*M, d] -> [N, M, d]
#bw_lat = tf.reshape(bw_lat_flat, [N, M, d])
lat = tf.concat([fw_lat, bw_lat], 2) # [N, M, 2d]
# album desciption
# [N*M, JD, d]
(fw_had_flat, bw_had_flat), (fw_lad_flat, bw_lad_flat) = tf.nn.bidirectional_dynamic_rnn(cell_text, cell_text, flat_xad, sequence_length=flat_xad_len, dtype="float", scope="utext")
fw_had = reconstruct(fw_had_flat, xad, 2) #
bw_had = reconstruct(bw_had_flat, xad, 2)
had = tf.concat([fw_had, bw_had], 3) # [N, M, JD, 2d]
# LSTM
fw_lad = tf.reshape(fw_lad_flat.h, [N, M, d]) # [N*M, d] -> [N, M, d]
bw_lad = tf.reshape(bw_lad_flat.h, [N, M, d])
# GRU
#fw_lad = tf.reshape(fw_lad_flat, [N, M, d]) # [N*M, d] -> [N, M, d]
#bw_lad = tf.reshape(bw_lad_flat, [N, M, d])
lad = tf.concat([fw_lad, bw_lad], 2) # [N, M, 2d]
# when
(fw_hwhen_flat, bw_hwhen_flat), (fw_lwhen_flat, bw_lwhen_flat) = tf.nn.bidirectional_dynamic_rnn(cell_text, cell_text, flat_xwhen, sequence_length=flat_xwhen_len, dtype="float", scope="utext")
fw_hwhen = reconstruct(fw_hwhen_flat, xwhen, 2) #
bw_hwhen = reconstruct(bw_hwhen_flat, xwhen, 2)
hwhen = tf.concat([fw_hwhen, bw_hwhen], 3) # [N, M, JT, 2d]
# LSTM
fw_lwhen = tf.reshape(fw_lwhen_flat.h, [N, M, d]) # [N*M, d] -> [N, M, d]
bw_lwhen = tf.reshape(bw_lwhen_flat.h, [N, M, d])
# GRU
#fw_lwhen = tf.reshape(fw_lwhen_flat, [N, M, d]) # [N*M, d] -> [N, M, d]
#bw_lwhen = tf.reshape(bw_lwhen_flat, [N, M, d])
lwhen = tf.concat([fw_lwhen, bw_lwhen], 2) # [N, M, 2d]
# where
(fw_hwhere_flat, bw_hwhere_flat), (fw_lwhere_flat, bw_lwhere_flat) = tf.nn.bidirectional_dynamic_rnn(cell_text, cell_text, flat_xwhere, sequence_length=flat_xwhere_len, dtype="float", scope="utext")
fw_hwhere = reconstruct(fw_hwhere_flat, xwhere, 2) #
bw_hwhere = reconstruct(bw_hwhere_flat, xwhere, 2)
hwhere = tf.concat([fw_hwhere, bw_hwhere], 3) # [N, M, JG, 2d]
# LSTM
fw_lwhere = tf.reshape(fw_lwhere_flat.h, [N, M, d]) # [N*M, d] -> [N, M, d]
bw_lwhere = tf.reshape(bw_lwhere_flat.h, [N, M, d])
# GRU
#fw_lwhere = tf.reshape(fw_lwhere_flat, [N, M, d]) # [N*M, d] -> [N, M, d]
#bw_lwhere = tf.reshape(bw_lwhere_flat, [N, M, d])
lwhere = tf.concat([fw_lwhere, bw_lwhere], 2) # [N, M, 2d]
# photo title
# [N*M*JI, JXP, d]
(fw_hpts_flat, bw_hpts_flat), (fw_lpts_flat, bw_lpts_flat) = tf.nn.bidirectional_dynamic_rnn(cell_text, cell_text, flat_xpts, sequence_length=flat_xpts_len, dtype="float", scope="utext")
fw_hpts = reconstruct(fw_hpts_flat, xpts, 2) #
bw_hpts = reconstruct(bw_hpts_flat, xpts, 2) # [N, M, JI, JXP, d]
hpts = tf.concat([fw_hpts, bw_hpts], 4) # [N, M, JI, JXP, 2d]
# LSTM
fw_lpts = tf.reshape(fw_lpts_flat.h, [N, M, JI, d]) # [N*M*JI, d] -> [N, M, JI, d]
bw_lpts = tf.reshape(bw_lpts_flat.h, [N, M, JI, d])
# GRU
#fw_lpts = tf.reshape(fw_lpts_flat, [N, M, JI, d]) # [N*M*JI, d] -> [N, M, JI, d]
#bw_lpts = tf.reshape(bw_lpts_flat, [N, M, JI, d])
lpts = tf.concat([fw_lpts, bw_lpts], 3) # [N, M, JI, 2d]
# choices
(fw_hchoices_flat, bw_hchoices_flat), (fw_lchoices_flat, bw_lchoices_flat) = tf.nn.bidirectional_dynamic_rnn(cell_text, cell_text, flat_qchoices, sequence_length=flat_qchoices_len, dtype="float", scope="utext")
fw_hchoices = reconstruct(fw_hchoices_flat, qchoices, 2) #
bw_hchoices = reconstruct(bw_hchoices_flat, qchoices, 2)
hchoices = tf.concat([fw_hchoices, bw_hchoices], 3) # [N, 4, JA, 2d]
# LSTM
fw_lchoices = tf.reshape(fw_lchoices_flat.h, [N, -1, d]) # [N*4, d] -> [N, 4, d]
bw_lchoices = tf.reshape(bw_lchoices_flat.h, [N, -1, d])
# GRU
#fw_lchoices = tf.reshape(fw_lchoices_flat, [N, -1, d]) # [N*4, d] -> [N, 4, d]
#bw_lchoices = tf.reshape(bw_lchoices_flat, [N, -1, d])
lchoices = tf.concat([fw_lchoices, bw_lchoices], 2) # [N, 4, 2d]
with tf.variable_scope("image"):
# photos
flat_xpis = flatten(xpis, 2) # [N, M, JI, idim] -> [N*M, JI, idim]
flat_xpis_len = flatten(pis_len, 0) # [N*M]
# photo # use different LSTM
# [N*M, JXP, d]
(fw_hpis_flat, bw_hpis_flat), (fw_lpis_flat, bw_lpis_flat) = tf.nn.bidirectional_dynamic_rnn(cell_img, cell_img, flat_xpis, sequence_length=flat_xpis_len, dtype="float", scope="uimage")
fw_hpis = reconstruct(fw_hpis_flat, xpis, 2) #
bw_hpis = reconstruct(bw_hpis_flat, xpis, 2) # [N, M, JI, JXP, d]
hpis = tf.concat([fw_hpis, bw_hpis], 3) # [N, M, JI, 2d]
# LSTM
fw_lpis = tf.reshape(fw_lpis_flat.h, [N, M, d]) # [N*M, d] -> [N, M, d]
bw_lpis = tf.reshape(bw_lpis_flat.h, [N, M, d])
# GRU
#fw_lpis = tf.reshape(fw_lpis_flat, [N, M, d]) # [N*M, d] -> [N, M, d]
#bw_lpis = tf.reshape(bw_lpis_flat, [N, M, d])
lpis = tf.concat([fw_lpis, bw_lpis], 2) # [N, M, 2d]
if config.wd is not None: # l2 weight decay for the reader
add_wd(config.wd)
# all rnn output
# hq -> [N, JQ, 2d]
# hat -> [N, M, JXA, 2d]
# had -> [N, M, JD, 2d]
# hwhen -> [N, M, JT, 2d]
# hwhere -> [N, M, JG, 2d]
# hpts -> [N, M, JI, JXP, 2d]
# hpis -> [N, M, JI, 2d]
# hchoices -> [N, 4, JA, 2d]
# last states:
# lq -> [N, 2d]
# lat -> [N, M, 2d]
# lad -> [N, M, 2d]
# lwhen -> [N, M, 2d]
# lwhere -> [N, M, 2d]
# lpts -> [N, M, JI, 2d]
# lpis -> [N, M, 2d]
# lchoices -> [N, 4, 2d]
# padding to become one context tensor
K = 6 # modality
if config.no_photo:
K=5
with tf.variable_scope("context_tensor"):
# first, pad all sequence to JMAX,
# this is different for each batch
JMAX = tf.reduce_max([JXA, JD, JT, JG, JI*JXP, JI])
#print JMAX
# pad all # paddins is shape (n, 2), where n is the rank of x
hat_pad = tf.pad(hat, paddings=[[0, 0], [0, 0], [0, JMAX-JXA], [0, 0]], mode="CONSTANT", name="hat_pad") # , constant_values=0 # tf 1.3
hat_pad = tf.reshape(hat_pad, [N, M, -1, 2*d]) # need to do this to let tf know the shape?
had_pad = tf.pad(had, paddings=[[0, 0], [0, 0], [0, JMAX-JD], [0, 0]], mode="CONSTANT", name="had_pad")
hwhen_pad = tf.pad(hwhen, paddings=[[0, 0], [0, 0], [0, JMAX-JT], [0, 0]], mode="CONSTANT", name="hwhen_pad")
hwhere_pad = tf.pad(hwhere, paddings=[[0, 0], [0, 0], [0, JMAX-JG], [0, 0]], mode="CONSTANT", name="hwhere_pad")
hpis_pad = tf.pad(hpis, paddings=[[0, 0], [0, 0], [0, JMAX-JI], [0, 0]], mode="CONSTANT", name="hpis_pad")
hpts_re = tf.reshape(hpts, [N, M, -1, 2*d])
hpts_pad = tf.pad(hpts_re, paddings=[[0, 0], [0, 0], [0, JMAX-JI*JXP], [0, 0]], mode="CONSTANT", name="hpts_pad")
at_mask_pad = tf.pad(self.at_mask, paddings=[[0, 0], [0, 0], [0, JMAX-JXA]], mode="CONSTANT", name="at_mask_pad") # , constant_values=0 # tf 1.3
ad_mask_pad = tf.pad(self.ad_mask, paddings=[[0, 0], [0, 0], [0, JMAX-JD]], mode="CONSTANT", name="ad_mask_pad")
when_mask_pad = tf.pad(self.when_mask, paddings=[[0, 0], [0, 0], [0, JMAX-JT]], mode="CONSTANT", name="when_mask_pad")
where_mask_pad = tf.pad(self.where_mask, paddings=[[0, 0], [0, 0], [0, JMAX-JG]], mode="CONSTANT", name="where_mask_pad")
pis_mask_pad = tf.pad(self.pis_mask, paddings=[[0, 0], [0, 0], [0, JMAX-JI]], mode="CONSTANT", name="pis_mask_pad")
pts_mask_re = tf.reshape(self.pts_mask, [N, M, -1])
pts_mask_pad = tf.pad(pts_mask_re, paddings=[[0, 0], [0, 0], [0, JMAX-JI*JXP]], mode="CONSTANT", name="pts_mask_pad")
# now they should all be [N, M, JMAX, 2d]
if config.no_photo:
hall = tf.stack([hat_pad, had_pad, hwhen_pad, hwhere_pad, hpts_pad], axis=1) # [N, K, M, JMAX, 2d]
hall_mask = tf.stack([at_mask_pad, ad_mask_pad, when_mask_pad, where_mask_pad, pts_mask_pad], axis=1) # [N, K, M, JMAX]
else:
# stack!
hall = tf.stack([hat_pad, had_pad, hwhen_pad, hwhere_pad, hpts_pad, hpis_pad], axis=1) # [N, K, M, JMAX, 2d]
hall_mask = tf.stack([at_mask_pad, ad_mask_pad, when_mask_pad, where_mask_pad, pts_mask_pad, pis_mask_pad], axis=1) # [N, K, M, JMAX]
self.hall = hall # the full context tensor
# time correlation c
C = None
"""
# this cause too much memory
if config.use_time_warp:
with tf.variable_scope("time_correlation_c"):
# hq [N, JQ, 2d]
# hall [N, K, M, JMAX, 2d]
hall_no_k = tf.reduce_mean(hall, 1) # [N, M, JMAX, 2d] # smaller tensor
hall_t = tf.reshape(hall_no_k, [N, -1, 2*d]) # [N, T, 2d]
T = tf.shape(hall_t)[-2]
hall_t1 = tf.tile(tf.expand_dims(tf.expand_dims(hall_t, 2), 2), [1, 1, T, JQ, 1])
hall_t2 = tf.tile(tf.expand_dims(tf.expand_dims(hall_t, 2), 2), [1, 1, T, JQ, 1])
hq_t = tf.tile(tf.expand_dims(tf.expand_dims(hq, 1), 1), [1, T, T, 1, 1])
#[N, T, T, JQ, 1]
C_logits = linear(tf.concat([hall_t1*hall_t2, (hall_t1 - hall_t2)*(hall_t1- hall_t2), hall_t1*hq_t, hall_t2*hq_t, (hall_t1 - hq_t)*(hall_t1 - hq_t), (hall_t2 - hq_t)*(hall_t2 - hq_t)], 4), output_size=1, add_tanh=config.add_tanh, scope="C_logits")
C_logits = tf.squeeze(C_logits, 4)
#[N, T, T]
C_logits = tf.reduce_max(C_logits, 3)
# apply time warping function
C = time_warp(C_logits, warp_type=config.warp_type, scope="time_warp_C")
# get new context
hall_tt = tf.tile(tf.expand_dims(tf.reshape(hall, [N, K, -1, 2*d]), 3), [1, 1, 1, T, 1]) #[N, K, T, T, 2d]
C = tf.expand_dims(tf.tile(tf.expand_dims(C, 1), [1, K, 1, 1]), -1)# [N, K, T, T, 1]
h_warped = tf.reduce_sum(hall_tt*C, -2) #[N, K, T, 2d]
hall = tf.reshape(h_warped, [N, K, M, JMAX, 2*d])
"""
# smaller version
if config.use_time_warp:
with tf.variable_scope("time_warp"):
# get question vector first
# hq [N, JQ, 2d]
# hall [N, K, T, 2d]
c_hidden_size = 2*d
# transpose Q
# [N, JQ, 2d] -> [N, 2d, JQ]
#hq_t = tf.transpose(hq, [0, 2, 1])
#print hq_t.get_shape() # need to know JQ
# [N, 2d, JQ] -> [N, 2d, 1]
#WQ = linear(hq_t, output_size=1, add_tanh=False, scope="WQ")
#WQ = tf.squeeze(WQ, 2) #[N, 2d]
WQ = lq
#WQ = attention(hq, tf.reshape(hall, [N, -1, 2*d]), self.q_mask, simiMatrix=config.simiMatrix, wd=config.wd, add_tanh=False, scope="question_att")
hall_t = tf.reshape(hall, [N, K, -1, 2*d]) # [N, K, T, 2d]
T = tf.shape(hall_t)[-2]
# [N, K, T1, T2, 2d]
hall_t1 = tf.tile(tf.expand_dims(hall_t, 3), [1, 1, 1, T, 1])
hall_t2 = tf.tile(tf.expand_dims(hall_t, 3), [1, 1, 1, T, 1])
# WQ [N, 2d] -> [N, K, T1, T2, 2d]
WQ_t = tf.tile(tf.expand_dims(tf.expand_dims(tf.expand_dims(WQ, 1), 1), 1), [1, K, T, T, 1])
#w[h, h], [N, K, T, T, 2d]
WH = linear(tf.concat([hall_t1*hall_t2, (hall_t1-hall_t2)*(hall_t1-hall_t2)], 4), output_size=c_hidden_size, add_tanh=False, scope="WH")
# [N, K, T, T, 1]
C_logits = linear(WH+WQ_t, output_size=1, add_tanh=False, scope="WC")
C_logits = tf.tanh(tf.reduce_sum(tf.squeeze(C_logits, 4), 1)) # [N, T, T]
# for batch_size 1
C_logits = tf.reshape(C_logits, [N, T, T])
self.C = C_logits # correlation
# times different indication function
C, win = time_indication_func(C_logits, warp_type=config.warp_type, scope="time_warp_C") # [N, T, T]
self.C_win = win # the window size variable if the warp_type==5 past_future