-
Notifications
You must be signed in to change notification settings - Fork 0
/
aug.py
38 lines (31 loc) · 986 Bytes
/
aug.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
import torch as th
import numpy as np
import dgl
def random_aug(graph, attr, diag_attr, x, feat_drop_rate, edge_mask_rate):
n_node = graph.number_of_nodes()
edge_mask = mask_edge(graph, edge_mask_rate)
feat = drop_feature(x, feat_drop_rate)
ng = dgl.graph([])
ng.add_nodes(n_node)
src = graph.edges()[0]
dst = graph.edges()[1]
nsrc = src[edge_mask]
ndst = dst[edge_mask]
ng.add_edges(nsrc, ndst)
ng = ng.add_self_loop()
attr = th.cat([attr[edge_mask], diag_attr])
return ng, attr, feat
def drop_feature(x, drop_prob):
drop_mask = th.empty(
(x.size(1),),
dtype=th.float32,
device=x.device).uniform_(0, 1) < drop_prob
x = x.clone()
x[:, drop_mask] = 0
return x
def mask_edge(graph, mask_prob):
E = graph.number_of_edges()
mask_rates = th.FloatTensor(np.ones(E) * mask_prob)
masks = th.bernoulli(1 - mask_rates)
mask_idx = masks.nonzero().squeeze(1)
return mask_idx