-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.py
executable file
·230 lines (191 loc) · 7.94 KB
/
main.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
import sys
import os
import torch
import random
import numpy as np
from tqdm import tqdm
import torch.nn as nn
import torch.optim as optim
import math
from DGCNN_embedding import DGCNN
from mlp_dropout import MLPClassifier
from sklearn import metrics
from embedding import EmbedMeanField, EmbedLoopyBP
from util import cmd_args, load_data
sys.path.append(
'%s/pytorch_structure2vec-master/s2v_lib' % os.path.dirname(
os.path.realpath(__file__)))
class Classifier(nn.Module):
def __init__(self):
super(Classifier, self).__init__()
if cmd_args.gm == 'mean_field':
model = EmbedMeanField
elif cmd_args.gm == 'loopy_bp':
model = EmbedLoopyBP
elif cmd_args.gm == 'DGCNN':
model = DGCNN
else:
print('unknown gm %s' % cmd_args.gm)
sys.exit()
if cmd_args.gm == 'DGCNN':
self.s2v = model(
latent_dim=cmd_args.latent_dim,
output_dim=cmd_args.out_dim,
num_node_feats=cmd_args.feat_dim+cmd_args.attr_dim,
num_edge_feats=0,
k=cmd_args.sortpooling_k)
else:
self.s2v = model(
latent_dim=cmd_args.latent_dim,
output_dim=cmd_args.out_dim,
num_node_feats=cmd_args.feat_dim,
num_edge_feats=0,
max_lv=cmd_args.max_lv)
out_dim = cmd_args.out_dim
if out_dim == 0:
if cmd_args.gm == 'DGCNN':
out_dim = self.s2v.dense_dim
else:
out_dim = cmd_args.latent_dim
self.mlp = MLPClassifier(
input_size=out_dim, hidden_size=cmd_args.hidden,
num_class=cmd_args.num_class, with_dropout=cmd_args.dropout)
def PrepareFeatureLabel(self, batch_graph):
labels = torch.LongTensor(len(batch_graph))
n_nodes = 0
if batch_graph[0].node_tags is not None:
node_tag_flag = True
concat_tag = []
else:
node_tag_flag = False
if batch_graph[0].node_features is not None:
node_feat_flag = True
concat_feat = []
else:
node_feat_flag = False
for i in range(len(batch_graph)):
labels[i] = batch_graph[i].label
n_nodes += batch_graph[i].num_nodes
if node_tag_flag:
concat_tag += batch_graph[i].node_tags
if node_feat_flag:
tmp = torch.from_numpy(
batch_graph[i].node_features).type('torch.FloatTensor')
concat_feat.append(tmp)
if node_tag_flag:
concat_tag = torch.LongTensor(concat_tag).view(-1, 1)
node_tag = torch.zeros(n_nodes, cmd_args.feat_dim)
node_tag.scatter_(1, concat_tag, 1)
if node_feat_flag:
node_feat = torch.cat(concat_feat, 0)
if node_feat_flag and node_tag_flag:
# concatenate one-hot embedding of node tags (node labels)
# with continuous node features
node_feat = torch.cat([node_tag.type_as(node_feat), node_feat], 1)
elif node_feat_flag is False and node_tag_flag:
node_feat = node_tag
elif node_feat_flag and node_tag_flag is False:
pass
else:
node_feat = torch.ones(n_nodes, 1)
if cmd_args.mode == 'gpu':
node_feat = node_feat.cuda()
labels = labels.cuda()
return node_feat, labels
def forward(self, batch_graph):
node_feat, labels = self.PrepareFeatureLabel(batch_graph)
embed = self.s2v(batch_graph, node_feat, None)
return self.mlp(embed, labels)
def output_features(self, batch_graph):
node_feat, labels = self.PrepareFeatureLabel(batch_graph)
embed = self.s2v(batch_graph, node_feat, None)
return embed, labels
def loop_dataset(g_list, classifier, sample_idxes, optimizer=None,
bsize=cmd_args.batch_size):
total_loss = []
total_iters = (len(sample_idxes) + (bsize - 1) * (optimizer is None)) // bsize # noqa
pbar = tqdm(range(total_iters), unit='batch')
all_targets = []
all_scores = []
n_samples = 0
for pos in pbar:
selected_idx = sample_idxes[pos * bsize: (pos + 1) * bsize]
batch_graph = [g_list[idx] for idx in selected_idx]
targets = [g_list[idx].label for idx in selected_idx]
all_targets += targets
logits, loss, acc = classifier(batch_graph)
all_scores.append(logits[:, 1].detach()) # for binary classification
if optimizer is not None:
optimizer.zero_grad()
loss.backward()
optimizer.step()
loss = loss.data.cpu().numpy()
pbar.set_description('loss: %0.5f acc: %0.5f' % (loss, acc))
total_loss.append(np.array([loss, acc]) * len(selected_idx))
n_samples += len(selected_idx)
if optimizer is None:
assert n_samples == len(sample_idxes)
total_loss = np.array(total_loss)
avg_loss = np.sum(total_loss, 0) / n_samples
all_scores = torch.cat(all_scores).cpu().numpy()
# np.savetxt('test_scores.txt', all_scores) # output test predictions
all_targets = np.array(all_targets)
fpr, tpr, _ = metrics.roc_curve(all_targets, all_scores, pos_label=1)
auc = metrics.auc(fpr, tpr)
avg_loss = np.concatenate((avg_loss, [auc]))
return avg_loss
if __name__ == '__main__':
print(cmd_args)
random.seed(cmd_args.seed)
np.random.seed(cmd_args.seed)
torch.manual_seed(cmd_args.seed)
train_graphs, test_graphs = load_data()
print('# train: %d, # test: %d' % (len(train_graphs), len(test_graphs)))
if cmd_args.sortpooling_k <= 1:
num_nodes_list = sorted([
g.num_nodes for g in train_graphs + test_graphs])
cmd_args.sortpooling_k = num_nodes_list[
int(math.ceil(cmd_args.sortpooling_k * len(num_nodes_list))) - 1]
cmd_args.sortpooling_k = max(10, cmd_args.sortpooling_k)
print('k used in SortPooling is: ' + str(cmd_args.sortpooling_k))
classifier = Classifier()
if cmd_args.mode == 'gpu':
classifier = classifier.cuda()
optimizer = optim.Adam(
classifier.parameters(), lr=cmd_args.learning_rate, amsgrad=True,
weight_decay=0.0008)
train_idxes = list(range(len(train_graphs)))
best_loss = None
max_acc = 0.0
for epoch in range(cmd_args.num_epochs):
random.shuffle(train_idxes)
classifier.train()
avg_loss = loop_dataset(
train_graphs, classifier, train_idxes, optimizer=optimizer)
if not cmd_args.printAUC:
avg_loss[2] = 0.0
print('\033[92maverage training of epoch %d: loss %.5f acc %.5f auc %.5f\033[0m' % (epoch, avg_loss[0], avg_loss[1], avg_loss[2])) # noqa
classifier.eval()
test_loss = loop_dataset(
test_graphs, classifier, list(range(len(test_graphs))))
if not cmd_args.printAUC:
test_loss[2] = 0.0
print('\033[93maverage test of epoch %d: loss %.5f acc %.5f auc %.5f\033[0m' % (epoch, test_loss[0], test_loss[1], test_loss[2])) # noqa
max_acc = max(max_acc, test_loss[1])
with open('acc_result.txt', 'a+') as f:
# f.write(str(test_loss[1]) + '\n')
f.write(str(max_acc) + '\n')
if cmd_args.printAUC:
with open('auc_results.txt', 'a+') as f:
f.write(str(test_loss[2]) + '\n')
if cmd_args.extract_features:
features, labels = classifier.output_features(train_graphs)
labels = labels.type('torch.FloatTensor')
np.savetxt('extracted_features_train.txt', torch.cat(
[labels.unsqueeze(1), features.cpu()], dim=1).detach().numpy(),
'%.4f')
features, labels = classifier.output_features(test_graphs)
labels = labels.type('torch.FloatTensor')
np.savetxt('extracted_features_test.txt', torch.cat(
[labels.unsqueeze(1), features.cpu()], dim=1).detach().numpy(),
'%.4f')