-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver.py
1327 lines (987 loc) · 68 KB
/
solver.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
import numpy as np
import os
import os.path as osp
import time
import datetime
import torch.nn
import torch
import torch.nn.functional as F
from torch.optim.lr_scheduler import ReduceLROnPlateau
from torch.utils.tensorboard import SummaryWriter
from utils import *
from models import Generator, Discriminator, Generator2, Discriminator2, Discriminator_old, Discriminator_old2, Discriminator3, PNA_Net
import torch_geometric.utils as geoutils
import wandb
from torch_geometric.loader import DataLoader
from new_dataloader import DruggenDataset
from new_dataloader_drugs import DruggenDataset_drugs
import torch.utils.data
from torch.nn.parallel import DataParallel as DP
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
import seaborn as sns
class Solver(object):
"""Solver for training and testing DrugGEN."""
def __init__(self, config):
"""Initialize configurations."""
self.dataset_file = config.dataset_file
self.dataset_name = self.dataset_file.split(".")[0]
self.z_dim = config.z_dim
self.num_test_epoch = config.num_test_epoch
# Data loader.
self.device = torch.device("cuda" if torch.cuda.is_available() else 'cpu')
self.batch_size = config.batch_size
self.dataset = DruggenDataset(config.mol_data_dir,self.dataset_file)
self.loader = DataLoader(self.dataset, shuffle=True,batch_size=self.batch_size, drop_last=True, pin_memory=True,num_workers=16)
self.inference_sample_num = config.inference_sample_num
atom_decoders = self.decoder_load("atom_decoders")
bond_decoders = self.decoder_load("bond_decoders")
self.m_dim = len(atom_decoders)
self.b_dim = len(bond_decoders)
self.vertexes = int(self.loader.dataset[0].x.shape[0])
self.drugs = DruggenDataset_drugs(config.drug_data_dir)
self.drugs_loader = DataLoader(self.drugs, shuffle=True,batch_size=self.batch_size, drop_last=True, pin_memory=True,num_workers=16)
drugs_atom_decoders = self.drug_decoder_load("drugs_atom_decoders")
drugs_bond_decoders = self.drug_decoder_load("drugs_bond_decoders")
self.drugs_m_dim = len(drugs_atom_decoders)
self.drugs_b_dim = len(drugs_bond_decoders)
self.drug_vertexes = int(self.drugs_loader.dataset[0].x.shape[0])
# Transformer and Convolution configurations.
self.g_conv_dim = config.g_conv_dim
self.d_conv_dim = config.d_conv_dim
self.lambda_gp = config.lambda_gp
self.post_method = config.post_method
self.dim = config.dim
self.depth = config.depth
self.heads = config.heads
self.mlp_ratio = config.mlp_ratio
self.drop_rate = config.drop_rate
self.metrics = config.metrics
self.vertexes_protein = None
self.edges_protein = None
self.nodes_protein = None
self.dec_depth = config.dec_depth
self.dec_heads = config.dec_heads
self.dis_select = config.dis_select
self.la = config.la
self.la2 = config.la2
self.gcn_depth = config.gcn_depth
# PNA config
self.agg = config.aggregators
self.sca = config.scalers
self.pna_in_ch = config.pna_in_ch
self.pna_out_ch = config.pna_out_ch
self.edge_dim = config.edge_dim
self.towers = config.towers
self.pre_lay = config.pre_lay
self.post_lay = config.post_lay
self.pna_layer_num = config.pna_layer_num
self.graph_add = config.graph_add
# Training configurations.
self.num_iters = config.num_iters
self.num_iters_decay = config.num_iters_decay
self.g_lr = config.g_lr
self.d_lr = config.d_lr
self.g2_lr = config.g2_lr
self.d2_lr = config.d2_lr
self.dropout = config.dropout
self.n_critic = config.n_critic
self.beta1 = config.beta1
self.beta2 = config.beta2
self.resume_iters = config.resume_iters
self.warm_up_steps = config.warm_up_steps
# Test configurations.
self.test_iters = config.test_iters
# Directories.
self.log_dir = config.log_dir
self.sample_dir = config.sample_dir
self.model_save_dir = config.model_save_dir
self.result_dir = config.result_dir
self.degree_dir = config.degree_dir
# Step size.
self.log_step = config.log_step
self.sample_step = config.sample_step
self.model_save_step = config.model_save_step
self.lr_update_step = config.lr_update_step
self.clipping_value = config.clipping_value
# Miscellaneous.
self.mode = config.mode
self.use_tensorboard = config.use_tensorboard
self.noise_strength_0 = torch.nn.Parameter(torch.zeros([]))
self.noise_strength_1 = torch.nn.Parameter(torch.zeros([]))
self.noise_strength_2 = torch.nn.Parameter(torch.zeros([]))
self.noise_strength_3 = torch.nn.Parameter(torch.zeros([]))
self.deg = self._genDegree()
self.init_type = config.init_type
self.build_model()
if self.use_tensorboard:
self.build_tensorboard()
def build_model(self):
self.tra_conv = False
if self.dis_select == "TraConv":
self.tra_conv = True
"""Create generators and discriminators."""
''' Generator is based on Transformer Encoder:
@ g_conv_dim: Dimensions for first MLP layers before Transformer Encoder
@ vertexes: maximum length of generated molecules (atom length)
@ b_dim: number of bond types
@ m_dim: number of atom types (or number of features used)
@ dropout: dropout possibility
@ dim: Hidden dimension of Transformer Encoder
@ depth: Transformer layer number
@ heads: Number of multihead-attention heads
@ mlp_ratio: Read-out layer dimension of Transformer
@ drop_rate: depricated
@ tra_conv: Whether module creates output for TransformerConv discriminator
'''
self.G = Generator(self.g_conv_dim,
self.vertexes,
self.b_dim,
self.m_dim,
self.dropout,
dim=self.dim,
depth=self.depth,
heads=self.heads,
mlp_ratio=self.mlp_ratio,
drop_rate=self.drop_rate)
self.G2 = Generator2(dim=self.dim,
depth=self.dec_depth,
heads=self.dec_heads,
mlp_ratio=self.mlp_ratio,
drop_rate=self.drop_rate,
drugs_m_dim=self.drugs_m_dim,
drugs_b_dim=self.drugs_b_dim,
b_dim = self.b_dim,
m_dim = self.m_dim)
''' Discriminator implementation with PNA:
@ deg: Degree distribution based on used data. (Created with _genDegree() function)
@ agg: aggregators used in PNA
@ sca: scalers used in PNA
@ pna_in_ch: First PNA hidden dimension
@ pna_out_ch: Last PNA hidden dimension
@ edge_dim: Edge hidden dimension
@ towers: Number of towers (Splitting the hidden dimension to multiple parallel processes)
@ pre_lay: Pre-transformation layer
@ post_lay: Post-transformation layer
@ pna_layer_num: number of PNA layers
@ graph_add: global pooling layer selection
'''
self.D_TraConv = Discriminator3(self.dim)
self.D_PNA = Discriminator(self.deg, self.agg,self.sca,self.pna_in_ch,self.pna_out_ch,self.edge_dim,self.towers,
self.pre_lay, self.post_lay, self.pna_layer_num, self.graph_add)
self.D2_PNA = Discriminator2(self.deg, self.agg,self.sca,self.pna_in_ch,self.pna_out_ch,self.edge_dim,self.towers,
self.pre_lay, self.post_lay, self.pna_layer_num, self.graph_add)
self.PNA = PNA_Net(self.deg)
self.D2_TraConv = Discriminator3(self.dim)
self.V_PNA = Discriminator(self.deg, self.agg,self.sca,self.pna_in_ch,self.pna_out_ch,self.edge_dim,self.towers,
self.pre_lay, self.post_lay, self.pna_layer_num, self.graph_add)
self.V2_PNA = Discriminator2(self.deg, self.agg,self.sca,self.pna_in_ch,self.pna_out_ch,self.edge_dim,self.towers,
self.pre_lay, self.post_lay, self.pna_layer_num, self.graph_add)
''' Discriminator implementation with Graph Convolution:
@ d_conv_dim: convolution dimensions for GCN
@ m_dim: number of atom types (or number of features used)
@ b_dim: number of bond types
@ dropout: dropout possibility
'''
self.D = Discriminator_old(self.d_conv_dim, self.m_dim , self.b_dim, self.dropout, self.gcn_depth)
self.D2 = Discriminator_old2(self.d_conv_dim, self.drugs_m_dim , self.drugs_b_dim, self.dropout, self.gcn_depth)
self.V = Discriminator_old(self.d_conv_dim, self.m_dim , self.b_dim, self.dropout, self.gcn_depth)
self.V2 = Discriminator_old2(self.d_conv_dim, self.drugs_m_dim , self.drugs_b_dim, self.dropout, self.gcn_depth)
''' Optimizers for G1, G2, D1, and D2:
Adam Optimizer is used and different beta1 and beta2s are used for GAN1 and GAN2
'''
self.g_optimizer = torch.optim.AdamW(self.G.parameters(),
self.g_lr, [self.beta1, self.beta2])
self.g2_optimizer = torch.optim.AdamW(self.G2.parameters(),
self.g2_lr, [self.beta1, self.beta2])
self.d_optimizer = torch.optim.AdamW(self.D.parameters(), self.d_lr, [self.beta1, self.beta2])
self.d2_optimizer = torch.optim.AdamW(self.D2.parameters(), self.d2_lr, [self.beta1, self.beta2])
self.d_pna_optimizer = torch.optim.AdamW(self.PNA.parameters(), self.d_lr, [self.beta1, self.beta2])
self.d2_pna_optimizer = torch.optim.AdamW(self.D2_PNA.parameters(), self.d2_lr, [self.beta1, self.beta2])
self.d_traconv_optimizer = torch.optim.AdamW(self.D_TraConv.parameters(), self.d_lr, [self.beta1, self.beta2])
self.d2_traconv_optimizer = torch.optim.AdamW(self.D2_TraConv.parameters(), self.d2_lr, [self.beta1, self.beta2])
self.v_optimizer = torch.optim.AdamW(self.V.parameters(), self.d_lr, [self.beta1, self.beta2])
self.v2_optimizer = torch.optim.AdamW(self.V2.parameters(), self.d2_lr, [self.beta1, self.beta2])
''' Learning rate scheduler:
Changes learning rate based on loss.
'''
self.scheduler_g = ReduceLROnPlateau(self.g_optimizer, mode='min', factor=0.5, patience=10, min_lr=0.00001)
self.scheduler_d = ReduceLROnPlateau(self.d_optimizer, mode='min', factor=0.5, patience=10, min_lr=0.00001)
self.scheduler_d_pna = ReduceLROnPlateau(self.d_pna_optimizer, mode='min', factor=0.5, patience=10, min_lr=0.00001)
self.scheduler_d_traconv = ReduceLROnPlateau(self.d_traconv_optimizer, mode='min', factor=0.5, patience=10, min_lr=0.00001)
self.scheduler_v = ReduceLROnPlateau(self.v_optimizer, mode='min', factor=0.5, patience=10, min_lr=0.00001)
self.scheduler_g2 = ReduceLROnPlateau(self.g2_optimizer, mode='min', factor=0.5, patience=10, min_lr=0.00001)
self.scheduler_d2 = ReduceLROnPlateau(self.d2_optimizer, mode='min', factor=0.5, patience=10, min_lr=0.00001)
self.scheduler_d2_pna = ReduceLROnPlateau(self.d2_pna_optimizer, mode='min', factor=0.5, patience=10, min_lr=0.00001)
self.scheduler_d2_traconv = ReduceLROnPlateau(self.d2_traconv_optimizer, mode='min', factor=0.5, patience=10, min_lr=0.00001)
self.scheduler_v2 = ReduceLROnPlateau(self.v2_optimizer, mode='min', factor=0.5, patience=10, min_lr=0.00001)
self.print_network(self.G, 'G')
self.print_network(self.D, 'D')
self.print_network(self.G2, 'G2')
self.print_network(self.D2, 'D2')
self.G.to(self.device)
self.D.to(self.device)
self.PNA.to(self.device)
self.D_TraConv.to(self.device)
self.V.to(self.device)
self.G2.to(self.device)
self.D2.to(self.device)
self.D2_PNA.to(self.device)
self.D2_TraConv.to(self.device)
self.V2.to(self.device)
for p in self.G.parameters():
if p.dim() > 1:
if self.init_type == 'uniform':
torch.nn.init.xavier_uniform_(p)
elif self.init_type == 'normal':
torch.nn.init.xavier_normal_(p)
elif self.init_type == 'random_normal':
torch.nn.init.normal_(p, 0.0, 0.02)
for p in self.G2.parameters():
if p.dim() > 1:
if self.init_type == 'uniform':
torch.nn.init.xavier_uniform_(p)
elif self.init_type == 'normal':
torch.nn.init.xavier_normal_(p)
elif self.init_type == 'random_normal':
torch.nn.init.normal_(p, 0.0, 0.02)
if self.dis_select == "conv":
for p in self.D.parameters():
if p.dim() > 1:
if self.init_type == 'uniform':
torch.nn.init.xavier_uniform_(p)
elif self.init_type == 'normal':
torch.nn.init.xavier_normal_(p)
elif self.init_type == 'random_normal':
torch.nn.init.normal_(p, 0.0, 0.02)
elif self.dis_select == "PNA":
for p in self.D_PNA.parameters():
if p.dim() > 1:
if self.init_type == 'uniform':
torch.nn.init.xavier_uniform_(p)
elif self.init_type == 'normal':
torch.nn.init.xavier_normal_(p)
elif self.init_type == 'random_normal':
torch.nn.init.normal_(p, 0.0, 0.02)
if self.dis_select == "conv":
for p in self.D2.parameters():
if p.dim() > 1:
if self.init_type == 'uniform':
torch.nn.init.xavier_uniform_(p)
elif self.init_type == 'normal':
torch.nn.init.xavier_normal_(p)
elif self.init_type == 'random_normal':
torch.nn.init.normal_(p, 0.0, 0.02)
elif self.dis_select == "PNA":
for p in self.D2_PNA.parameters():
if p.dim() > 1:
if self.init_type == 'uniform':
torch.nn.init.xavier_uniform_(p)
elif self.init_type == 'normal':
torch.nn.init.xavier_normal_(p)
elif self.init_type == 'random_normal':
torch.nn.init.normal_(p, 0.0, 0.02)
def decoder_load(self, dictionary_name):
''' Loading the atom and bond decoders'''
with open("DrugGEN/data/" + dictionary_name + self.dataset_name + '.pkl', 'rb') as f:
return pickle.load(f)
def drug_decoder_load(self, dictionary_name):
''' Loading the atom and bond decoders'''
with open("DrugGEN/data/" + dictionary_name +'.pkl', 'rb') as f:
return pickle.load(f)
def _genDegree(self):
''' Generates the Degree distribution tensor for PNA, should be used everytime a different
dataset is used.
Can be called without arguments and saves the tensor for later use. If tensor was created
before, it just loads the degree tensor.
'''
degree_path = os.path.join(self.degree_dir, self.dataset_name + '-degree.pt')
if not os.path.exists(degree_path):
max_degree = -1
for data in self.dataset:
d = geoutils.degree(data.edge_index[1], num_nodes=data.num_nodes, dtype=torch.long)
max_degree = max(max_degree, int(d.max()))
# Compute the in-degree histogram tensor
deg = torch.zeros(max_degree + 1, dtype=torch.long)
for data in self.dataset:
d = geoutils.degree(data.edge_index[1], num_nodes=data.num_nodes, dtype=torch.long)
deg += torch.bincount(d, minlength=deg.numel())
torch.save(deg, 'DrugGEN/data/' + self.dataset_name + '-degree.pt')
else:
deg = torch.load(degree_path, map_location=lambda storage, loc: storage)
return deg
def print_network(self, model, name):
"""Print out the network information."""
num_params = 0
for p in model.parameters():
num_params += p.numel()
print(model)
print(name)
print("The number of parameters: {}".format(num_params))
def restore_model(self, resume_iters):
"""Restore the trained generator and discriminator."""
print('Loading the trained models from step {}...'.format(resume_iters))
G_path = os.path.join(self.model_directory, '{}-G.ckpt'.format(resume_iters))
D_path = os.path.join(self.model_directory, '{}-D.ckpt'.format(resume_iters))
V_path = os.path.join(self.model_directory, '{}-V.ckpt'.format(resume_iters))
self.G.load_state_dict(torch.load(G_path, map_location=lambda storage, loc: storage))
self.D.load_state_dict(torch.load(D_path, map_location=lambda storage, loc: storage))
self.V.load_state_dict(torch.load(V_path, map_location=lambda storage, loc: storage))
G2_path = os.path.join(self.model_directory, '{}-G2.ckpt'.format(resume_iters))
D2_path = os.path.join(self.model_directory, '{}-D2.ckpt'.format(resume_iters))
V2_path = os.path.join(self.model_directory, '{}-V2.ckpt'.format(resume_iters))
self.G2.load_state_dict(torch.load(G2_path, map_location=lambda storage, loc: storage))
self.D2.load_state_dict(torch.load(D2_path, map_location=lambda storage, loc: storage))
self.V2.load_state_dict(torch.load(V2_path, map_location=lambda storage, loc: storage))
def update_lr(self, g_lr, d_lr, g2_lr, d2_lr):
"""Decay learning rates of the generator and discriminator."""
for param_group in self.g_optimizer.param_groups:
param_group['lr'] = g_lr
for param_group in self.g2_optimizer.param_groups:
param_group['lr'] = g2_lr
if self.dis_select == "conv":
for param_group in self.d_optimizer.param_groups:
param_group['lr'] = d_lr
for param_group in self.d2_optimizer.param_groups:
param_group['lr'] = d2_lr
elif self.dis_select == "PNA":
for param_group in self.d_pna_optimizer.param_groups:
param_group['lr'] = d_lr
for param_group in self.d2_pna_optimizer.param_groups:
param_group['lr'] = d2_lr
elif self.dis_select == "TraConv":
for param_group in self.d_traconv_optimizer.param_groups:
param_group['lr'] = d_lr
for param_group in self.d2_traconv_optimizer.param_groups:
param_group['lr'] = d2_lr
def reset_grad(self):
"""Reset the gradient buffers."""
self.g_optimizer.zero_grad()
self.g2_optimizer.zero_grad()
self.d_optimizer.zero_grad()
self.d2_optimizer.zero_grad()
if self.dis_select == "PNA":
self.d_pna_optimizer.zero_grad()
self.d2_pna_optimizer.zero_grad()
elif self.dis_select == "TraConv":
self.d_traconv_optimizer.zero_grad()
self.d2_traconv_optimizer.zero_grad()
def denorm(self, x):
"""Convert the range from [-1, 1] to [0, 1]."""
out = (x + 1) / 2
return out.clamp_(0, 1)
def gradient_penalty(self, y, x):
"""Compute gradient penalty: (L2_norm(dy/dx) - 1)**2."""
weight = torch.ones(y.size(),requires_grad=False).to(self.device)
dydx = torch.autograd.grad(outputs=y,
inputs=x,
grad_outputs=weight,
retain_graph=True,
create_graph=True,
only_inputs=True)[0]
dydx = dydx.view(dydx.size(0), -1)
gradient_penalty = ((dydx.norm(2, dim=1) - 1) ** 2).mean()
return gradient_penalty
def label2onehot(self, labels, dim):
"""Convert label indices to one-hot vectors."""
out = torch.zeros(list(labels.size())+[dim]).to(self.device)
out.scatter_(len(out.size())-1,labels.unsqueeze(-1),1.)
return out.float()
def sample_z_node(self, batch_size):
''' Random noise for nodes logits. '''
return np.random.normal(0,1, size=(batch_size,self.vertexes, self.m_dim)) # 128, 9, 5
def sample_z_edge(self, batch_size):
''' Random noise for edges logits. '''
return np.random.normal(0,1, size=(batch_size,self.vertexes,self.vertexes,self.b_dim)) # 128, 9, 9, 5
def postprocess(self, inputs, post_method, temperature=1.,dimension=-1):
if post_method == 'soft_gumbel':
softmax = F.gumbel_softmax(inputs
/ temperature, hard=False, dim = dimension)
elif post_method == 'hard_gumbel':
softmax = F.gumbel_softmax(inputs
/ temperature, hard=True, dim = dimension)
elif post_method == 'softmax':
softmax = F.softmax(inputs / temperature, dim = dimension)
return softmax
def reward(self, mols):
''' Rewards that can be used for Reinforcement Networks. '''
rr = 1.
for m in ('logp,sas,qed,unique' if self.metrics == 'all' else self.metrics).split(','):
if m == 'np':
rr *= MolecularMetrics.natural_product_scores(mols, norm=True)
elif m == 'logp':
rr *= MolecularMetrics.water_octanol_partition_coefficient_scores(mols, norm=True)
elif m == 'sas':
rr *= MolecularMetrics.synthetic_accessibility_score_scores(mols, norm=True)
elif m == 'qed':
rr *= MolecularMetrics.quantitative_estimation_druglikeness_scores(mols, norm=True)
elif m == 'novelty':
rr *= MolecularMetrics.novel_scores(mols, self.data)
elif m == 'dc':
rr *= MolecularMetrics.drugcandidate_scores(mols, self.data)
elif m == 'unique':
rr *= MolecularMetrics.unique_scores(mols)
elif m == 'diversity':
rr *= MolecularMetrics.diversity_scores(mols, self.data)
elif m == 'validity':
rr *= MolecularMetrics.valid_total_score(mols)
else:
raise RuntimeError('{} is not defined as a metric'.format(m))
return rr.reshape(-1, 1)
def dense_to_sparse_with_attr(self, adj):
###
assert adj.dim() >= 2 and adj.dim() <= 3
assert adj.size(-1) == adj.size(-2)
index = adj.nonzero(as_tuple=True)
edge_attr = adj[index]
if len(index) == 3:
batch = index[0] * adj.size(-1)
index = (batch + index[1], batch + index[2])
index = torch.stack(index, dim=0)
return index, edge_attr
def plot_grad_flow(self, named_parameters, model, iter, epoch):
# Based on https://discuss.pytorch.org/t/check-gradient-flow-in-network/15063/10
'''Plots the gradients flowing through different layers in the net during training.
Can be used for checking for possible gradient vanishing / exploding problems.
Usage: Plug this function in Trainer class after loss.backwards() as
"plot_grad_flow(self.model.named_parameters())" to visualize the gradient flow'''
ave_grads = []
max_grads= []
layers = []
for n, p in named_parameters:
if(p.requires_grad) and ("bias" not in n):
#print(p.grad,n)
layers.append(n)
ave_grads.append(p.grad.abs().mean().cpu())
max_grads.append(p.grad.abs().max().cpu())
plt.bar(np.arange(len(max_grads)), max_grads, alpha=0.1, lw=1, color="c")
plt.bar(np.arange(len(max_grads)), ave_grads, alpha=0.1, lw=1, color="b")
plt.hlines(0, 0, len(ave_grads)+1, lw=2, color="k" )
plt.xticks(range(0,len(ave_grads), 1), layers, rotation="vertical")
plt.xlim(left=0, right=len(ave_grads))
plt.ylim(bottom = -0.001, top=1) # zoom in on the lower gradient regions
plt.xlabel("Layers")
plt.ylabel("average gradient")
plt.title("Gradient flow")
plt.grid(True)
plt.legend([Line2D([0], [0], color="c", lw=4),
Line2D([0], [0], color="b", lw=4),
Line2D([0], [0], color="k", lw=4)], ['max-gradient', 'mean-gradient', 'zero-gradient'])
pltsavedir = "/home/atabey/gradients/tryout"
plt.savefig(os.path.join(pltsavedir, "weights_" + model + "_" + self.dataset_name + "_" + str(iter) + "_" + str(epoch) + ".png"), dpi= 500,bbox_inches='tight')
def plot_attn(self, attn_w, model, iter, epoch):
cols = 4
rows = int(self.heads/cols)
fig, axes = plt.subplots( rows,cols, figsize = (30, 14))
axes = axes.flat
attentions_pos = attn_w[0]
attentions_pos = attentions_pos.cpu().detach().numpy()
for i,att in enumerate(attentions_pos):
#im = axes[i].imshow(att, cmap='gray')
sns.heatmap(att,vmin = 0, vmax = 1,ax = axes[i])
axes[i].set_title(f'head - {i} ')
axes[i].set_ylabel('layers')
pltsavedir = "/home/atabey/attn/second"
plt.savefig(os.path.join(pltsavedir, "attn" + model + "_" + self.dataset_name + "_" + str(iter) + "_" + str(epoch) + ".png"), dpi= 500,bbox_inches='tight')
def train(self):
''' Training Script starts from here'''
wandb.config = {'beta1': 0.9}
wandb.init(project="druggen", entity="atabeyunlu")
# Defining sampling paths and creating logger
bce_loss = torch.nn.BCELoss()
self.arguments = "glr{}_dlr{}_g2lr{}_d2lr{}_dim{}_depth{}_heads{}_decdepth{}_decheads{}_ncritic{}_batch{}_epoch{}_warmup{}_dataset{}_disc-{}_la{}_dropout{}".format(self.g_lr,self.d_lr,self.g2_lr,self.d2_lr,self.dim,self.depth,self.heads,self.dec_depth,self.dec_heads,self.n_critic,self.batch_size,self.num_iters,self.warm_up_steps,self.dataset_name,self.dis_select,self.la,self.dropout)
writer = SummaryWriter(log_dir=os.path.join(self.result_dir, self.arguments))
self.model_directory= os.path.join(self.model_save_dir,self.arguments)
self.sample_directory=os.path.join(self.sample_dir,self.arguments)
log_path = os.path.join(self.log_dir, "{}.txt".format(self.arguments))
if not os.path.exists(self.model_directory):
os.makedirs(self.model_directory)
if not os.path.exists(self.sample_directory):
os.makedirs(self.sample_directory)
# Learning rate cache for decaying.
g_lr = self.g_lr
d_lr = self.d_lr
g2_lr = self.g2_lr
d2_lr = self.d2_lr
# protein data
akt1_human_adj = torch.load("DrugGEN/data/akt/AKT1_human_adj.pt")
akt1_human_annot = torch.load("DrugGEN/data/akt/AKT1_human_annot.pt")
# Start training.
print('Start training...')
start_time = time.time()
for idx in range(self.num_iters):
# =================================================================================== #
# 1. Preprocess input data #
# =================================================================================== #
# Load the data
dataloader_iterator = iter(self.drugs_loader)
for i, data in enumerate(self.loader):
try:
drugs = next(dataloader_iterator)
except StopIteration:
dataloader_iterator = iter(self.drugs_loader)
drugs = next(dataloader_iterator)
# Preprocess both dataset
data = data.to(self.device)
drugs = drugs.to(self.device)
z_e = self.sample_z_edge(self.batch_size) # (batch,max_len,max_len)
z_n = self.sample_z_node(self.batch_size) # (batch,max_len)
z_edge = torch.from_numpy(z_e).to(self.device).float().requires_grad_(True) # Edge noise.(batch,max_len,max_len)
z_node = torch.from_numpy(z_n).to(self.device).float().requires_grad_(True) # Node noise.(batch,max_len)
a = geoutils.to_dense_adj(edge_index = data.edge_index,batch=data.batch,edge_attr=data.edge_attr, max_num_nodes=int(data.batch.shape[0]/self.batch_size))
x = data.x.view(-1,int(data.batch.shape[0]/self.batch_size))
a_tensor = self.label2onehot(a, self.b_dim)
x_tensor = self.label2onehot(x, self.m_dim)
a_tensor = a_tensor + torch.randn([a_tensor.size(0), a_tensor.size(1), a_tensor.size(2),1], device=a_tensor.device) * self.noise_strength_0
x_tensor = x_tensor + torch.randn([x_tensor.size(0), x_tensor.size(1),1], device=x_tensor.device) * self.noise_strength_1
drugs_a = geoutils.to_dense_adj(edge_index = drugs.edge_index,batch=drugs.batch,edge_attr=drugs.edge_attr, max_num_nodes=int(drugs.batch.shape[0]/self.batch_size))
drugs_x = drugs.x.view(-1,int(drugs.batch.shape[0]/self.batch_size))
drugs_a = drugs_a.to(self.device).long()
drugs_x = drugs_x.to(self.device).long()
drugs_a_tensor = self.label2onehot(drugs_a, self.drugs_b_dim)
drugs_x_tensor = self.label2onehot(drugs_x, self.drugs_m_dim)
drugs_a_tensor = drugs_a_tensor + torch.randn([drugs_a_tensor.size(0), drugs_a_tensor.size(1), drugs_a_tensor.size(2),1], device=drugs_a_tensor.device) * self.noise_strength_2
drugs_x_tensor = drugs_x_tensor + torch.randn([drugs_x_tensor.size(0), drugs_x_tensor.size(1),1], device=drugs_x_tensor.device) * self.noise_strength_3
prot_n = akt1_human_annot[None,:].to(self.device).float()
prot_e = akt1_human_adj[None,None,:].view(1,546,546,1).to(self.device).float()
y_real = torch.autograd.Variable(torch.ones(self.batch_size, 1)).to(self.device)
y_fake = torch.autograd.Variable(torch.zeros(self.batch_size, 1)).to(self.device)
y_fake_value = torch.autograd.Variable(torch.zeros(self.batch_size, 1)).to(self.device)
# =================================================================================== #
# 2. Train the discriminator #
# =================================================================================== #
self.reset_grad()
# Compute loss with real molecules.
if self.dis_select == "conv":
logits_real, features_real = self.D(a_tensor, None, x_tensor)
elif self.dis_select == "PNA":
#logits_real = self.PNA(data.x, data.edge_index.float(), data.edge_attr, data.batch)
logits_real = self.PNA(x_tensor.to(self.device).float() , a.to(self.device).float() ,mask=None)
prediction_real = - torch.mean(logits_real)
#d_loss_real = bce_loss(torch.sigmoid(logits_real), y_real)
# Compute loss with fake molecules.
edges_logits, nodes_logits, attn= self.G(z_edge,z_node,a,a_tensor,x_tensor)
#edges_hat = edges_logits.view(-1,self.b_dim,self.vertexes,self.vertexes)
edges_hat = self.postprocess(edges_logits, "soft_gumbel")
nodes_hat = self.postprocess(nodes_logits, "soft_gumbel")
edges_hat_sample, nodes_hat_sample = torch.max(edges_hat, -1)[1], torch.max(nodes_hat, -1)[1]
features_hat = None
#fake_edge_index, fake_edge_attr = self.dense_to_sparse_with_attr(edges_hat_sample)
#nodes_fake = nodes_hat_sample.view(-1,1)
#edges_hat = edges_hat.view(-1, self.vertexes,self.vertexes,self.b_dim)
if self.dis_select == "conv":
logits_fake, features_fake = self.D(edges_hat, None, nodes_hat)
elif self.dis_select == "PNA":
#logits_fake = self.PNA(nodes_fake, fake_edge_index, fake_edge_attr, data.batch)
logits_fake = self.PNA(nodes_hat, edges_hat_sample.float(),mask=None)
prediction_fake = torch.mean(logits_fake)
#d_loss_fake = bce_loss(torch.sigmoid(logits_fake),y_fake)
# Compute gradient loss.
eps = torch.rand(logits_real.size(0),1,1,1).to(self.device)
x_int0 = (eps * a_tensor + (1. - eps) * edges_hat).requires_grad_(True)
x_int1 = (eps.squeeze(-1) * x_tensor + (1. - eps.squeeze(-1)) * nodes_hat).requires_grad_(True)
grad0, grad1 = self.D(x_int0, None, x_int1,torch.sigmoid)
d_loss_gp = self.gradient_penalty(grad0, x_int0) + self.gradient_penalty(grad1, x_int1)
# Calculate total loss
d_loss = prediction_fake + prediction_real + d_loss_gp * self.lambda_gp
# Feed the loss
d_loss.backward()
if self.dis_select == "conv":
torch.nn.utils.clip_grad_norm_(self.D.parameters(), self.clipping_value)
#elif self.dis_select == "PNA":
#torch.nn.utils.clip_grad_norm_(self.D_PNA.parameters(), self.clipping_value)
#elif self.dis_select == "TraConv":
#torch.nn.utils.clip_grad_norm_(self.D_TraConv.parameters(), self.clipping_value)
#if (i+1) % 100 == 0:
# if self.dis_select == "conv":
# self.plot_grad_flow(self.D.named_parameters(),"D",i+1,idx)
# elif self.dis_select == "PNA":
# self.plot_grad_flow(self.PNA.named_parameters(),"D",i+1,idx)
#elif self.dis_select == "TraConv":
#self.plot_grad_flow(self.D_TraConv.named_parameters(),"D",i+1,idx)
#d_loss.backward(retain_graph=True)
if self.dis_select == "conv":
self.d_optimizer.step()
self.scheduler_d.step(d_loss)
elif self.dis_select == "PNA":
self.d_pna_optimizer.step()
self.scheduler_d_pna.step(d_loss)
elif self.dis_select == "TraConv":
self.d_traconv_optimizer.step()
self.scheduler_d_traconv.step(d_loss)
# Logging.
loss = {}
loss['D/d_loss_real'] = prediction_fake.item()
loss['D/d_loss_fake'] = prediction_fake.item()
#loss['D/loss_gp'] = d_loss_gp.item()
loss["D/d_loss"] = d_loss.item()
wandb.log({"d_loss_fake": prediction_fake, "d_loss_real": prediction_fake, "d_loss": d_loss, "iteration/epoch":[i,idx]})
# =================================================================================== #
# 3. Train the generator #
# =================================================================================== #
if (i+1) % self.n_critic == 0:
self.reset_grad()
# Generate fake molecules.
g_edges_logits, g_nodes_logits, g_attn = self.G(z_edge,z_node,a,a_tensor,x_tensor)
# Postprocess with Gumbel softmax
#g_edges_hat = g_edges_logits.view(-1,self.b_dim,self.vertexes,self.vertexes)
g_edges_hat = self.postprocess(g_edges_logits, "soft_gumbel")
g_nodes_hat = self.postprocess(g_nodes_logits, "soft_gumbel")
g_edges_hat_sample, g_nodes_hat_sample = torch.max(g_edges_hat, -1)[1], torch.max(g_nodes_hat, -1)[1]
features_hat = None
#g_fake_edge_index, g_fake_edge_attr = self.dense_to_sparse_with_attr(g_edges_hat)
g_nodes_fake = g_nodes_hat_sample.view(-1,1)
if (i+1) % (self.log_step) == 0:
fake_mol = [self.dataset.matrices2mol(n_.data.cpu().numpy(), e_.data.cpu().numpy(), strict=True)
for e_, n_ in zip(g_edges_hat_sample, g_nodes_hat_sample)]
mols = [self.dataset.matrices2mol(n_.data.cpu().numpy(), e_.data.cpu().numpy(), strict=False)
for e_, n_ in zip(a, x)]
#g_edges_hat = g_edges_hat.view(-1, self.vertexes,self.vertexes,self.b_dim)
# Compute loss with fake molecules.
if self.dis_select == "conv":
g_logits_fake, g_features_fake = self.D(g_edges_hat, None, g_nodes_hat)
elif self.dis_select == "PNA":
#g_logits_fake = self.PNA(g_nodes_fake, g_fake_edge_index, g_fake_edge_attr, data.batch)
g_logits_fake = self.PNA(g_nodes_hat, edges_hat_sample.float(),mask=None)
#elif self.dis_select == "TraConv":
#g_logits_fake = self.D_TraConv(g_nodes_for_traconv, g_fake_edge_index, g_attr_for_traconv, data.batch)
g_prediction_fake = - torch.mean(g_logits_fake)
#g_loss_fake = bce_loss(torch.sigmoid(g_logits_fake),y_real)
# Real Reward
#rewardR = torch.from_numpy(self.reward(mols)).to(self.device)
# Fake Reward
#rewardF = torch.from_numpy(self.reward(fake_mol)).to(self.device)
# Reinforcement Loss
#value_logit_real,_ = self.V(a_tensor, None, x_tensor,torch.sigmoid)
#value_logit_fake,_ = self.V(g_edges_hat, None, g_nodes_hat, torch.sigmoid)
#g_loss_value_pred = torch.mean((value_logit_real - rewardR) ** 2 + (value_logit_fake - rewardF) ** 2)
#g_loss_value_pred = (1 - rewardF) ** 2
#g_loss_value = bce_loss(torch.sigmoid(g_loss_value_pred),y_fake_value)
# Clone edge and node logits for GAN2
# Backward and optimize.
g_loss = g_prediction_fake # + (1. - self.la) * g_loss_value_pred
g_loss.backward()
torch.nn.utils.clip_grad_norm_(self.G.parameters(), self.clipping_value)
#torch.nn.utils.clip_grad_norm_(self.V.parameters(), self.clipping_value)
#if (i+1) % 100 == 0:
# self.plot_grad_flow(self.G.named_parameters(),"G",i+1,idx)
# print(g_edges_hat_sample[0], g_nodes_hat_sample[0])
#self.plot_attn(g_attn,"G",i+1,idx)
#self.plot_grad_flow(self.V.named_parameters(),"V",i+1,idx)
#self.plot_grad_flow(self.V.named_parameters())
#g_loss.backward(retain_graph=True)
self.g_optimizer.step()
#self.v_optimizer.step()
self.scheduler_g.step(g_loss)
#self.scheduler_v.step(g_loss)
# Logging.
loss['G/g_loss_fake'] = g_prediction_fake.item()
#loss['G/g_loss_value'] = g_loss_value_pred.item()
loss["G/g_loss"] = g_loss.item()
wandb.log({ "g_loss": g_loss, "iteration/epoch":[i,idx]})
g_edges_logits_forGAN2, g_nodes_logits_forGAN2 = g_edges_logits.detach().clone(), g_nodes_logits.detach().clone()
loss2 = {}
if (idx+1 > self.warm_up_steps) & (i+1 > 10):
# =================================================================================== #
# 4. Train the discriminator - 2 #
# =================================================================================== #
tra_edges_logits, tra_nodes_logits, dec_attn = self.G2(g_edges_logits_forGAN2, g_nodes_logits_forGAN2,prot_n,prot_e)
tra_edges_hat = self.postprocess(tra_edges_logits, "soft_gumbel")
tra_nodes_hat = self.postprocess(tra_nodes_logits, "soft_gumbel")
if self.dis_select == "conv":
logits_fake2, features_fake2 = self.D2(tra_edges_hat, None, tra_nodes_hat)
d2_loss_fake = torch.mean(logits_fake2)
if self.dis_select == "conv":
logits_real2, features_real2 = self.D2(drugs_a_tensor, None,drugs_x_tensor)
elif self.dis_select == "PNA":
logits_real2 = self.D2_PNA(drugs.x, drugs.edge_index, drugs.edge_attr, drugs.batch)
d2_loss_real = - torch.mean(logits_real2)
pad_adj_shape = drugs_a_tensor.shape[1] - tra_edges_hat.shape[1]
pad_x_features_shape = drugs_x_tensor.shape[-1] - tra_nodes_hat.shape[-1]
pad_adj = (0, 0, 0,pad_adj_shape,0,pad_adj_shape)
pad_x = ( 0,pad_x_features_shape,0,pad_adj_shape)
tra_edges_hats = F.pad(tra_edges_hat, pad_adj)
tra_nodes_hats = F.pad(tra_nodes_hat, pad_x)
eps = torch.rand(logits_real.size(0),1,1,1).to(self.device)
x_int0 = (eps * drugs_a_tensor + (1. - eps) * tra_edges_hats).requires_grad_(True)
x_int1 = (eps.squeeze(-1) * drugs_x_tensor + (1. - eps.squeeze(-1)) * tra_nodes_hats).requires_grad_(True)
grad0, grad1 = self.D2(x_int0, None, x_int1,torch.sigmoid)
d2_loss_gp = self.gradient_penalty(grad0, x_int0) + self.gradient_penalty(grad1, x_int1)
d2_loss = d2_loss_fake + d2_loss_real + d2_loss_gp * self.lambda_gp
self.reset_grad()
d2_loss.backward(retain_graph=True)
torch.nn.utils.clip_grad_norm_(self.D2.parameters(), self.clipping_value)
if self.dis_select == "conv":
self.d2_optimizer.step()
self.scheduler_d2.step(d2_loss)
elif self.dis_select == "PNA":
self.d2_pna_optimizer.step()
self.scheduler_d2_pna.step(d2_loss)
elif self.dis_select == "TraConv":
self.d2_traconv_optimizer.step()
self.scheduler_d2_traconv.step(d2_loss)
loss2['D2/d2_loss_real'] = d2_loss_real.item()
loss2['D2/d2_loss_fake'] = d2_loss_fake.item()
#loss['D2/loss_gp'] = d2_loss_gp_tra.item()
loss2["D2/d2_loss"] = d2_loss.item()
wandb.log({"d2_loss_fake": d2_loss_fake, "d2_loss_real": d2_loss_real, "d2_loss": d2_loss, "iteration/epoch":[i,idx]})
# =================================================================================== #
# 5. Train the generator - 2 #
# =================================================================================== #
if ((idx+1 > self.warm_up_steps) & ((i+1) % self.n_critic == 0)):
g_tra_edges_logits, g_tra_nodes_logits, g_dec_attn= self.G2(g_edges_logits_forGAN2, g_nodes_logits_forGAN2,prot_n,prot_e)
g_tra_edges_hat = self.postprocess(g_tra_edges_logits, "soft_gumbel")
g_tra_nodes_hat = self.postprocess(g_tra_nodes_logits, "soft_gumbel")
g_tra_edges_hard, g_tra_nodes_hard = torch.max(g_tra_edges_hat, -1)[1], torch.max(g_tra_nodes_hat, -1)[1]
if self.dis_select == "conv":
g_tra_logits_fake2, g_tra_features_fake2 = self.D2(g_tra_edges_hat, None, g_tra_nodes_hat)