This repository has been archived by the owner on Mar 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaseline.py
300 lines (261 loc) · 8.73 KB
/
baseline.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
import argparse
import copy
import numpy as np
import torch
from torch import optim
from torch import nn
from pathlib import Path
from models import Model
from criterion import BaselineLoss
from dataloader import load_data
from utils import (
get_logger,
get_evaluator,
set_seed,
get_training_config,
check_writable,
compute_min_cut_loss,
graph_split,
feature_prop,
)
from train_and_eval import run_transductive, run_inductive
def get_args():
parser = argparse.ArgumentParser(description="PyTorch DGL implementation")
parser.add_argument("--device", type=int, default=0, help="CUDA device, -1 means CPU")
parser.add_argument("--seed", type=int, default=0, help="Random seed")
parser.add_argument(
"--log_level",
type=int,
default=20,
help="Logger levels for run {10: DEBUG, 20: INFO, 30: WARNING}",
)
parser.add_argument(
"--console_log",
action="store_true",
help="Set to True to display log info in console",
)
parser.add_argument(
"--output_path", type=str, default="outputs", help="Path to save outputs"
)
parser.add_argument(
"--num_exp", type=int, default=1, help="Repeat how many experiments"
)
parser.add_argument(
"--exp_setting",
type=str,
default="tran",
choices=["tran", "ind"],
help="transductive or inductive",
)
parser.add_argument(
"--eval_interval", type=int, default=1, help="Evaluate once per how many epochs"
)
parser.add_argument(
"--save_results",
action="store_true",
help="Set to True to save the loss curves, trained model, and min-cut loss for the transductive setting",
)
"""Dataset"""
parser.add_argument("--dataset", type=str, default="cora", help="Dataset")
parser.add_argument("--data_path", type=str, default="./data", help="Path to data")
parser.add_argument(
"--split_idx",
type=int,
default=0,
help="For Non-Homo datasets only, one of [0,1,2,3,4]",
)
"""Model"""
parser.add_argument(
"--model_config_path",
type=str,
default="./train.conf.yaml",
help="Path to model configeration",
)
parser.add_argument("--model", type=str, default="SAGE")
"""Optimization"""
parser.add_argument(
"--max_epoch", type=int, default=1000, help="Evaluate once per how many epochs"
)
parser.add_argument(
"--patience",
type=int,
default=100,
help="Early stop is the score on validation set does not improve for how many epochs",
)
"""Ablation"""
parser.add_argument(
"--feature_noise",
type=float,
default=0,
help="add white noise to features for analysis, value in [0, 1] for noise level",
)
parser.add_argument(
"--split_rate",
type=float,
default=0.2,
help="Rate for graph split, see comment of graph_split for more details",
)
parser.add_argument(
"--compute_min_cut",
action="store_true",
help="Set to True to compute and store the min-cut loss",
)
parser.add_argument(
"--feature_aug_k",
type=int,
default=0,
help="Augment node futures by aggregating feature_aug_k-hop neighbor features",
)
args = parser.parse_args()
return args
def run(args):
"""
Returns:
score_lst: a list of evaluation results on test set.
len(score_lst) = 1 for the transductive setting.
len(score_lst) = 2 for the inductive/production setting.
"""
""" Set seed, device, and logger """
set_seed(args.seed)
if torch.cuda.is_available() and args.device >= 0:
device = torch.device("cuda:" + str(args.device))
else:
device = "cpu"
if args.feature_noise != 0:
args.output_path = args.output_path + f"/noise_{args.feature_noise}"
if args.feature_aug_k > 0:
args.output_path = args.output_path + f"/aug_hop_{args.feature_aug_k}"
if args.exp_setting == "tran":
output_dir = Path.cwd().joinpath(
args.output_path,
"transductive",
args.dataset,
args.model,
f"seed_{args.seed}",
)
else:
output_dir = Path.cwd().joinpath(
args.output_path,
"inductive",
f"split_rate_{args.split_rate}",
args.dataset,
args.model,
f"seed_{args.seed}",
)
args.output_dir = output_dir
check_writable(output_dir, overwrite=False)
logger = get_logger(output_dir.joinpath("log"), args.console_log, args.log_level)
""" Load data """
g, labels, idx_train, idx_val, idx_test = load_data(
args.dataset,
args.data_path,
split_idx=args.split_idx,
seed=args.seed,
)
logger.info(f"Total {g.number_of_nodes()} nodes, {g.number_of_edges()} edges.")
feats = g.ndata["feat"]
args.feat_dim = feats.shape[1]
args.num_classes = labels.max().item() + 1
args.label_dim = labels.max().item() + 1
if 0 < args.feature_noise <= 1:
feats = (
1 - args.feature_noise
) * feats + args.feature_noise * torch.randn_like(feats)
""" Model config """
conf = {}
if args.model_config_path is not None:
conf = get_training_config(args.model_config_path, f"{f'GA{args.feature_aug_k}' if args.feature_aug_k else ''}{args.model}", args.dataset)
conf = dict(args.__dict__, **conf)
conf["device"] = device
logger.info(f"conf: {conf}")
""" Model init """
model = Model(conf)
model.p = torch.ones(1, conf["feat_dim"]).to(device)
optimizer = optim.Adam(
model.parameters(), lr=conf["learning_rate"], weight_decay=conf["weight_decay"]
)
criterion = BaselineLoss()
evaluator = get_evaluator(conf["dataset"], True)
""" Data split and run """
loss_and_score = []
if args.exp_setting == "tran":
indices = (idx_train, idx_val, idx_test)
# propagate node feature
if args.feature_aug_k > 0:
feats = feature_prop(feats, g, args.feature_aug_k)
out, score_val, score_test = run_transductive(
conf,
model,
g,
feats,
labels,
indices,
criterion,
evaluator,
optimizer,
logger,
loss_and_score,
)
score_lst = [score_test]
else:
indices = graph_split(idx_train, idx_val, idx_test, args.split_rate, args.seed)
# propagate node feature. The propagation for the observed graph only happens within the subgraph obs_g
if args.feature_aug_k > 0:
idx_obs = indices[3]
obs_g = g.subgraph(idx_obs)
obs_feats = feature_prop(feats[idx_obs], obs_g, args.feature_aug_k)
feats = feature_prop(feats, g, args.feature_aug_k)
feats[idx_obs] = obs_feats
out, score_val, score_test_tran, score_test_ind = run_inductive(
conf,
model,
g,
feats,
labels,
indices,
criterion,
evaluator,
optimizer,
logger,
loss_and_score,
)
score_lst = [score_test_tran, score_test_ind]
logger.info(f"# params {sum(p.numel() for p in model.parameters() if p.requires_grad)}")
""" Saving loss curve and model """
if args.save_results:
# Loss curves
loss_and_score = np.array(loss_and_score)
np.savez(output_dir.joinpath("loss_and_score"), loss_and_score)
# Model
torch.save(model.state_dict(), output_dir.joinpath("model.pth"))
""" Saving min-cut loss """
if args.exp_setting == "tran" and args.compute_min_cut:
min_cut = compute_min_cut_loss(g, out)
with open(output_dir.parent.joinpath("min_cut_loss"), "a+") as f:
f.write(f"{min_cut :.4f}\n")
return score_lst
def repeat_run(args):
scores = []
for seed in range(args.num_exp):
tmp = copy.deepcopy(args)
tmp.seed = seed
scores.append(run(tmp))
args.output_dir = tmp.output_dir
scores_np = np.array(scores)
return scores_np.mean(axis=0), scores_np.std(axis=0)
def main():
args = get_args()
if args.num_exp == 1:
score = run(args)
score_str = "".join([f"{s : .4f}\t" for s in score])
elif args.num_exp > 1:
score_mean, score_std = repeat_run(args)
score_str = "".join(
[f"{s : .4f}\t" for s in score_mean] + [f"{s : .4f}\t" for s in score_std]
)
with open(args.output_dir.parent.joinpath("exp_results"), "a+") as f:
f.write(f"{score_str}\n")
# for collecting aggregated results
print(score_str)
if __name__ == "__main__":
main()