aug.py 940 Bytes
Newer Older
1
2
# Data augmentation on graphs via edge dropping and feature masking

Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
3
import dgl
4
import numpy as np
5
6
7
import torch as th


8
def aug(graph, x, feat_drop_rate, edge_mask_rate):
9
    n_node = graph.num_nodes()
10

11
12
    edge_mask = mask_edge(graph, edge_mask_rate)
    feat = drop_feature(x, feat_drop_rate)
13

14
15
    src = graph.edges()[0]
    dst = graph.edges()[1]
16

17
18
    nsrc = src[edge_mask]
    ndst = dst[edge_mask]
19

20
21
    ng = dgl.graph((nsrc, ndst), num_nodes=n_node)
    ng = ng.add_self_loop()
22

23
    return ng, feat
24

25

26
def drop_feature(x, drop_prob):
27
28
29
30
    drop_mask = (
        th.empty((x.size(1),), dtype=th.float32, device=x.device).uniform_(0, 1)
        < drop_prob
    )
31
    x = x.clone()
32
33
34
    x[:, drop_mask] = 0

    return x
35

36

37
38
39
40
41
42
def mask_edge(graph, mask_prob):
    E = graph.num_edges()

    mask_rates = th.FloatTensor(np.ones(E) * mask_prob)
    masks = th.bernoulli(1 - mask_rates)
    mask_idx = masks.nonzero().squeeze(1)
43
    return mask_idx