-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdata_loader.py
126 lines (93 loc) · 4.06 KB
/
data_loader.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
import warnings
import torch
import scipy.sparse as sp
import numpy as np
import os
import torch_geometric.transforms as T
from torch_geometric.datasets import Planetoid, WikipediaNetwork, Actor, WebKB, Amazon, Coauthor, WikiCS
from torch_geometric.utils import remove_self_loops
warnings.simplefilter("ignore")
def get_split(num_samples: int, train_ratio: float = 0.1, test_ratio: float = 0.8, num_splits: int = 10):
assert train_ratio + test_ratio < 1
train_size = int(num_samples * train_ratio)
test_size = int(num_samples * test_ratio)
trains, vals, tests = [], [], []
for _ in range(num_splits):
indices = torch.randperm(num_samples)
train_mask = torch.zeros(num_samples, dtype=torch.bool)
train_mask.fill_(False)
train_mask[indices[:train_size]] = True
test_mask = torch.zeros(num_samples, dtype=torch.bool)
test_mask.fill_(False)
test_mask[indices[train_size: test_size + train_size]] = True
val_mask = torch.zeros(num_samples, dtype=torch.bool)
val_mask.fill_(False)
val_mask[indices[test_size + train_size:]] = True
trains.append(train_mask.unsqueeze(1))
vals.append(val_mask.unsqueeze(1))
tests.append(test_mask.unsqueeze(1))
train_mask_all = torch.cat(trains, 1)
val_mask_all = torch.cat(vals, 1)
test_mask_all = torch.cat(tests, 1)
return train_mask_all, val_mask_all, test_mask_all
def get_structural_encoding(edges, nnodes, str_enc_dim=16):
row = edges[0, :].numpy()
col = edges[1, :].numpy()
data = np.ones_like(row)
A = sp.csr_matrix((data, (row, col)), shape=(nnodes, nnodes))
D = (np.array(A.sum(1)).squeeze()) ** -1.0
Dinv = sp.diags(D)
RW = A * Dinv
M = RW
SE = [torch.from_numpy(M.diagonal()).float()]
M_power = M
for _ in range(str_enc_dim - 1):
M_power = M_power * M
SE.append(torch.from_numpy(M_power.diagonal()).float())
SE = torch.stack(SE, dim=-1)
return SE
def load_data(dataset_name):
path = os.path.join(os.path.dirname(os.path.realpath(__file__)), '.', 'data', dataset_name)
if dataset_name in ['cora', 'citeseer', 'pubmed']:
dataset = Planetoid(path, dataset_name)
elif dataset_name in ['chameleon']:
dataset = WikipediaNetwork(path, dataset_name)
elif dataset_name in ['squirrel']:
dataset = WikipediaNetwork(path, dataset_name, transform=T.NormalizeFeatures())
elif dataset_name in ['actor']:
dataset = Actor(path)
elif dataset_name in ['cornell', 'texas', 'wisconsin']:
dataset = WebKB(path, dataset_name)
elif dataset_name in ['computers', 'photo']:
dataset = Amazon(path, dataset_name, transform=T.NormalizeFeatures())
elif dataset_name in ['cs', 'physics']:
dataset = Coauthor(path, dataset_name, transform=T.NormalizeFeatures())
elif dataset_name in ['wikics']:
dataset = WikiCS(path)
data = dataset[0]
edges = remove_self_loops(data.edge_index)[0]
features = data.x
[nnodes, nfeats] = features.shape
nclasses = torch.max(data.y).item() + 1
if dataset_name in ['computers', 'photo', 'cs', 'physics', 'wikics']:
train_mask, val_mask, test_mask = get_split(nnodes)
else:
train_mask, val_mask, test_mask = data.train_mask, data.val_mask, data.test_mask
if len(train_mask.shape) < 2:
train_mask = train_mask.unsqueeze(1)
val_mask = val_mask.unsqueeze(1)
test_mask = test_mask.unsqueeze(1)
labels = data.y
path = '../data/se/{}'.format(dataset_name)
if not os.path.exists(path):
os.makedirs(path)
file_name = path + '/{}_{}.pt'.format(dataset_name, 16)
if os.path.exists(file_name):
se = torch.load(file_name)
# print('Load exist structural encoding.')
else:
print('Computing structural encoding...')
se = get_structural_encoding(edges, nnodes)
torch.save(se, file_name)
print('Done. The structural encoding is saved as: {}.'.format(file_name))
return features, edges, se, train_mask, val_mask, test_mask, labels, nnodes, nfeats