-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgatt_model.py
1338 lines (1062 loc) · 60.8 KB
/
gatt_model.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
'''
CoulGAT: A graph attention model utilizing a screened coulomb potentioal attention.
'''
#import basic packages
import sys
import numpy as np
import os
import tensorflow as tf
import common as cm
class GattModel:
def __init__(self, param_dict):
'''
Initialize graph attention model based on hyperparamaters.
Inputs:
param_dict: a dictionary containing hyperparameters for the model.
Returns:
a GattModel object.
'''
self.N = param_dict['num_nodes']
self.F = param_dict['num_features']
self.cls_num = param_dict['class_number']
self.lay_num = param_dict['num_graph_layers']
self.blockn = param_dict['resgnn_block_num'] if param_dict['resgnn_block_num'] is not None else 2
self.F_hid = param_dict['list_hidden_graph_layers']
self.K_hid = param_dict['list_hidden_heads']
self.use_Kavg = param_dict['use_head_averaging'] if param_dict['use_head_averaging'] is not None else False
self.enable_bn = param_dict['enable_bn'] if param_dict['enable_bn'] is not None else False
self.bnaxis = 2
self.bnmomentum = param_dict['bn_momentum'] if param_dict['bn_momentum'] is not None else 0.99
model_sel_dict={'model_1': self.gmodel,
'model_2': self.res_gmodel,
'model_3':self.avg_gmodel,
'model_4':self.res_avg_gmodel,
}
if param_dict['model_name'] in model_sel_dict.keys():
self.model_name=param_dict['model_name']
else:
print('Indicate the model to run and run again:', model_sel_dict.keys())
sys.exit(1)
print('Selected model graph is:', self.model_name)
if self.model_name == 'model_1':
if self.use_Kavg:
self.num_ops=self.lay_num
self.num_khidden=self.lay_num-1
print('Doing K avg in last layer')
else:
self.num_ops=self.lay_num
self.num_khidden=self.lay_num
if self.model_name == 'model_2':
if self.use_Kavg:
self.num_ops = self.blockn * (self.lay_num - 1) + 3
self.num_khidden = self.num_ops - 1
print('Doing K avg in last layer')
else:
self.num_ops = self.blockn * (self.lay_num - 1) + 2
self.num_khidden = self.num_ops
self.K_hid=[1]+[self.K_hid]*(self.num_ops-1)
self.F_hid=[self.F]+[self.F_hid]*(self.num_ops-1)
print("model_2 K_hid:", self.K_hid)
print("model_2 F_hid", self.F_hid)
if self.model_name=='model_3':
self.K_hid = [1]+self.K_hid*(self.lay_num-1)
self.F_hid = [self.F]+self.F_hid*(self.lay_num-1)
print("model_3 K_hid:", self.K_hid)
print("model_3 F_hid", self.F_hid)
if self.model_name=='model_4':
self.K_hid = [1]+self.K_hid*(self.lay_num-1)
self.F_hid = [self.F]+self.F_hid*(self.lay_num-1)
print("model_4 K_hid:", self.K_hid)
print("model_4 F_hid", self.F_hid)
self.istrain=True
self.dense_hid = 2*[self.K_hid[-1]*self.F_hid[-1]] + [self.cls_num]
print('dense hid layers are:', self.dense_hid)
self.batch_size = param_dict['batch_size'] if param_dict['batch_size'] is not None else 50
self.num_epochs = param_dict['num_epochs'] if param_dict['num_epochs'] is not None else 100
self.lr = param_dict['learning_rate'] if param_dict['learning_rate'] is not None else 0.001
self.reg_scale= param_dict['reg_scale'] if param_dict['reg_scale'] is not None else 0.01
self.loss_type= param_dict['loss_type'] if param_dict['loss_type'] is not None else 'SCCLMAE'
self.trn_in_keep_prob = param_dict['trn_in_keep_prob'] if param_dict['trn_in_keep_prob'] is not None else 1.0
self.trn_eij_keep_prob = param_dict['trn_eij_keep_prob'] if param_dict['trn_eij_keep_prob'] is not None else 1.0
self.enable_pw=param_dict['enable_pw'] if param_dict['enable_pw'] is not None else False
self.is_classify = param_dict['is_classify'] if param_dict['is_classify'] is not None else False
self.num_early_stop = param_dict['num_early_stop'] if param_dict['num_early_stop'] is not None else 0.5
self.early_stop_int = int(np.ceil((self.num_epochs-1) / self.num_early_stop))
self.early_stop_threshold = param_dict['early_stop_threshold'] if param_dict['early_stop_threshold'] is not None else 0
self.models_folder=param_dict['models_folder'] if param_dict['models_folder'] is not None else 'tmp_saved_models'
self.sum_folder=param_dict['sum_folder'] if param_dict['sum_folder'] is not None else 'summaries'
self.postfix=param_dict['label']+'_'+self.model_name if param_dict['label'] is not None else self.model_name
self.batch_count = None
self.gat_graph=tf.Graph()
model_param_dict=model_sel_dict[self.model_name](self.gat_graph)
self.init=model_param_dict['init']
self.train_op=model_param_dict['train_op']
self.loss=model_param_dict['loss']
self.y_pred=model_param_dict['y_pred']
self.pwlst_hid=model_param_dict['adjm_power']
self.alst_hid=model_param_dict['alst_hid']
self.Wlst_hid=model_param_dict['Wlst_hid']
self.blst_hid=model_param_dict['blst_hid']
self.Hlst=model_param_dict['Hlst']
self.dense_layers=model_param_dict['dense_layers']
self.X=model_param_dict['X']
self.AdjM=model_param_dict['AdjM']
self.y=model_param_dict['y']
self.X_in_var=model_param_dict['X_in_var']
self.Xadjm_in_var=model_param_dict['Xadjm_in_var']
self.Y_in_var=model_param_dict['Y_in_var']
self.YID_in_var = model_param_dict['YID_in_var']
self.sbuff_size=model_param_dict['shuffle_buff_size']
self.val_X_in_var=model_param_dict['val_X_in_var']
self.val_Xadjm_in_var=model_param_dict['val_Xadjm_in_var']
self.val_Y_in_var=model_param_dict['val_Y_in_var']
self.val_YID_in_var=model_param_dict['val_YID_in_var']
self.saver=model_param_dict['saver']
self.trn_iterator=model_param_dict['trn_iterator']
self.val_iterator=model_param_dict['val_iterator']
self.loss_early_stop = 1000
print('parameters are initialized from dict:', param_dict)
cm.pklsave('saved_model_params/hyperparams_'+self.postfix+'.pkl', param_dict)
@staticmethod
def single_attn_mod( a, W, b, H, Adj, act_att=tf.nn.leaky_relu, pw=None):
'''
Calculates the attention coefficient matrix E
Inputs:
a: learnable attention vector.
W: Weight tensor
b: bias tensor
H: input tensor to hidden layer
Adj: weighted adjacency matrix
act_att: activation function used in the attention
Returns:
E: the softmax normalized attention function output
'''
We = tf.tile(tf.expand_dims(W, 0), tf.stack([tf.shape(H)[0], 1, 1]))
WH = tf.add(tf.matmul(We, H), b) # dim batch_size, F' N
if pw is not None:
pwe = tf.tile(tf.expand_dims(pw, 0), tf.stack([tf.shape(H)[0], 1, 1]))
Adj=tf.math.pow(Adj, pwe, name="Adj_pw")
Adj = tf.nn.softmax(Adj, axis=-1)
ae = tf.tile(tf.expand_dims(a, 0), tf.stack([tf.shape(H)[0], 1, 1]))
WHiWHj = tf.matmul(WH, tf.transpose(Adj, [0, 2, 1]))
E = act_att(tf.matmul(ae, WHiWHj))
En = tf.nn.softmax(E, axis=-1)
return En, WH
@staticmethod
def single_hidden_out(E, WH, act_hidden=tf.nn.relu, in_keep_prob=1.0, eij_keep_prob=1.0):
'''
Calculates the hidden layer output for single head.
Inputs:
E: attention coefficient matrix
WH: Matrix multiplication of Weight tensor and input tensor
act_hidden: activation function for layer output
in_keep_prob:1 - drop out rate for WH
eij_keep_prob:1- drop out rate for E
Returns:
Next hidden layer tensor for single head
'''
if in_keep_prob < 1.0:
WH = tf.nn.dropout(WH, rate=1 - in_keep_prob)
if eij_keep_prob < 1.0:
E = tf.nn.dropout(E, rate=1 - eij_keep_prob)
H_next = act_hidden(tf.matmul(WH, tf.transpose(E, [0, 2,1])))
return H_next
def K_hidden_out(self, H, A, alst, Wlst, blst, K=1, clsfy_layer=False, in_keep_prob=1.0,
eij_keep_prob=1.0, act_att=tf.nn.leaky_relu, act_hidden=tf.nn.relu, pwlst=None):
'''
Calculates the hidden layer output for K heads
Inputs:
H: input tensor
A: weighted adjacency matrix
alst: List of attention vectors for the hidden layer
Wlst: list of weight tensors for the hidden layer
K: number of heads
clsfy_layer: True: pooling, False: concatenating
in_keep_prob:1 - drop out rate for WH
eij_keep_prob:1- drop out rate for E
act_hidden: activation function for layer output
act_att: activation function used in the attention
pwlist: list of learnable power matrix
Returns:
Next hidden layer tensor
'''
Hlst = []
if not clsfy_layer:
act_input=act_hidden
else:
act_input=tf.identity
for i in range(K):
pwlst_item=pwlst[i] if pwlst is not None else None
Ei, WlstiH = self.single_attn_mod(alst[i], Wlst[i], blst[i], H, A, act_att=act_att, pw=pwlst_item)
Hi = self.single_hidden_out(Ei, WlstiH, act_hidden=act_input,
in_keep_prob=in_keep_prob, eij_keep_prob=eij_keep_prob)
Hlst.append(Hi)
if not clsfy_layer:
H_next = tf.concat(Hlst, axis=1)
else:
H_next = tf.add_n(Hlst) / K
return H_next
def res_K_hidden_out(self, H, A, alst, Wlst, blst, K, clsfy_layer=False, in_keep_prob=1.0,
eij_keep_prob=1.0, act_att=tf.nn.leaky_relu, act_hidden=tf.nn.relu, pwlst=None, en_BN=False):
'''
Calculates the hidden layer output for K heads for residual networks.
Inputs:
H: input tensor
A: weighted adjacency matrix
alst: List of attention vectors for the hidden layer
Wlst: list of weight tensors for the hidden layer
K: number of heads
clsfy_layer: True: pooling, False: concatenating
in_keep_prob:1 - drop out rate for WH
eij_keep_prob:1- drop out rate for E
act_hidden: activation function for layer output
act_att: activation function used in the attention
pwlist: list of learnable power matrix
en_BN: enable abtch normalization before activation
Returns:
List of hidden layer tensors
'''
Hresb=[H]
for i in range(0, self.blockn):
if en_BN:
Hresb[i]=tf.layers.batch_normalization(Hresb[i], axis=self.bnaxis, training=self.istrain, momentum=self.bnmomentum)
Hresb.append(self.K_hidden_out(act_hidden(Hresb[i]), A, alst[i], Wlst[i], blst[i], K=K[i], clsfy_layer=clsfy_layer, in_keep_prob=in_keep_prob,
eij_keep_prob=eij_keep_prob, act_att=act_att, act_hidden=tf.identity, pwlst=pwlst[i]))
Hresb[-1]=Hresb[-1]+H
return Hresb[1:]
def avg_res_K_hidden_out(self, H, A, alst, Wlst, blst, K, clsfy_layer=False, in_keep_prob=1.0,
eij_keep_prob=1.0, act_att=tf.nn.leaky_relu, act_hidden=tf.nn.relu, pwlst=None, en_BN=False, en_res=True):
'''
Calculates the hidden layer output for K heads for residual networks with averaging.
Inputs:
H: input tensor
A: weighted adjacency matrix
alst: List of attention vectors for the hidden layer
Wlst: list of weight tensors for the hidden layer
K: number of heads
clsfy_layer: True: pooling, False: concatenating
in_keep_prob:1 - drop out rate for WH
eij_keep_prob:1- drop out rate for E
act_hidden: activation function for layer output
act_att: activation function used in the attention
pwlist: list of learnable power matrix
en_BN: enable abtch normalization before activation
en_res: Enable residual connection
Returns:
List of hidden layer tensors calculated within resblock
'''
Hresb=[H]
for i in range(0, self.blockn-1):
pwlst_item = pwlst[i] if pwlst is not None else None
if en_BN:
Hresb[i]=tf.layers.batch_normalization(Hresb[i], axis=self.bnaxis, training=self.istrain, momentum=self.bnmomentum)
Hresb.append(self.K_hidden_out(act_hidden(Hresb[i]), A, alst[i], Wlst[i], blst[i], K=K[i], clsfy_layer=clsfy_layer, in_keep_prob=in_keep_prob,
eij_keep_prob=eij_keep_prob, act_att=act_att, act_hidden=tf.identity, pwlst=pwlst_item))
if en_BN:
Hresb[-1]=tf.layers.batch_normalization(Hresb[-1], axis=self.bnaxis, training=self.istrain, momentum=self.bnmomentum)
pwlst_item = pwlst[-1] if pwlst is not None else None
Hresb.append(self.K_hidden_out(act_hidden(Hresb[-1]), A, alst[-1], Wlst[-1], blst[-1], K=K[-1],
clsfy_layer=True, in_keep_prob=in_keep_prob,
eij_keep_prob=eij_keep_prob, act_att=act_att, act_hidden=tf.identity,
pwlst=pwlst_item))
if en_res:
Hresb[-1]=Hresb[-1]+H
return Hresb[1:]
def avg_K_hidden_out(self, H, A, alst, Wlst, blst, K, clsfy_layer=False, in_keep_prob=1.0,
eij_keep_prob=1.0, act_att=tf.nn.leaky_relu, act_hidden=tf.nn.relu, pwlst=None, en_BN=False, en_fin_act=True):
'''
Calculates the hidden layer output for K heads for plain network with averaging.
Inputs:
H: input tensor
A: weighted adjacency matrix
alst: List of attention vectors for the hidden layer
Wlst: list of weight tensors for the hidden layer
K: number of heads
clsfy_layer: True: pooling, False: concatenating
in_keep_prob:1 - drop out rate for WH
eij_keep_prob:1- drop out rate for E
act_hidden: activation function for layer output
act_att: activation function used in the attention
pwlist: list of learnable power matrix
en_BN: enable abtch normalization before activation
en_fin_act: Apply activation to output if true.
Returns:
List of hidden layer tensors calculated within resblock
'''
Hresb=[H]
for i in range(0, self.blockn-1):
pwlst_item = pwlst[i] if pwlst is not None else None
Hresb.append(self.K_hidden_out(Hresb[i], A, alst[i], Wlst[i], blst[i], K=K[i], clsfy_layer=clsfy_layer, in_keep_prob=in_keep_prob,
eij_keep_prob=eij_keep_prob, act_att=act_att, act_hidden=tf.identity, pwlst=pwlst_item))
if en_BN:
Hresb[-1]=tf.layers.batch_normalization(Hresb[-1], axis=self.bnaxis, training=self.istrain, momentum=self.bnmomentum)
Hresb[-1]=act_hidden(Hresb[-1])
pwlst_item = pwlst[-1] if pwlst is not None else None
Hresb.append(self.K_hidden_out(Hresb[-1], A, alst[-1], Wlst[-1], blst[-1], K=K[-1],
clsfy_layer=True, in_keep_prob=in_keep_prob,
eij_keep_prob=eij_keep_prob, act_att=act_att, act_hidden=tf.identity,
pwlst=pwlst_item))
if en_BN:
Hresb[-1]=tf.layers.batch_normalization(Hresb[-1], axis=self.bnaxis, training=self.istrain, momentum=self.bnmomentum)
if en_fin_act:
Hresb[-1]=act_hidden(Hresb[-1])
return Hresb[1:]
def make_iter(self):
'''
Generate reinitializable iterators for training and validation.
'''
X_in_var = tf.placeholder(tf.float32, shape=[None, self.F, self.N], name="X_in_var")
Xadjm_in_var = tf.placeholder(tf.float32, shape=[None, self.N, self.N], name="Xadjm_in_var")
Y_in_var = tf.placeholder(tf.float32, shape=[None, self.cls_num], name="Y_in_var")
YID_in_var = tf.placeholder(tf.int32, shape=[None, self.cls_num], name="YID_in_var")
sbuff_size=tf.placeholder(tf.int64, shape=[], name="shuffle_buff_size")
val_X_in_var = tf.placeholder(tf.float32, shape=[None, self.F, self.N], name="val_X_in_var")
val_Xadjm_in_var = tf.placeholder(tf.float32, shape=[None, self.N, self.N], name="val_Xadjm_in_var")
val_Y_in_var = tf.placeholder(tf.float32, shape=[None, self.cls_num], name="val_Y_in_var")
val_YID_in_var = tf.placeholder(tf.int32, shape=[None, self.cls_num], name="val_YID_in_var")
dataset1ka = tf.data.Dataset.from_tensor_slices(X_in_var)
dataset1kb = tf.data.Dataset.from_tensor_slices(Xadjm_in_var)
dataset1kc = tf.data.Dataset.from_tensor_slices(Y_in_var)
dataset1kd = tf.data.Dataset.from_tensor_slices(YID_in_var)
dataset1k = tf.data.Dataset.zip((dataset1ka, dataset1kb, dataset1kc, dataset1kd))
print("Train Dataset output types:", dataset1k.output_types)
print("Train Dataset output shapes:", dataset1k.output_shapes)
dataset1k = dataset1k.shuffle(buffer_size=sbuff_size)
dataset1k = dataset1k.repeat()
batch_dataset1k = dataset1k.batch(self.batch_size).prefetch(1)
val_dataset1ka = tf.data.Dataset.from_tensor_slices(val_X_in_var)
val_dataset1kb = tf.data.Dataset.from_tensor_slices(val_Xadjm_in_var)
val_dataset1kc = tf.data.Dataset.from_tensor_slices(val_Y_in_var)
val_dataset1kd = tf.data.Dataset.from_tensor_slices(val_YID_in_var)
val_dataset1k = tf.data.Dataset.zip((val_dataset1ka, val_dataset1kb, val_dataset1kc, val_dataset1kd))
print("Validation Dataset output types:", val_dataset1k.output_types)
print("Validation Dataset output shapes:", val_dataset1k.output_shapes)
val_batch_dataset1k = val_dataset1k.batch(self.batch_size)
iterator = tf.data.Iterator.from_structure(batch_dataset1k.output_types, batch_dataset1k.output_shapes)
trn_iterator=iterator.make_initializer(batch_dataset1k)
val_iterator=iterator.make_initializer(val_batch_dataset1k)
data_param_dict={
'iterator':iterator,
'trn_iterator':trn_iterator,
'val_iterator':val_iterator,
'X_in_var': X_in_var,
'Xadjm_in_var': Xadjm_in_var,
'Y_in_var': Y_in_var,
'YID_in_var': YID_in_var,
'val_X_in_var': val_X_in_var,
'val_Xadjm_in_var': val_Xadjm_in_var,
'val_Y_in_var': val_Y_in_var,
'val_YID_in_var': val_YID_in_var,
'shuffle_buff_size':sbuff_size
}
return data_param_dict
def scclmae(self, y, y_pred, yid):
'''
Calculates the SCCLMAE score
Inputs:
y: true values
y_pred: predicted values
yid: id values for nor-zero entries of y
Returns:
SCCLMAE loss
'''
yid_nz_cond=tf.greater(yid, 0)
yid_nz_ind=tf.where_v2(yid_nz_cond)
y_nz=tf.gather_nd(y, yid_nz_ind)
y_pred_nz=tf.gather_nd(y_pred, yid_nz_ind)
lmae_loss=tf.math.log(1e-9+tf.losses.absolute_difference(y_nz, y_pred_nz))
return lmae_loss
# model_1
def gmodel(self, gat_graph_in):
'''
Model_1: Plain graph attention network with/without pooling as final layer.
'''
reset_graph()
with gat_graph_in.as_default():
set_graph_seed()
X = tf.placeholder(tf.float32, shape=[None, self.F, self.N], name="X_inp")
AdjM = tf.placeholder(tf.float32, shape=[None, self.N, self.N], name="adja")
y = tf.placeholder(tf.float32, shape=[None, self.cls_num], name="y_label")
yid = tf.placeholder(tf.int32, shape=[None, self.cls_num], name="y_label_id")
data_param_dict_in = self.make_iter()
X, AdjM, y, yid = data_param_dict_in['iterator'].get_next()
alst_hid = []
Wlst_hid = []
blst_hid = []
pwlst_hid = []
kernel_regularizer = tf.contrib.layers.l2_regularizer(self.reg_scale)
pw_initializer = tf.contrib.layers.xavier_initializer()
with tf.variable_scope("gat_a_and_W", reuse=tf.AUTO_REUSE):
for h in range(1, self.num_ops):
alst_hid.append([tf.get_variable("alst_hid" + str(h) + str(i), shape=(self.N, self.F_hid[h]),
dtype=tf.float32,
initializer=tf.contrib.layers.xavier_initializer()) for i in
range(self.K_hid[h])])
if self.enable_pw is True:
pwlst_hid.append([tf.get_variable("pwlst_hid" + str(h) + str(i), shape=(self.N, self.N),
dtype=tf.float32,
initializer=pw_initializer) for i in range(self.K_hid[h])])
Wlst_hid.append([tf.get_variable("Wlst_hid" + str(h) + str(i),
shape=(self.F_hid[h], self.K_hid[h - 1] * self.F_hid[h - 1]),
dtype=tf.float32,
initializer=tf.contrib.layers.xavier_initializer()) for i in
range(self.K_hid[h])])
blst_hid.append([tf.get_variable("blst_hid" + str(h) + str(i), shape=(self.F_hid[h], self.N),
dtype=tf.float32,
initializer=tf.initializers.zeros()) for i in
range(self.K_hid[h])])
Hlst = []
Hlst.append(X)
for h in range(1, self.num_khidden):
pwlst_item = pwlst_hid[h - 1] if self.enable_pw is True else None
Hlst.append(self.K_hidden_out(Hlst[h - 1], AdjM, alst_hid[h - 1], Wlst_hid[h - 1], blst_hid[h - 1],
K=self.K_hid[h],
clsfy_layer=False, in_keep_prob=self.trn_in_keep_prob,
eij_keep_prob=self.trn_eij_keep_prob, act_att=tf.nn.leaky_relu,
act_hidden=tf.nn.relu, pwlst=pwlst_item))
if self.use_Kavg:
pwlst_item = pwlst_hid[h - 1] if self.enable_pw is True else None
Hlst.append(
self.K_hidden_out(Hlst[-1], AdjM, alst_hid[-1], Wlst_hid[-1], blst_hid[-1], K=self.K_hid[-1],
clsfy_layer=True, in_keep_prob=self.trn_in_keep_prob,
eij_keep_prob=self.trn_eij_keep_prob, act_att=tf.nn.leaky_relu,
act_hidden=tf.nn.relu,
pwlst=pwlst_item))
Hlst[-1] = tf.nn.relu(Hlst[-1])
flat_H = tf.reshape(Hlst[-1], (-1, Hlst[-1].shape[1] * Hlst[-1].shape[2]))
print('Input shape is:', X.shape)
print('Last output shape is:', Hlst[-1].shape)
print('Flattened last output shape is:', flat_H.shape)
if len(self.dense_hid) > 1:
dense_layers = [tf.layers.dense(inputs=flat_H, units=self.dense_hid[0], activation=tf.nn.relu,
kernel_initializer='glorot_uniform', use_bias=True,
bias_initializer='zeros', kernel_regularizer=kernel_regularizer)]
for n in range(1, len(self.dense_hid[:-1])):
dense_layers.append(
tf.layers.dense(inputs=dense_layers[n - 1], units=self.dense_hid[n], activation=tf.nn.relu,
kernel_initializer='glorot_uniform', use_bias=True,
bias_initializer='zeros', kernel_regularizer=kernel_regularizer))
dense_layers.append(
tf.layers.dense(inputs=dense_layers[-1], units=self.dense_hid[-1], activation=None,
kernel_initializer='glorot_uniform',
use_bias=True, bias_initializer='zeros', kernel_regularizer=kernel_regularizer))
else:
dense_layers = [tf.layers.dense(inputs=flat_H, units=self.dense_hid[-1], activation=None,
kernel_initializer='glorot_uniform',
use_bias=True, bias_initializer='zeros',
kernel_regularizer=kernel_regularizer)]
y_pred = dense_layers[-1]
loss_dict = {
'MSE': tf.losses.mean_squared_error(y, y_pred),
'MAE': tf.losses.absolute_difference(y, y_pred),
'HUBER': tf.losses.huber_loss(y, y_pred, delta=1.0),
'SCCLMAE': self.scclmae(y, y_pred, yid)
}
if self.is_classify:
loss = tf.nn.softmax_cross_entropy_with_logits_v2(y, y_pred)
else:
loss = loss_dict[self.loss_type]
reg_losses = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)
loss = tf.add_n([loss] + reg_losses)
opt = tf.train.AdamOptimizer(learning_rate=self.lr)
train_op = opt.minimize(loss)
init = tf.global_variables_initializer()
saver = tf.train.Saver()
model_param_dict = {
'X': X,
'AdjM': AdjM,
'y': y,
'init': init,
'train_op': train_op,
'loss': loss,
'y_pred': y_pred,
'adjm_power': pwlst_hid,
'alst_hid': alst_hid,
'Wlst_hid': Wlst_hid,
'blst_hid': blst_hid,
'Hlst': Hlst,
'dense_layers': dense_layers,
'saver': saver,
'X_in_var': data_param_dict_in['X_in_var'],
'Xadjm_in_var': data_param_dict_in['Xadjm_in_var'],
'Y_in_var': data_param_dict_in['Y_in_var'],
'YID_in_var': data_param_dict_in['YID_in_var'],
'val_X_in_var': data_param_dict_in['val_X_in_var'],
'val_Xadjm_in_var': data_param_dict_in['val_Xadjm_in_var'],
'val_Y_in_var': data_param_dict_in['val_Y_in_var'],
'val_YID_in_var': data_param_dict_in['val_YID_in_var'],
'shuffle_buff_size': data_param_dict_in['shuffle_buff_size'],
'trn_iterator': data_param_dict_in['trn_iterator'],
'val_iterator': data_param_dict_in['val_iterator']
}
return model_param_dict
#model_2
def res_gmodel(self, gat_graph_in):
'''
Model_2: Residual graph attention network with/without pooling as final layer.
'''
reset_graph()
with gat_graph_in.as_default():
set_graph_seed()
X = tf.placeholder(tf.float32, shape=[None, self.F, self.N], name="X_inp")
AdjM = tf.placeholder(tf.float32, shape=[None, self.N, self.N], name="adja")
y = tf.placeholder(tf.float32, shape=[None, self.cls_num], name="y_label")
yid = tf.placeholder(tf.int32, shape=[None, self.cls_num], name="y_label_id")
data_param_dict_in=self.make_iter()
X, AdjM, y, yid = data_param_dict_in['iterator'].get_next()
alst_hid = []
Wlst_hid = []
blst_hid = []
pwlst_hid=[]
kernel_regularizer=tf.contrib.layers.l2_regularizer(self.reg_scale)
pw_initializer=tf.contrib.layers.xavier_initializer()
bn=self.blockn
with tf.variable_scope("gat_a_and_W", reuse=tf.AUTO_REUSE):
for h in range(1, self.num_ops):
alst_hid.append([tf.get_variable("alst_hid" + str(h) + str(i), shape=(self.N, self.F_hid[h]), dtype=tf.float32,
initializer=tf.contrib.layers.xavier_initializer()) for i in
range(self.K_hid[h])])
if self.enable_pw is True:
pwlst_hid.append([tf.get_variable("pwlst_hid" + str(h) + str(i), shape=(self.N, self.N), dtype=tf.float32,
initializer=pw_initializer) for i in range(self.K_hid[h])])
Wlst_hid.append([tf.get_variable("Wlst_hid" + str(h) + str(i),
shape=(self.F_hid[h], self.K_hid[h - 1] * self.F_hid[h - 1]), dtype=tf.float32,
initializer=tf.contrib.layers.xavier_initializer()) for i in
range(self.K_hid[h])])
blst_hid.append([tf.get_variable("blst_hid" + str(h) + str(i), shape=(self.F_hid[h], self.N), dtype=tf.float32,
initializer=tf.initializers.zeros()) for i in range(self.K_hid[h])])
Hlst = []
Hlst.append(X)
pwlst_item = pwlst_hid[0] if self.enable_pw is True else None
Hlst.append(self.K_hidden_out(Hlst[0], AdjM, alst_hid[0], Wlst_hid[0], blst_hid[0], K=self.K_hid[1],
clsfy_layer=False, in_keep_prob=self.trn_in_keep_prob,
eij_keep_prob=self.trn_eij_keep_prob, act_att=tf.nn.leaky_relu, act_hidden=tf.identity, pwlst=pwlst_item))
if self.enable_bn:
Hlst[-1] = tf.layers.batch_normalization(Hlst[-1], axis=self.bnaxis, training=self.istrain, momentum=self.bnmomentum)
Hlst[-1] = tf.nn.relu(Hlst[-1])
for h in range(2, self.num_khidden, bn):
pwlst_item = pwlst_hid[h-1:h-1+bn] if self.enable_pw is True else None
Hlst=Hlst+ self.res_K_hidden_out(Hlst[h-1], AdjM, alst_hid[h-1:h-1+bn], Wlst_hid[h-1:h-1+bn], blst_hid[h-1:h-1+bn], K=self.K_hid[h:h+bn],
clsfy_layer=False, in_keep_prob=self.trn_in_keep_prob,
eij_keep_prob=self.trn_eij_keep_prob, act_att=tf.nn.leaky_relu, act_hidden=tf.nn.relu, pwlst=pwlst_item, en_BN=self.enable_bn)
Hlst[-1] = tf.nn.relu(Hlst[-1])
if self.use_Kavg:
Hlst.append(self.K_hidden_out(Hlst[-1], AdjM, alst_hid[-1], Wlst_hid[-1], blst_hid[-1], K=self.K_hid[-1],
clsfy_layer=True, in_keep_prob=self.trn_in_keep_prob,
eij_keep_prob=self.trn_eij_keep_prob, act_att=tf.nn.leaky_relu, act_hidden=tf.nn.relu,
pwlst=pwlst_hid[-1]))
Hlst[-1] = tf.nn.relu(Hlst[-1])
flat_H = tf.reshape(Hlst[-1], (-1, Hlst[-1].shape[1] * Hlst[-1].shape[2]))
print('Input shape is:', X.shape)
print('Last output shape is:', Hlst[-1].shape)
print('Flattened last output shape is:', flat_H.shape)
if len(self.dense_hid) > 1:
dense_layers = [tf.layers.dense(inputs=flat_H, units=self.dense_hid[0], activation=tf.nn.relu,
kernel_initializer='glorot_uniform', use_bias=True, bias_initializer='zeros', kernel_regularizer=kernel_regularizer)]
for n in range(1, len(self.dense_hid[:-1])):
dense_layers.append(tf.layers.dense(inputs=dense_layers[n - 1], units=self.dense_hid[n], activation=tf.nn.relu,
kernel_initializer='glorot_uniform', use_bias=True,
bias_initializer='zeros', kernel_regularizer=kernel_regularizer))
dense_layers.append(tf.layers.dense(inputs=dense_layers[-1], units=self.dense_hid[-1], activation=None,
kernel_initializer='glorot_uniform',
use_bias=True, bias_initializer='zeros', kernel_regularizer=kernel_regularizer))
else:
dense_layers= [tf.layers.dense(inputs=flat_H, units=self.dense_hid[-1], activation=None,
kernel_initializer='glorot_uniform',
use_bias=True, bias_initializer='zeros', kernel_regularizer=kernel_regularizer)]
y_pred = dense_layers[-1]
loss_dict={
'MSE': tf.losses.mean_squared_error(y, y_pred),
'MAE': tf.losses.absolute_difference(y, y_pred),
'HUBER': tf.losses.huber_loss(y, y_pred, delta=1.0),
'SCCLMAE': self.scclmae(y, y_pred, yid)
}
if self.is_classify:
loss = tf.nn.softmax_cross_entropy_with_logits_v2(y, y_pred)
else:
loss = loss_dict[self.loss_type]
reg_losses = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)
loss=tf.add_n([loss]+reg_losses)
opt = tf.train.AdamOptimizer(learning_rate=self.lr)
train_op = opt.minimize(loss)
init = tf.global_variables_initializer()
saver = tf.train.Saver()
model_param_dict={
'X': X,
'AdjM': AdjM,
'y': y,
'init': init,
'train_op':train_op,
'loss':loss,
'y_pred': y_pred,
'adjm_power':pwlst_hid,
'alst_hid':alst_hid,
'Wlst_hid':Wlst_hid,
'blst_hid': blst_hid,
'Hlst':Hlst,
'dense_layers': dense_layers,
'saver': saver,
'X_in_var':data_param_dict_in['X_in_var'],
'Xadjm_in_var': data_param_dict_in['Xadjm_in_var'],
'Y_in_var': data_param_dict_in['Y_in_var'],
'YID_in_var': data_param_dict_in['YID_in_var'],
'val_X_in_var': data_param_dict_in['val_X_in_var'],
'val_Xadjm_in_var': data_param_dict_in['val_Xadjm_in_var'],
'val_Y_in_var': data_param_dict_in['val_Y_in_var'],
'val_YID_in_var': data_param_dict_in['val_YID_in_var'],
'shuffle_buff_size':data_param_dict_in['shuffle_buff_size'],
'trn_iterator': data_param_dict_in['trn_iterator'],
'val_iterator': data_param_dict_in['val_iterator']
}
return model_param_dict
#model_3
def avg_gmodel(self, gat_graph_in):
'''
model_3: plain graph attention network formed by attention layer blocks
with pooling as last layer.
'''
reset_graph()
with gat_graph_in.as_default():
set_graph_seed()
X = tf.placeholder(tf.float32, shape=[None, self.F, self.N], name="X_inp")
AdjM = tf.placeholder(tf.float32, shape=[None, self.N, self.N], name="adja")
y = tf.placeholder(tf.float32, shape=[None, self.cls_num], name="y_label")
yid = tf.placeholder(tf.int32, shape=[None, self.cls_num], name="y_label_id")
data_param_dict_in=self.make_iter()
X, AdjM, y, yid = data_param_dict_in['iterator'].get_next()
alst_hid = []
Wlst_hid = []
blst_hid = []
pwlst_hid=[]
bn=self.blockn
kernel_regularizer=tf.contrib.layers.l2_regularizer(self.reg_scale)
pw_initializer=tf.contrib.layers.xavier_initializer()
with tf.variable_scope("gat_a_and_W", reuse=tf.AUTO_REUSE):
for h in range(1, (self.lay_num-1)*bn+1, bn):
for k in range(0, bn):
if k==0:
K_mult=1
else:
K_mult=self.K_hid[h+k - 1]
alst_hid.append([tf.get_variable("alst_hid" + str(h) +str(k)+str(i), shape=(self.N, self.F_hid[h]), dtype=tf.float32,
initializer=tf.contrib.layers.xavier_initializer()) for i in
range(self.K_hid[h+k])])
if self.enable_pw is True:
pwlst_hid.append([tf.get_variable("pwlst_hid" + str(h)+str(k)+str(i), shape=(self.N, self.N), dtype=tf.float32,
initializer=pw_initializer) for i in range(self.K_hid[h+k])])
Wlst_hid.append([tf.get_variable("Wlst_hid" + str(h)+str(k)+str(i),
shape=(self.F_hid[h+k], K_mult * self.F_hid[h+k - 1]), dtype=tf.float32,
initializer=tf.contrib.layers.xavier_initializer()) for i in
range(self.K_hid[h+k])])
blst_hid.append([tf.get_variable("blst_hid" + str(h) +str(k)+ str(i), shape=(self.F_hid[h+k], self.N), dtype=tf.float32,
initializer=tf.initializers.zeros()) for i in range(self.K_hid[h+k])])
Hlst = []
Hlst.append(X)
for h in range(1, (self.lay_num-1)*bn+1, bn):
pwlst_item=pwlst_hid[h-1:h-1+bn] if self.enable_pw is True else None
Hlst=Hlst + self.avg_K_hidden_out(Hlst[h-1], AdjM, alst_hid[h-1:h-1+bn], Wlst_hid[h-1:h-1+bn], blst_hid[h-1:h-1+bn], self.K_hid[h:h+bn], clsfy_layer=False, in_keep_prob=self.trn_in_keep_prob,
eij_keep_prob=self.trn_eij_keep_prob, act_att=tf.nn.leaky_relu, act_hidden=tf.nn.relu, pwlst=pwlst_item,
en_BN=self.enable_bn, en_fin_act=True)
flat_H = tf.reshape(Hlst[-1], (-1, Hlst[-1].shape[1] * Hlst[-1].shape[2]))
print('Input shape is:', X.shape)
print('Last output shape is:', Hlst[-1].shape)
print('Flattened last output shape is:', flat_H.shape)
if len(self.dense_hid) > 1:
dense_layers = [tf.layers.dense(inputs=flat_H, units=self.dense_hid[0], activation=tf.nn.relu,
kernel_initializer='glorot_uniform', use_bias=True, bias_initializer='zeros', kernel_regularizer=kernel_regularizer)]
for n in range(1, len(self.dense_hid[:-1])):
dense_layers.append(tf.layers.dense(inputs=dense_layers[n - 1], units=self.dense_hid[n], activation=tf.nn.relu,
kernel_initializer='glorot_uniform', use_bias=True,
bias_initializer='zeros', kernel_regularizer=kernel_regularizer))
dense_layers.append(tf.layers.dense(inputs=dense_layers[-1], units=self.dense_hid[-1], activation=None,
kernel_initializer='glorot_uniform',
use_bias=True, bias_initializer='zeros', kernel_regularizer=kernel_regularizer))
else:
dense_layers= [tf.layers.dense(inputs=flat_H, units=self.dense_hid[-1], activation=None,
kernel_initializer='glorot_uniform',
use_bias=True, bias_initializer='zeros', kernel_regularizer=kernel_regularizer)]
y_pred = dense_layers[-1]
loss_dict={
'MSE': tf.losses.mean_squared_error(y, y_pred),
'MAE': tf.losses.absolute_difference(y, y_pred),
'HUBER': tf.losses.huber_loss(y, y_pred, delta=1.0),
'SCCLMAE': self.scclmae(y, y_pred, yid)
}
if self.is_classify:
loss = tf.nn.softmax_cross_entropy_with_logits_v2(y, y_pred)
else:
loss = loss_dict[self.loss_type]
reg_losses = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)
loss=tf.add_n([loss]+reg_losses)
opt = tf.train.AdamOptimizer(learning_rate=self.lr)
train_op = opt.minimize(loss)
init = tf.global_variables_initializer()
saver = tf.train.Saver()
model_param_dict={
'X': X,
'AdjM': AdjM,
'y': y,
'init': init,
'train_op':train_op,
'loss':loss,
'y_pred': y_pred,
'adjm_power':pwlst_hid,
'alst_hid':alst_hid,
'Wlst_hid':Wlst_hid,
'blst_hid': blst_hid,
'Hlst':Hlst,
'dense_layers': dense_layers,
'saver': saver,
'X_in_var':data_param_dict_in['X_in_var'],
'Xadjm_in_var': data_param_dict_in['Xadjm_in_var'],
'Y_in_var': data_param_dict_in['Y_in_var'],
'YID_in_var': data_param_dict_in['YID_in_var'],
'val_X_in_var': data_param_dict_in['val_X_in_var'],
'val_Xadjm_in_var': data_param_dict_in['val_Xadjm_in_var'],
'val_Y_in_var': data_param_dict_in['val_Y_in_var'],
'val_YID_in_var': data_param_dict_in['val_YID_in_var'],
'shuffle_buff_size':data_param_dict_in['shuffle_buff_size'],
'trn_iterator': data_param_dict_in['trn_iterator'],
'val_iterator': data_param_dict_in['val_iterator']
}
return model_param_dict
#model_4
def res_avg_gmodel(self, gat_graph_in):
'''
model_4: Residual graph attention network formed by res-blocks
that has pooling in the last attention layer.
'''
reset_graph()
with gat_graph_in.as_default():
set_graph_seed()
X = tf.placeholder(tf.float32, shape=[None, self.F, self.N], name="X_inp")
AdjM = tf.placeholder(tf.float32, shape=[None, self.N, self.N], name="adja")
y = tf.placeholder(tf.float32, shape=[None, self.cls_num], name="y_label")
yid = tf.placeholder(tf.int32, shape=[None, self.cls_num], name="y_label_id")
data_param_dict_in=self.make_iter()
X, AdjM, y, yid = data_param_dict_in['iterator'].get_next()
alst_hid = []
Wlst_hid = []
blst_hid = []
pwlst_hid=[]
bn=self.blockn
kernel_regularizer=tf.contrib.layers.l2_regularizer(self.reg_scale)
pw_initializer=tf.contrib.layers.xavier_initializer()
with tf.variable_scope("gat_a_and_W", reuse=tf.AUTO_REUSE):
for h in range(1, (self.lay_num-1)*bn+1, bn):
for k in range(0, bn):
if k==0:
K_mult=1
else:
K_mult=self.K_hid[h+k - 1]
alst_hid.append([tf.get_variable("alst_hid" + str(h) +str(k)+str(i), shape=(self.N, self.F_hid[h]), dtype=tf.float32,
initializer=tf.contrib.layers.xavier_initializer()) for i in
range(self.K_hid[h+k])])
if self.enable_pw is True:
pwlst_hid.append([tf.get_variable("pwlst_hid" + str(h)+str(k)+str(i), shape=(self.N, self.N), dtype=tf.float32,
initializer=pw_initializer) for i in range(self.K_hid[h+k])])
Wlst_hid.append([tf.get_variable("Wlst_hid" + str(h)+str(k)+str(i),
shape=(self.F_hid[h+k], K_mult * self.F_hid[h+k - 1]), dtype=tf.float32,
initializer=tf.contrib.layers.xavier_initializer()) for i in
range(self.K_hid[h+k])])
blst_hid.append([tf.get_variable("blst_hid" + str(h) +str(k)+ str(i), shape=(self.F_hid[h+k], self.N), dtype=tf.float32,
initializer=tf.initializers.zeros()) for i in range(self.K_hid[h+k])])
Hlst = []
Hlst.append(X)
pwlst_item = pwlst_hid[0:bn] if self.enable_pw is True else None
Hlst=Hlst + self.avg_K_hidden_out(Hlst[0], AdjM, alst_hid[0:bn], Wlst_hid[0:bn], blst_hid[0:bn],
self.K_hid[1:bn+1], clsfy_layer=False, in_keep_prob=self.trn_in_keep_prob,
eij_keep_prob=self.trn_eij_keep_prob, act_att=tf.nn.leaky_relu, act_hidden=tf.nn.relu, pwlst=pwlst_item,
en_BN=self.enable_bn, en_fin_act=True)
for h in range(bn+1, (self.lay_num-1)*bn+1, bn):
pwlst_item = pwlst_hid[h-1:h-1+bn] if self.enable_pw is True else None
Hlst=Hlst + self.avg_res_K_hidden_out(Hlst[h-1], AdjM, alst_hid[h-1:h-1+bn], Wlst_hid[h-1:h-1+bn], blst_hid[h-1:h-1+bn],
self.K_hid[h:h+bn], clsfy_layer=False, in_keep_prob=self.trn_in_keep_prob,
eij_keep_prob=self.trn_eij_keep_prob, act_att=tf.nn.leaky_relu, act_hidden=tf.nn.relu, pwlst=pwlst_item,
en_BN=self.enable_bn, en_res=True)
Hlst[-1]=tf.nn.relu(Hlst[-1])
flat_H = tf.reshape(Hlst[-1], (-1, Hlst[-1].shape[1] * Hlst[-1].shape[2]))
print('Input shape is:', X.shape)
print('Last output shape is:', Hlst[-1].shape)
print('Flattened last output shape is:', flat_H.shape)
if len(self.dense_hid) > 1:
dense_layers = [tf.layers.dense(inputs=flat_H, units=self.dense_hid[0], activation=tf.nn.relu,
kernel_initializer='glorot_uniform', use_bias=True, bias_initializer='zeros', kernel_regularizer=kernel_regularizer)]
for n in range(1, len(self.dense_hid[:-1])):
dense_layers.append(tf.layers.dense(inputs=dense_layers[n - 1], units=self.dense_hid[n], activation=tf.nn.relu,
kernel_initializer='glorot_uniform', use_bias=True,
bias_initializer='zeros', kernel_regularizer=kernel_regularizer))
dense_layers.append(tf.layers.dense(inputs=dense_layers[-1], units=self.dense_hid[-1], activation=None,
kernel_initializer='glorot_uniform',
use_bias=True, bias_initializer='zeros', kernel_regularizer=kernel_regularizer))