-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.py
82 lines (62 loc) · 2.39 KB
/
utils.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
import os
import dgl
import torch
import shutil
import random
import numpy as np
def set_seed(seed):
torch.manual_seed(seed)
np.random.seed(seed)
random.seed(seed)
dgl.random.seed(seed)
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
if torch.cuda.is_available():
torch.cuda.manual_seed_all(seed)
def check_writable(path, overwrite=True):
if not os.path.exists(path):
os.makedirs(path)
elif overwrite:
shutil.rmtree(path)
os.makedirs(path)
else:
pass
def extract_indices(g):
edge_idx_loop = g.adjacency_matrix(transpose=True)._indices()
edge_idx_no_loop = dgl.remove_self_loop(g).adjacency_matrix(transpose=True)._indices()
edge_idx = (edge_idx_loop, edge_idx_no_loop)
return edge_idx
def idx_split(idx, ratio, seed=0):
set_seed(seed)
n = len(idx)
cut = int(n * ratio)
idx_idx_shuffle = torch.randperm(n)
idx1_idx, idx2_idx = idx_idx_shuffle[:cut], idx_idx_shuffle[cut:]
idx1, idx2 = idx[idx1_idx], idx[idx2_idx]
return idx1, idx2
def graph_split(idx_train, idx_val, idx_test, rate, seed):
"""
Args:
The original setting was transductive. Full graph is observed, and idx_train takes up a small portion.
Split the graph by further divide idx_test into [idx_test_tran, idx_test_ind].
rate = idx_test_ind : idx_test (how much test to hide for the inductive evaluation)
Ex. Ogbn-products
loaded : train : val : test = 8 : 2 : 90, rate = 0.2
after split: train : val : test_tran : test_ind = 8 : 2 : 72 : 18
Return:
Indices start with 'obs_' correspond to the node indices within the observed subgraph,
where as indices start directly with 'idx_' correspond to the node indices in the original graph
"""
idx_test_ind, idx_test_tran = idx_split(idx_test, rate, seed)
idx_obs = torch.cat([idx_train, idx_val, idx_test_tran])
N1, N2 = idx_train.shape[0], idx_val.shape[0]
obs_idx_all = torch.arange(idx_obs.shape[0])
obs_idx_train = obs_idx_all[:N1]
obs_idx_val = obs_idx_all[N1 : N1 + N2]
obs_idx_test = obs_idx_all[N1 + N2 :]
return obs_idx_train, obs_idx_val, obs_idx_test, idx_obs, idx_test_ind
def get_evaluator(dataset):
def evaluator(out, labels):
pred = out.argmax(1)
return pred.eq(labels).float().mean().item()
return evaluator