-
Notifications
You must be signed in to change notification settings - Fork 3
/
train_herg.py
179 lines (149 loc) · 8.06 KB
/
train_herg.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
import numpy as np
from sklearn.utils.random import sample_without_replacement
from sklearn.metrics import auc, precision_recall_curve, roc_curve
from sklearn.svm import OneClassSVM
import argparse
import networkx as nx
from GCN_embedding import *
import torch
import torch.nn as nn
import time
import GCN_embedding
from torch.autograd import Variable
from graph_sampler import GraphSampler
from numpy.random import seed
import random
import copy
import torch.nn.functional as F
from matplotlib import cm
from tdc.utils import retrieve_label_name_list
from sklearn.model_selection import train_test_split
from sklearn.model_selection import StratifiedKFold
from tdc.single_pred import Tox
from pysmiles import read_smiles
from loss import *
def arg_parse():
parser = argparse.ArgumentParser(description='HimNet Arguments.')
parser.add_argument('--datadir', dest='datadir', default ='dataset', help='Directory where benchmark is located')
parser.add_argument('--DS', dest='DS', default ='hERG', help='dataset name')
parser.add_argument('--max-nodes', dest='max_nodes', type=int, default=0, help='Maximum number of nodes (ignore graghs with nodes exceeding the number.')
parser.add_argument('--clip', dest='clip', default=0.1, type=float, help='Gradient clipping.')
parser.add_argument('--batch-size', dest='batch_size', default=300, type=int, help='Batch size.')
parser.add_argument('--hidden-dim', dest='hidden_dim', default=512, type=int, help='Hidden dimension')
parser.add_argument('--output-dim', dest='output_dim', default=256, type=int, help='Output dimension')
parser.add_argument('--num-gc-layers', dest='num_gc_layers', default=3, type=int, help='Number of graph convolution layers before each pooling')
parser.add_argument('--nodemem-num', dest='mem_num_node', default=3, type=int, help='Node Memory blocks')
parser.add_argument('--graphmem-num', dest='mem_num_graph', default=1, type=int, help='Graph Memory blocks')
parser.add_argument('--nobn', dest='bn', action='store_const', const=False, default=True, help='Whether batch normalization is used')
parser.add_argument('--dropout', dest='dropout', default=0.3, type=float, help='Dropout rate.')
parser.add_argument('--nobias', dest='bias', action='store_const', const=False, default=False, help='Whether to add bias. Default to True.')
parser.add_argument('--lr', dest='lr', default= 0.01, type=float, help='Learning Rate')
parser.add_argument('--epoch', dest='epoch', default=90, type=int, help='total epoch number')
parser.add_argument('--feature', dest='feature', default='deg-num', help='use what node feature')
parser.add_argument('--alpha', dest='alpha', default= 0.01, type=float, help='weight parameter')
parser.add_argument('--seed', dest='seed', type=int, default=0, help='seed')
return parser.parse_args()
def setup_seed(seed):
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True
np.random.seed(seed)
random.seed(seed)
def train(dataset, data_test_loader, model, k, args):
optimizer = torch.optim.Adam(model.parameters(), lr = args.lr)
tr_entropy_loss_func = EntropyLoss()
auroc_final = []
for epoch in range(args.epoch):
model.train()
loss_epoch = 0
num_train = 0
for batch_idx, data in enumerate(dataset):
optimizer.zero_grad()
adj = Variable(data['adj'].float(), requires_grad=False).cuda()
h0 = Variable(data['feats'].float(), requires_grad=False).cuda()
adj_label = Variable(data['adj_label'].float(), requires_grad=False).cuda()
recon_node, recon_adj, att_node, att_graph, graph_embed, recon_graph_embed = model(h0, adj)
loss_recon_adj, loss_recon_node = loss_func(adj_label, recon_adj, h0, recon_node)
entropy_loss_node = tr_entropy_loss_func(att_node)
entropy_loss_graph = tr_entropy_loss_func(att_graph)
graph_embed_loss = graphembloss(graph_embed, recon_graph_embed)
loss = loss_recon_adj.mean() + loss_recon_node.mean() + graph_embed_loss.mean() + args.alpha*entropy_loss_node + args.alpha*entropy_loss_graph
loss_epoch += loss.item() * adj.shape[0]
num_train += adj.shape[0]
loss.backward()
optimizer.step()
print("Epoch: %d Train AE Loss: %f" % (epoch+1, loss_epoch / num_train))
if (epoch+1)%args.epoch == 0:
model.eval()
loss = []
y=[]
for batch_idx, data in enumerate(data_test_loader):
adj = Variable(data['adj'].float(), requires_grad=False).cuda()
h0 = Variable(data['feats'].float(), requires_grad=False).cuda()
adj_label = Variable(data['adj_label'].float(), requires_grad=False).cuda()
recon_node, recon_adj, _, _, graph_embed, recon_graph_embed = model(h0, adj)
loss_recon_adj, loss_recon_node = loss_func(adj_label, recon_adj, h0, recon_node)
graph_embed_loss = graphembloss(graph_embed, recon_graph_embed)
lossall = loss_recon_adj + loss_recon_node + graph_embed_loss
loss_ = lossall
loss_ = np.array(loss_.cpu().detach())
loss.append(loss_)
if data['label'] == 0:
y.append(1)
else:
y.append(0)
label_test = []
for loss_ in loss:
label_test.append(loss_)
label_test = np.array(label_test)
fpr_ab, tpr_ab, _ = roc_curve(y, label_test)
test_roc_ab = auc(fpr_ab, tpr_ab)
auroc_final.append(test_roc_ab)
print('Epoch: {} Abnormal Detection: auroc_ab: {}'.format(epoch+1, test_roc_ab))
if epoch == (args.epoch-1):
auroc_final = test_roc_ab
return auroc_final
if __name__ == '__main__':
args = arg_parse()
DS = args.DS
setup_seed(args.seed)
data = Tox(name = DS)
df = data.get_data()
data_=df.values
smilesstr=[]
graph_label=[]
for i in range(data_.shape[0]):
smilesstr.append(data_[i,1])
graph_label.append(data_[i,2])
graphs=[]
for data in smilesstr:
graphs.append(read_smiles(data))
max_nodes_num = max([G.number_of_nodes() for G in graphs])
datanum = len(graphs)
print(datanum)
for idx, graph in enumerate(graphs):
graph.graph['label'] = graph_label[idx]
kfd=StratifiedKFold(n_splits=5, random_state=args.seed, shuffle = True)
result_auc=[]
for k, (train_index,test_index) in enumerate(kfd.split(graphs, graph_label)):
graphs_train_ = [graphs[i] for i in train_index]
graphs_test = [graphs[i] for i in test_index]
graphs_train = []
for graph in graphs_train_:
if graph.graph['label'] == 1:
graphs_train.append(graph)
num_train = len(graphs_train)
num_test = len(graphs_test)
print(num_train, num_test)
dataset_sampler_train = GraphSampler(graphs_train, features=args.feature, normalize=False, max_num_nodes=max_nodes_num)
model = GNNet(dataset_sampler_train.feat_dim, args.hidden_dim, args.output_dim, args.num_gc_layers, args.mem_num_node, args.mem_num_graph, max_nodes_num, args=args).cuda()
data_train_loader = torch.utils.data.DataLoader(dataset_sampler_train, shuffle=True, batch_size=args.batch_size)
dataset_sampler_test = GraphSampler(graphs_test, features=args.feature, normalize=False, max_num_nodes=max_nodes_num)
data_test_loader = torch.utils.data.DataLoader(dataset_sampler_test, shuffle=False, batch_size=1)
results = train(data_train_loader, data_test_loader, model, k, args)
result_auc.append(results)
result_auc = np.array(result_auc)
auc_avg = np.mean(result_auc)
auc_std = np.std(result_auc)
print(' auroc {}, average: {}, std: {}'.format(result_auc, auc_avg, auc_std))