-
Notifications
You must be signed in to change notification settings - Fork 8
/
view_functions.py
432 lines (336 loc) · 16.2 KB
/
view_functions.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
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
import torch
import random
import numpy as np
from torch_geometric.data import Batch, Data
from sklearn.preprocessing import MinMaxScaler
from torch_geometric.utils import to_dense_adj, dense_to_sparse, subgraph
"""
Part of the code is adapted from https://github.com/divelab/DIG.
"""
class EdgePerturbation():
"""
Edge perturbation on the given graph or batched graphs. Class objects callable via
method :meth:`views_fn`.
Args:
add (bool, optional): Set :obj:`True` if randomly add edges in a given graph.
(default: :obj:`True`)
drop (bool, optional): Set :obj:`True` if randomly drop edges in a given graph.
(default: :obj:`False`)
ratio (float, optional): Percentage of edges to add or drop. (default: :obj:`0.1`)
"""
def __init__(self, add=True, drop=False, ratio=0.1):
self.add = add
self.drop = drop
self.ratio = ratio
def __call__(self, data):
return self.views_fn(data)
def do_trans(self, data):
node_num, _ = data.x.size()
_, edge_num = data.edge_index.size()
perturb_num = int(edge_num * self.ratio)
edge_index = data.edge_index.detach().clone()
idx_remain = edge_index
idx_add = torch.tensor([]).reshape(2, -1).long()
if self.drop:
idx_remain = edge_index[:, np.random.choice(edge_num, edge_num-perturb_num, replace=False)]
if self.add:
idx_add = torch.randint(node_num, (2, perturb_num))
new_edge_index = torch.cat((idx_remain, idx_add), dim=1)
new_edge_index = torch.unique(new_edge_index, dim=1)
return Data(x=data.x, edge_index=new_edge_index)
def views_fn(self, data):
"""
Method to be called when :class:`EdgePerturbation` object is called.
Args:
data (:class:`torch_geometric.data.Data`): The input graph or batched graphs.
:rtype: :class:`torch_geometric.data.Data`.
"""
if isinstance(data, Batch):
dlist = [self.do_trans(d) for d in data.to_data_list()]
return Batch.from_data_list(dlist)
elif isinstance(data, Data):
return self.do_trans(data)
class Diffusion():
"""
Diffusion on the given graph or batched graphs, used in
`MVGRL <https://arxiv.org/pdf/2006.05582v1.pdf>`_. Class objects callable via
method :meth:`views_fn`.
Args:
mode (string, optional): Diffusion instantiation mode with two options:
:obj:`"ppr"`: Personalized PageRank; :obj:`"heat"`: heat kernel.
(default: :obj:`"ppr"`)
alpha (float, optinal): Teleport probability in a random walk. (default: :obj:`0.2`)
t (float, optinal): Diffusion time. (default: :obj:`5`)
add_self_loop (bool, optional): Set True to add self-loop to edge_index.
(default: :obj:`True`)
"""
def __init__(self, mode="ppr", alpha=0.2, t=5, add_self_loop=True):
self.mode = mode
self.alpha = alpha
self.t = t
self.add_self_loop = add_self_loop
def __call__(self, data):
return self.views_fn(data)
def do_trans(self, data):
node_num, _ = data.x.size()
if self.add_self_loop:
sl = torch.tensor([[n, n] for n in range(node_num)]).t()
edge_index = torch.cat((data.edge_index, sl), dim=1)
else:
edge_index = data.edge_index.detach().clone()
orig_adj = to_dense_adj(edge_index)[0]
orig_adj = torch.where(orig_adj>1, torch.ones_like(orig_adj), orig_adj)
d = torch.diag(torch.sum(orig_adj, 1))
if self.mode == "ppr":
dinv = torch.inverse(torch.sqrt(d))
at = torch.matmul(torch.matmul(dinv, orig_adj), dinv)
diff_adj = self.alpha * torch.inverse((torch.eye(orig_adj.shape[0]) - (1 - self.alpha) * at))
elif self.mode == "heat":
diff_adj = torch.exp(self.t * (torch.matmul(orig_adj, torch.inverse(d)) - 1))
else:
raise Exception("Must choose one diffusion instantiation mode from 'ppr' and 'heat'!")
edge_ind, edge_attr = dense_to_sparse(diff_adj)
return Data(x=data.x, edge_index=edge_ind, edge_attr=edge_attr)
def views_fn(self, data):
"""
Method to be called when :class:`Diffusion` object is called.
Args:
data (:class:`torch_geometric.data.Data`): The input graph or batched graphs.
:rtype: :class:`torch_geometric.data.Data`.
"""
if isinstance(data, Batch):
dlist = [self.do_trans(d) for d in data.to_data_list()]
return Batch.from_data_list(dlist)
elif isinstance(data, Data):
return self.do_trans(data)
class DiffusionWithSample():
"""
Diffusion with node sampling on the given graph for node-level datasets, used in
`MVGRL <https://arxiv.org/pdf/2006.05582v1.pdf>`_. Class objects callable via method
:meth:`views_fn`.
Args:
sample_size (int, optional): Number of nodes in the sampled subgraoh from a large graph.
(default: :obj:`2000`)
batch_size (int, optional): Number of subgraphs to sample. (default: :obj:`4`)
mode (string, optional): Diffusion instantiation mode with two options:
:obj:`"ppr"`: Personalized PageRank; :obj:`"heat"`: heat kernel;
(default: :obj:`"ppr"`)
alpha (float, optinal): Teleport probability in a random walk. (default: :obj:`0.2`)
t (float, optinal): Diffusion time. (default: :obj:`5`)
add_self_loop (bool, optional): Set True to add self-loop to edge_index.
(default: :obj:`True`)
"""
def __init__(self, sample_size=2000, batch_size=4, mode="ppr",
alpha=0.2, t=5, epsilon=False, add_self_loop=True):
self.sample_size = sample_size
self.batch_size = batch_size
self.mode = mode
self.alpha = alpha
self.t = t
self.epsilon = epsilon
self.add_self_loop = add_self_loop
def __call__(self, data):
return self.views_fn(data)
def views_fn(self, data):
"""
Method to be called when :class:`DiffusionWithSample` object is called.
Args:
data (:class:`torch_geometric.data.Data`): The input graph or batched graphs.
:rtype: :class:`torch_geometric.data.Data`.
"""
node_num, _ = data.x.size()
if self.add_self_loop:
sl = torch.tensor([[n, n] for n in range(node_num)]).t()
edge_index = torch.cat((data.edge_index, sl), dim=1)
else:
edge_index = data.edge_index.detach().clone()
orig_adj = to_dense_adj(edge_index)[0]
orig_adj = torch.where(orig_adj>1, torch.ones_like(orig_adj), orig_adj)
d = torch.diag(torch.sum(orig_adj, 1))
if self.mode == "ppr":
dinv = torch.inverse(torch.sqrt(d))
at = torch.matmul(torch.matmul(dinv, orig_adj), dinv)
diff_adj = self.alpha * torch.inverse((torch.eye(orig_adj.shape[0]) - (1 - self.alpha) * at))
elif self.mode == "heat":
diff_adj = torch.exp(self.t * (torch.matmul(orig_adj, torch.inverse(d)) - 1))
else:
raise Exception("Must choose one diffusion instantiation mode from 'ppr' and 'heat'!")
if self.epsilon:
epsilons = [1e-5, 1e-4, 1e-3, 1e-2]
avg_degree = torch.sum(orig_adj) / orig_adj.shape[0]
ep = epsilons[np.argmin([abs(avg_degree - torch.sum(diff_adj >= e) / diff_adj.shape[0]) for e in epsilons])]
diff_adj[diff_adj < ep] = 0.0
scaler = MinMaxScaler()
scaler.fit(diff_adj)
diff_adj = torch.tensor(scaler.transform(diff_adj))
dlist_orig_x = []
dlist_diff_x = []
drop_num = node_num - self.sample_size
for b in range(self.batch_size):
idx_drop = np.random.choice(node_num, drop_num, replace=False)
idx_nondrop = [n for n in range(node_num) if not n in idx_drop]
sample_orig_adj = orig_adj.clone()
sample_orig_adj = sample_orig_adj[idx_nondrop, :][:, idx_nondrop]
sample_diff_adj = diff_adj.clone()
sample_diff_adj = sample_diff_adj[idx_nondrop, :][:, idx_nondrop]
sample_orig_x = data.x[idx_nondrop]
edge_ind, edge_attr = dense_to_sparse(sample_diff_adj)
dlist_orig_x.append(Data(x=sample_orig_x,
edge_index=dense_to_sparse(sample_orig_adj)[0]))
dlist_diff_x.append(Data(x=sample_orig_x,
edge_index=edge_ind,
edge_attr=edge_attr))
return Batch.from_data_list(dlist_orig_x), Batch.from_data_list(dlist_diff_x)
class UniformSample():
"""
Uniformly node dropping on the given graph or batched graphs.
Class objects callable via method :meth:`views_fn`.
Args:
ratio (float, optinal): Ratio of nodes to be dropped. (default: :obj:`0.1`)
"""
def __init__(self, ratio=0.1):
self.ratio = ratio
def __call__(self, data):
return self.views_fn(data)
def do_trans(self, data):
node_num, _ = data.x.size()
_, edge_num = data.edge_index.size()
keep_num = int(node_num * (1-self.ratio))
idx_nondrop = torch.randperm(node_num)[:keep_num]
mask_nondrop = torch.zeros_like(data.x[:,0]).scatter_(0, idx_nondrop, 1.0).bool()
edge_index, _ = subgraph(mask_nondrop, data.edge_index, relabel_nodes=True, num_nodes=node_num)
return Data(x=data.x[mask_nondrop], edge_index=edge_index)
def views_fn(self, data):
"""
Method to be called when :class:`UniformSample` object is called.
Args:
data (:class:`torch_geometric.data.Data`): The input graph or batched graphs.
:rtype: :class:`torch_geometric.data.Data`.
"""
if isinstance(data, Batch):
dlist = [self.do_trans(d) for d in data.to_data_list()]
return Batch.from_data_list(dlist)
elif isinstance(data, Data):
return self.do_trans(data)
class RWSample():
"""
Subgraph sampling based on random walk on the given graph or batched graphs.
Class objects callable via method :meth:`views_fn`.
Args:
ratio (float, optional): Percentage of nodes to sample from the graph.
(default: :obj:`0.1`)
add_self_loop (bool, optional): Set True to add self-loop to edge_index.
(default: :obj:`False`)
"""
def __init__(self, ratio=0.1, add_self_loop=False):
self.ratio = ratio
self.add_self_loop = add_self_loop
def __call__(self, data):
return self.views_fn(data)
def do_trans(self, data):
node_num, _ = data.x.size()
sub_num = int(node_num * self.ratio)
if self.add_self_loop:
sl = torch.tensor([[n, n] for n in range(node_num)]).t()
edge_index = torch.cat((data.edge_index, sl), dim=1)
else:
edge_index = data.edge_index.detach().clone()
# edge_index = edge_index.numpy()
idx_sub = [np.random.randint(node_num, size=1)[0]]
# idx_neigh = set([n for n in edge_index[1][edge_index[0]==idx_sub[0]]])
idx_neigh = set([n.item() for n in edge_index[1][edge_index[0]==idx_sub[0]]])
count = 0
while len(idx_sub) <= sub_num:
count = count + 1
if count > node_num:
break
if len(idx_neigh) == 0:
break
sample_node = np.random.choice(list(idx_neigh))
if sample_node in idx_sub:
continue
idx_sub.append(sample_node)
# idx_neigh.union(set([n for n in edge_index[1][edge_index[0]==idx_sub[-1]]]))
idx_neigh.union(set([n.item() for n in edge_index[1][edge_index[0]==idx_sub[-1]]]))
idx_sub = torch.LongTensor(idx_sub).to(data.x.device)
mask_nondrop = torch.zeros_like(data.x[:,0]).scatter_(0, idx_sub, 1.0).bool()
edge_index, _ = subgraph(mask_nondrop, data.edge_index, relabel_nodes=True, num_nodes=node_num)
return Data(x=data.x[mask_nondrop], edge_index=edge_index)
def views_fn(self, data):
"""
Method to be called when :class:`RWSample` object is called.
Args:
data (:class:`torch_geometric.data.Data`): The input graph or batched graphs.
:rtype: :class:`torch_geometric.data.Data`.
"""
if isinstance(data, Batch):
dlist = [self.do_trans(d) for d in data.to_data_list()]
return Batch.from_data_list(dlist)
elif isinstance(data, Data):
return self.do_trans(data)
class NodeAttrMask():
"""
Node attribute masking on the given graph or batched graphs.
Class objects callable via method :meth:`views_fn`.
Args:
mode (string, optinal): Masking mode with three options:
:obj:`"whole"`: mask all feature dimensions of the selected node with a Gaussian distribution;
:obj:`"partial"`: mask only selected feature dimensions with a Gaussian distribution;
:obj:`"onehot"`: mask all feature dimensions of the selected node with a one-hot vector.
(default: :obj:`"whole"`)
mask_ratio (float, optinal): The ratio of node attributes to be masked. (default: :obj:`0.1`)
mask_mean (float, optional): Mean of the Gaussian distribution to generate masking values.
(default: :obj:`0.5`)
mask_std (float, optional): Standard deviation of the distribution to generate masking values.
Must be non-negative. (default: :obj:`0.5`)
"""
def __init__(self, mode='whole', mask_ratio=0.1, mask_mean=0.5, mask_std=0.5, return_mask=False):
self.mode = mode
self.mask_ratio = mask_ratio
self.mask_mean = mask_mean
self.mask_std = mask_std
self.return_mask = return_mask
def __call__(self, data):
return self.views_fn(data)
def do_trans(self, data):
node_num, feat_dim = data.x.size()
x = data.x.detach().clone()
if self.mode == "whole":
mask = torch.zeros(node_num)
mask_num = int(node_num * self.mask_ratio)
idx_mask = np.random.choice(node_num, mask_num, replace=False)
x[idx_mask] = torch.tensor(np.random.normal(loc=self.mask_mean, scale=self.mask_std,
size=(mask_num, feat_dim)), dtype=torch.float32)
mask[idx_mask] = 1
elif self.mode == "partial":
mask = torch.zeros((node_num, feat_dim))
for i in range(node_num):
for j in range(feat_dim):
if random.random() < self.mask_ratio:
x[i][j] = torch.tensor(np.random.normal(loc=self.mask_mean,
scale=self.mask_std), dtype=torch.float32)
mask[i][j] = 1
elif self.mode == "onehot":
mask = torch.zeros(node_num)
mask_num = int(node_num * self.mask_ratio)
idx_mask = np.random.choice(node_num, mask_num, replace=False)
x[idx_mask] = torch.tensor(np.eye(feat_dim)[np.random.randint(0, feat_dim, size=(mask_num))], dtype=torch.float32)
mask[idx_mask] = 1
else:
raise Exception("Masking mode option '{0:s}' is not available!".format(mode))
if self.return_mask:
return Data(x=x, edge_index=data.edge_index, mask=mask)
else:
return Data(x=x, edge_index=data.edge_index)
def views_fn(self, data):
"""
Method to be called when :class:`NodeAttrMask` object is called.
Args:
data (:class:`torch_geometric.data.Data`): The input graph or batched graphs.
:rtype: :class:`torch_geometric.data.Data`.
"""
if isinstance(data, Batch):
dlist = [self.do_trans(d) for d in data.to_data_list()]
return Batch.from_data_list(dlist)
elif isinstance(data, Data):
return self.do_trans(data)