-
Notifications
You must be signed in to change notification settings - Fork 3k
/
main.py
184 lines (149 loc) · 5.2 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
import sys
from parser import Parser
import mxnet as mx
import numpy as np
from dataloader import collate, GraphDataLoader
from dgl.data.gindt import GINDataset
from gin import GIN
from mxnet import gluon, nd
from mxnet.gluon import nn
from tqdm import tqdm
def train(args, net, trainloader, trainer, criterion, epoch):
running_loss = 0
total_iters = len(trainloader)
# setup the offset to avoid the overlap with mouse cursor
bar = tqdm(range(total_iters), unit="batch", position=2, file=sys.stdout)
for pos, (graphs, labels) in zip(bar, trainloader):
# batch graphs will be shipped to device in forward part of model
labels = labels.as_in_context(args.device)
feat = graphs.ndata["attr"].as_in_context(args.device)
with mx.autograd.record():
graphs = graphs.to(args.device)
outputs = net(graphs, feat)
loss = criterion(outputs, labels)
loss = loss.sum() / len(labels)
running_loss += loss.asscalar()
# backprop
loss.backward()
trainer.step(batch_size=1)
# report
bar.set_description("epoch-{}".format(epoch))
bar.close()
# the final batch will be aligned
running_loss = running_loss / total_iters
return running_loss
def eval_net(args, net, dataloader, criterion):
total = 0
total_loss = 0
total_correct = 0
for data in dataloader:
graphs, labels = data
labels = labels.as_in_context(args.device)
feat = graphs.ndata["attr"].as_in_context(args.device)
total += len(labels)
graphs = graphs.to(args.device)
outputs = net(graphs, feat)
predicted = nd.argmax(outputs, axis=1)
predicted = predicted.astype("int64")
total_correct += (predicted == labels).sum().asscalar()
loss = criterion(outputs, labels)
# crossentropy(reduce=True) for default
total_loss += loss.sum().asscalar()
loss, acc = 1.0 * total_loss / total, 1.0 * total_correct / total
return loss, acc
def main(args):
# set up seeds, args.seed supported
mx.random.seed(0)
np.random.seed(seed=0)
if args.device >= 0:
args.device = mx.gpu(args.device)
else:
args.device = mx.cpu()
dataset = GINDataset(args.dataset, not args.learn_eps)
trainloader, validloader = GraphDataLoader(
dataset,
batch_size=args.batch_size,
collate_fn=collate,
seed=args.seed,
shuffle=True,
split_name="fold10",
fold_idx=args.fold_idx,
).train_valid_loader()
# or split_name='rand', split_ratio=0.7
model = GIN(
args.num_layers,
args.num_mlp_layers,
dataset.dim_nfeats,
args.hidden_dim,
dataset.gclasses,
args.final_dropout,
args.learn_eps,
args.graph_pooling_type,
args.neighbor_pooling_type,
)
model.initialize(ctx=args.device)
criterion = gluon.loss.SoftmaxCELoss()
print(model.collect_params())
lr_scheduler = mx.lr_scheduler.FactorScheduler(50, 0.5)
trainer = gluon.Trainer(
model.collect_params(), "adam", {"lr_scheduler": lr_scheduler}
)
# it's not cost-effective to hanle the cursor and init 0
# https://stackoverflow.com/a/23121189
tbar = tqdm(
range(args.epochs), unit="epoch", position=3, ncols=0, file=sys.stdout
)
vbar = tqdm(
range(args.epochs), unit="epoch", position=4, ncols=0, file=sys.stdout
)
lrbar = tqdm(
range(args.epochs), unit="epoch", position=5, ncols=0, file=sys.stdout
)
for epoch, _, _ in zip(tbar, vbar, lrbar):
train(args, model, trainloader, trainer, criterion, epoch)
train_loss, train_acc = eval_net(args, model, trainloader, criterion)
tbar.set_description(
"train set - average loss: {:.4f}, accuracy: {:.0f}%".format(
train_loss, 100.0 * train_acc
)
)
valid_loss, valid_acc = eval_net(args, model, validloader, criterion)
vbar.set_description(
"valid set - average loss: {:.4f}, accuracy: {:.0f}%".format(
valid_loss, 100.0 * valid_acc
)
)
if not args.filename == "":
with open(args.filename, "a") as f:
f.write(
"%s %s %s %s"
% (
args.dataset,
args.learn_eps,
args.neighbor_pooling_type,
args.graph_pooling_type,
)
)
f.write("\n")
f.write(
"%f %f %f %f"
% (train_loss, train_acc, valid_loss, valid_acc)
)
f.write("\n")
lrbar.set_description(
"Learning eps with learn_eps={}: {}".format(
args.learn_eps,
[
layer.eps.data(args.device).asscalar()
for layer in model.ginlayers
],
)
)
tbar.close()
vbar.close()
lrbar.close()
if __name__ == "__main__":
args = Parser(description="GIN").args
print("show all arguments configuration...")
print(args)
main(args)