-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathtrainer.py
More file actions
341 lines (307 loc) · 12.7 KB
/
trainer.py
File metadata and controls
341 lines (307 loc) · 12.7 KB
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
import os
import numpy as np
import torch
import torch_geometric.datasets
from ogb.nodeproppred import Evaluator, PygNodePropPredDataset
from sklearn.metrics import f1_score
from torch.profiler import ProfilerActivity, profile
from torch_geometric.transforms import ToSparseTensor, ToUndirected
from GraphSampling import *
from LP.LP_Adj import LabelPropagation_Adj
from Precomputing import *
def load_data(dataset_name, to_sparse=True):
if dataset_name in ["ogbn-products", "ogbn-papers100M", "ogbn-arxiv"]:
root = os.path.join(os.path.dirname(os.path.realpath(__file__)), "..", "data", dataset_name)
T = ToSparseTensor() if to_sparse else lambda x: x
if to_sparse and dataset_name == "ogbn-arxiv":
T = lambda x: ToSparseTensor()(ToUndirected()(x))
dataset = PygNodePropPredDataset(name=dataset_name, root=root, transform=T)
processed_dir = dataset.processed_dir
split_idx = dataset.get_idx_split()
evaluator = Evaluator(name=dataset_name)
data = dataset[0]
split_masks = {}
for split in ["train", "valid", "test"]:
mask = torch.zeros(data.num_nodes, dtype=torch.bool)
mask[split_idx[split]] = True
data[f"{split}_mask"] = mask
split_masks[f"{split}"] = data[f"{split}_mask"]
x = data.x
y = data.y = data.y.squeeze()
elif dataset_name in ["Reddit", "Flickr", "AmazonProducts", "Yelp"]:
path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "..", "data", dataset_name)
T = ToSparseTensor() if to_sparse else lambda x: x
dataset_class = getattr(torch_geometric.datasets, dataset_name)
dataset = dataset_class(path, transform=T)
processed_dir = dataset.processed_dir
data = dataset[0]
evaluator = None
split_masks = {}
split_masks["train"] = data.train_mask
split_masks["valid"] = data.val_mask
split_masks["test"] = data.test_mask
x = data.x
y = data.y
# E = data.edge_index.shape[1]
# N = data.train_mask.shape[0]
# data.edge_idx = torch.arange(0, E)
# data.node_idx = torch.arange(0, N)
else:
raise Exception(f"the dataset of {dataset} has not been implemented")
return data, x, y, split_masks, evaluator, processed_dir
def idx2mask(idx, N_nodes):
mask = torch.tensor([False] * N_nodes, device=idx.device)
mask[idx] = True
return mask
class trainer(object):
def __init__(self, args, trial=None):
self.dataset = args.dataset
self.device = torch.device(f"cuda:{args.cuda_num}" if args.cuda else "cpu")
self.args = args
self.args.device = self.device
self.type_model = args.type_model
self.epochs = args.epochs
self.eval_steps = args.eval_steps
# used to indicate multi-label classification.
# If it is, using BCE and micro-f1 performance metric
self.multi_label = args.multi_label
if self.multi_label:
self.loss_op = torch.nn.BCEWithLogitsLoss()
else:
self.loss_op = torch.nn.NLLLoss()
if self.type_model == "EnGCN":
args.tosparse = True
self.data, self.x, self.y, self.split_masks, self.evaluator, self.processed_dir = load_data(
args.dataset, args.tosparse
)
if self.type_model in ["GraphSAGE"]:
self.model = GraphSAGE(args, self.data, self.split_masks["train"], self.processed_dir)
elif self.type_model in ["FastGCN"]:
self.model = FastGCN(args, self.data, self.split_masks["train"], self.processed_dir)
elif self.type_model in ["LADIES"]:
self.model = LADIES(args, self.data, self.split_masks["train"], self.processed_dir)
elif self.type_model == "GraphSAINT":
self.model = GraphSAINT(args, self.data, self.split_masks["train"], self.processed_dir)
elif self.type_model == "ClusterGCN":
self.model = ClusterGCN(args, self.data, self.split_masks["train"], self.processed_dir)
elif self.type_model == "LP_Adj": # -wz-ini
self.model = LabelPropagation_Adj(args, self.data, self.split_masks["train"])
elif self.type_model == "SIGN_MLP":
self.model = SIGN_MLP(args, self.data, self.split_masks["train"], self.processed_dir)
elif self.type_model == "SIGN":
if self.dataset == "Products":
self.model = SIGN_v2(args, self.data, self.split_masks["train"], self.processed_dir)
else:
self.model = SIGN(args, self.data, self.split_masks["train"], self.processed_dir)
elif self.type_model == "SGC":
self.model = SGC(args, self.data, self.split_masks["train"], self.processed_dir)
elif self.type_model == "SAGN":
self.model = SAGN(args, self.data, self.split_masks["train"], self.processed_dir)
elif self.type_model == "EnGCN":
self.model = EnGCN(
args,
self.data,
self.evaluator,
trial=trial,
)
elif self.type_model == "GAMLP":
if args.GAMLP_type == "R":
self.model = R_GAMLP(
args,
self.data,
self.split_masks["train"],
pre_process=True,
alpha=args.GAMLP_alpha,
)
elif args.GAMLP_type == "JK":
self.model = JK_GAMLP(
args,
self.data,
self.split_masks["train"],
pre_process=True,
alpha=args.GAMLP_alpha,
)
else:
raise ValueError(f"Unknown GAMLP type: {args.GAMLP_type}")
else:
raise NotImplementedError("please specify `type_model`")
self.model.to(self.device)
if len(list(self.model.parameters())) != 0:
self.optimizer = torch.optim.Adam(self.model.parameters(), lr=args.lr, weight_decay=args.weight_decay)
else:
self.optimizer = None
def mem_speed_bench(self):
input_dict = self.get_input_dict(0)
self.model.mem_speed_bench(input_dict)
def train_ensembling(self, seed):
# assert isinstance(self.model, (SAdaGCN, AdaGCN, GBGCN))
input_dict = self.get_input_dict(0)
acc = self.model.train_and_test(input_dict)
return acc
def test_cpu_mem(self, seed):
input_dict = self.get_input_dict(0)
with profile(activities=[ProfilerActivity.CPU], profile_memory=True, record_shapes=True) as prof:
acc = self.model.train_and_test(input_dict)
print(prof.key_averages().table(sort_by="self_cpu_memory_usage", row_limit=10))
return acc
def train_and_test(self, seed):
results = []
for epoch in range(self.epochs):
train_loss, train_acc = self.train_net(epoch) # -wz-run
print(
f"Seed: {seed:02d}, "
f"Epoch: {epoch:02d}, "
f"Loss: {train_loss:.4f}, "
f"Approx Train Acc: {train_acc:.4f}"
)
if epoch % self.eval_steps == 0 and epoch != 0:
out, result = self.test_net()
results.append(result)
train_acc, valid_acc, test_acc = result
print(
f"Epoch: {epoch:02d}, "
f"Loss: {train_loss:.4f}, "
f"Train: {100 * train_acc:.2f}%, "
f"Valid: {100 * valid_acc:.2f}% "
f"Test: {100 * test_acc:.2f}%"
)
results = 100 * np.array(results)
best_idx = np.argmax(results[:, 1])
best_train = results[best_idx, 0]
best_valid = results[best_idx, 1]
best_test = results[best_idx, 2]
print(f"Best train: {best_train:.2f}%, " f"Best valid: {best_valid:.2f}% " f"Best test: {best_test:.2f}%")
return best_train, best_valid, best_test
def train_net(self, epoch):
self.model.train()
input_dict = self.get_input_dict(epoch)
train_loss, train_acc = self.model.train_net(input_dict)
return train_loss, train_acc
def get_input_dict(self, epoch):
if self.type_model in [
"GraphSAGE",
"GraphSAINT",
"ClusterGCN",
"FastGCN",
"LADIES",
]:
input_dict = {
"x": self.x,
"y": self.y,
"optimizer": self.optimizer,
"loss_op": self.loss_op,
"device": self.device,
}
elif self.type_model in ["DST-GCN", "_GraphSAINT", "GradientSampling"]:
input_dict = {
"x": self.x,
"y": self.y,
"optimizer": self.optimizer,
"loss_op": self.loss_op,
"device": self.device,
"epoch": epoch,
"split_masks": self.split_masks,
}
elif self.type_model in ["LP_Adj"]:
input_dict = {
"split_masks": self.split_masks,
"data": self.data,
"x": self.x,
"y": self.y,
"optimizer": self.optimizer,
"loss_op": self.loss_op,
"device": self.device,
}
elif self.type_model in [
"SIGN",
"SGC",
"SAGN",
"GAMLP",
"GPRGNN",
"PPRGo",
"Bagging",
"SAdaGCN",
"AdaGCN",
"AdaGCN_CandS",
"AdaGCN_SLE",
"EnGCN",
"GBGCN",
]:
input_dict = {
"split_masks": self.split_masks,
"data": self.data,
"x": self.x,
"y": self.y,
"optimizer": self.optimizer,
"loss_op": self.loss_op,
"device": self.device,
}
else:
Exception(f"the model of {self.type_model} has not been implemented")
return input_dict
@torch.no_grad()
def test_net(self):
self.model.eval()
input_dict = {"x": self.x, "y": self.y, "device": self.device}
out = self.model.inference(input_dict)
if self.evaluator is not None:
y_true = self.y.unsqueeze(-1)
y_pred = out.argmax(dim=-1, keepdim=True)
train_acc = self.evaluator.eval(
{
"y_true": y_true[self.split_masks["train"]],
"y_pred": y_pred[self.split_masks["train"]],
}
)["acc"]
valid_acc = self.evaluator.eval(
{
"y_true": y_true[self.split_masks["valid"]],
"y_pred": y_pred[self.split_masks["valid"]],
}
)["acc"]
test_acc = self.evaluator.eval(
{
"y_true": y_true[self.split_masks["test"]],
"y_pred": y_pred[self.split_masks["test"]],
}
)["acc"]
else:
if not self.multi_label:
pred = out.argmax(dim=-1).to("cpu")
y_true = self.y
correct = pred.eq(y_true)
train_acc = correct[self.split_masks["train"]].sum().item() / self.split_masks["train"].sum().item()
valid_acc = correct[self.split_masks["valid"]].sum().item() / self.split_masks["valid"].sum().item()
test_acc = correct[self.split_masks["test"]].sum().item() / self.split_masks["test"].sum().item()
else:
pred = (out > 0).float().numpy()
y_true = self.y.numpy()
# calculating F1 scores
train_acc = (
f1_score(
y_true[self.split_masks["train"]],
pred[self.split_masks["train"]],
average="micro",
)
if pred[self.split_masks["train"]].sum() > 0
else 0
)
valid_acc = (
f1_score(
y_true[self.split_masks["valid"]],
pred[self.split_masks["valid"]],
average="micro",
)
if pred[self.split_masks["valid"]].sum() > 0
else 0
)
test_acc = (
f1_score(
y_true[self.split_masks["test"]],
pred[self.split_masks["test"]],
average="micro",
)
if pred[self.split_masks["test"]].sum() > 0
else 0
)
return out, (train_acc, valid_acc, test_acc)