-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
303 lines (249 loc) · 9.9 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
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
import os.path as osp
import numpy as np
import scipy.sparse as sp
import torch
from deeprobust.graph.data import Dataset
from deeprobust.graph.utils import get_train_val_test
import numpy as np
import torch.nn.functional as F
from sklearn.preprocessing import StandardScaler
# from deeprobust.graph.utils import *
from torch_geometric.data import NeighborSampler
from torch_geometric.utils import add_remaining_self_loops, to_undirected
import json
class Pyg2Dpr(Dataset):
def __init__(self, pyg_data, **kwargs):
try:
splits = pyg_data.get_idx_split()
except:
pass
dataset_name = pyg_data.name
pyg_data = pyg_data[0]
n = pyg_data.num_nodes
if dataset_name == 'ogbn-arxiv': # symmetrization
pyg_data.edge_index = to_undirected(pyg_data.edge_index, pyg_data.num_nodes)
self.adj = sp.csr_matrix((np.ones(pyg_data.edge_index.shape[1]),
(pyg_data.edge_index[0], pyg_data.edge_index[1])), shape=(n, n))
self.features = pyg_data.x.numpy()
self.labels = pyg_data.y.numpy()
if len(self.labels.shape) == 2 and self.labels.shape[1] == 1:
self.labels = self.labels.reshape(-1) # ogb-arxiv needs to reshape
if hasattr(pyg_data, 'train_mask'):
# for fixed split
self.idx_train = mask_to_index(pyg_data.train_mask, n)
self.idx_val = mask_to_index(pyg_data.val_mask, n)
self.idx_test = mask_to_index(pyg_data.test_mask, n)
self.name = 'Pyg2Dpr'
else:
try:
# for ogb
self.idx_train = splits['train']
self.idx_val = splits['valid']
self.idx_test = splits['test']
self.name = 'Pyg2Dpr'
except:
# for other datasets
self.idx_train, self.idx_val, self.idx_test = get_train_val_test(
nnodes=n, val_size=0.1, test_size=0.8, stratify=self.labels)
def mask_to_index(index, size):
all_idx = np.arange(size)
return all_idx[index]
def index_to_mask(index, size):
mask = torch.zeros((size,), dtype=torch.bool)
mask[index] = 1
return mask
def match_loss(gw_syn, gw_real, args, device):
dis = torch.tensor(0.0).to(device)
if args.dis_metric == 'ours':
for ig in range(len(gw_real)):
gwr = gw_real[ig]
gws = gw_syn[ig]
dis += distance_wb(gwr, gws)
elif args.dis_metric == 'cos':
gw_real_vec = []
gw_syn_vec = []
for ig in range(len(gw_real)):
gw_real_vec.append(gw_real[ig].reshape((-1)))
gw_syn_vec.append(gw_syn[ig].reshape((-1)))
gw_real_vec = torch.cat(gw_real_vec, dim=0)
gw_syn_vec = torch.cat(gw_syn_vec, dim=0)
dis = 1 - torch.sum(gw_real_vec * gw_syn_vec, dim=-1) / (
torch.norm(gw_real_vec, dim=-1) * torch.norm(gw_syn_vec, dim=-1) + 0.000001)
else:
exit('DC error: unknown distance function')
return dis
def distance_wb(gwr, gws):
shape = gwr.shape
# TODO: output node!!!!
if len(gwr.shape) == 2:
gwr = gwr.T
gws = gws.T
if len(shape) == 4: # conv, out*in*h*w
gwr = gwr.reshape(shape[0], shape[1] * shape[2] * shape[3])
gws = gws.reshape(shape[0], shape[1] * shape[2] * shape[3])
elif len(shape) == 3: # layernorm, C*h*w
gwr = gwr.reshape(shape[0], shape[1] * shape[2])
gws = gws.reshape(shape[0], shape[1] * shape[2])
elif len(shape) == 2: # linear, out*in
tmp = 'do nothing'
elif len(shape) == 1: # batchnorm/instancenorm, C; groupnorm x, bias
gwr = gwr.reshape(1, shape[0])
gws = gws.reshape(1, shape[0])
return 0
dis_weight = torch.sum(
1 - torch.sum(gwr * gws, dim=-1) / (torch.norm(gwr, dim=-1) * torch.norm(gws, dim=-1) + 0.000001))
dis = dis_weight
return dis
def regularization(adj, x, eig_real=None):
# fLf
loss = 0
# loss += torch.norm(adj, p=1)
loss += feature_smoothing(adj, x)
return loss
def sparsity2(adj):
n = adj.shape[0]
loss_degree = - torch.log(adj.sum(1)).sum() / n
loss_fro = torch.norm(adj) / n
return 0 * loss_degree + loss_fro
def sparsity(adj):
n = adj.shape[0]
thresh = n * n * 0.01
return F.relu(adj.sum() - thresh)
# return F.relu(adj.sum()-thresh) / n**2
def feature_smoothing(adj, X):
adj = (adj.t() + adj) / 2
rowsum = adj.sum(1)
r_inv = rowsum.flatten()
D = torch.diag(r_inv)
L = D - adj
r_inv = r_inv + 1e-8
r_inv = r_inv.pow(-1 / 2).flatten()
r_inv[torch.isinf(r_inv)] = 0.
r_mat_inv = torch.diag(r_inv)
L = r_mat_inv @ L @ r_mat_inv
XLXT = torch.matmul(torch.matmul(X.t(), L), X)
loss_smooth_feat = torch.trace(XLXT)
return loss_smooth_feat
def row_normalize_tensor(mx):
rowsum = mx.sum(1)
r_inv = rowsum.pow(-1).flatten()
r_mat_inv = torch.diag(r_inv)
mx = r_mat_inv @ mx
return mx
class DataGraphSAINT:
'''datasets used in GraphSAINT paper'''
def __init__(self, dataset, **kwargs):
root='./'
dataset_str = root + 'data/' + dataset + '/'
adj_full = sp.load_npz(dataset_str + 'adj_full.npz')
self.nnodes = adj_full.shape[0]
if dataset == 'ogbn-arxiv':
adj_full = adj_full + adj_full.T
adj_full[adj_full > 1] = 1
role = json.load(open(dataset_str + 'role.json', 'r'))
idx_train = role['tr']
idx_test = role['te']
idx_val = role['va']
if 'label_rate' in kwargs:
label_rate = kwargs['label_rate']
if label_rate < 1:
idx_train = idx_train[:int(label_rate * len(idx_train))]
self.adj_train = adj_full[np.ix_(idx_train, idx_train)]
self.adj_val = adj_full[np.ix_(idx_val, idx_val)]
self.adj_test = adj_full[np.ix_(idx_test, idx_test)]
feat = np.load(dataset_str + 'feats.npy')
# ---- normalize feat ----
feat_train = feat[idx_train]
scaler = StandardScaler()
scaler.fit(feat_train)
feat = scaler.transform(feat)
self.feat_train = feat[idx_train]
self.feat_val = feat[idx_val]
self.feat_test = feat[idx_test]
class_map = json.load(open(dataset_str + 'class_map.json', 'r'))
labels = self.process_labels(class_map)
self.labels_train = labels[idx_train]
self.labels_val = labels[idx_val]
self.labels_test = labels[idx_test]
self.data_full = GraphData(adj_full, feat, labels, idx_train, idx_val, idx_test)
self.class_dict = None
self.class_dict2 = None
self.adj_full = adj_full
self.feat_full = feat
self.labels_full = labels
self.idx_train = np.array(idx_train)
self.idx_val = np.array(idx_val)
self.idx_test = np.array(idx_test)
self.samplers = None
def process_labels(self, class_map):
"""
setup vertex property map for output classests
"""
num_vertices = self.nnodes
if isinstance(list(class_map.values())[0], list):
num_classes = len(list(class_map.values())[0])
self.nclass = num_classes
class_arr = np.zeros((num_vertices, num_classes))
for k, v in class_map.items():
class_arr[int(k)] = v
else:
class_arr = np.zeros(num_vertices, dtype=np.int32)
for k, v in class_map.items():
class_arr[int(k)] = v
class_arr = class_arr - class_arr.min()
self.nclass = max(class_arr) + 1
return class_arr
def retrieve_class(self, c, num=256):
if self.class_dict is None:
self.class_dict = {}
for i in range(self.nclass):
self.class_dict['class_%s' % i] = (self.labels_train == i)
idx = np.arange(len(self.labels_train))
idx = idx[self.class_dict['class_%s' % c]]
return np.random.permutation(idx)[:num]
def retrieve_class_sampler(self, c, adj, transductive, num=256, args=None):
if args.nlayers == 1:
sizes = [30]
if args.nlayers == 2:
if args.dataset in ['reddit', 'flickr']:
if args.option == 0:
sizes = [15, 8]
if args.option == 1:
sizes = [20, 10]
if args.option == 2:
sizes = [25, 10]
else:
sizes = [10, 5]
if self.class_dict2 is None:
print(sizes)
self.class_dict2 = {}
for i in range(self.nclass):
if transductive:
idx_train = np.array(self.idx_train)
idx = idx_train[self.labels_train == i]
else:
idx = np.arange(len(self.labels_train))[self.labels_train == i]
self.class_dict2[i] = idx
if self.samplers is None:
self.samplers = []
for i in range(self.nclass):
node_idx = torch.LongTensor(self.class_dict2[i])
if len(node_idx) == 0:
continue
self.samplers.append(NeighborSampler(adj,
node_idx=node_idx,
sizes=sizes, batch_size=num,
num_workers=8, return_e_id=False,
num_nodes=adj.size(0),
shuffle=True))
batch = np.random.permutation(self.class_dict2[c])[:num]
out = self.samplers[c].sample(batch)
return out
class GraphData:
def __init__(self, adj, features, labels, idx_train, idx_val, idx_test):
self.adj = adj
self.features = features
self.labels = labels
self.idx_train = idx_train
self.idx_val = idx_val
self.idx_test = idx_test