pyg-team/pytorch_geometric

is there any effcient way to add and drop edge in edge_index ?

Closed this issue · 3 comments

## ❓ Questions & Help

is there any way to add or drop edge in the data.edge_index as below


dataset = 'Cora'
path = osp.join(osp.dirname(osp.realpath(__file__)), '..', 'data', dataset)
dataset = Planetoid(path, dataset, T.NormalizeFeatures())
data = dataset[0]
data.edge_index

I implement add edges randomly in this way, but I think it is too slow, is there any efficient way?
`

    n_count = int(edge_numer * ratio)
    while n_count >0:
        head = randrange(data.num_nodes)
        tail_set = data.edge_index[:,data.edge_index[0] == head][1]
        tail = randrange(data.num_nodes)
        while tail in tail_set:
            tail = randrange(data.num_nodes)
        new_edge = torch.tensor([[head,tail],[tail,head]],dtype=data.edge_index.dtype)
        data.edge_index = torch.cat([data.edge_index,new_edge],dim=1)
        n_count = n_count - 2`

We implement the random removal of edges in dropout_adj.

Random addition of edges can be achieved in two ways (untested pseudocode):

a) dense:

adj = torch.ones((num_nodes, num_nodes), dtype=torch.uint8)
adj[edge_index[0], edge_index[1]] = 0
potential_edges = adj.nonzero().t()
... # Sample a fixed amount of edges.

b) sparse:

row, col = edge_index
added_row, added_col = torch.randint(num_nodes, (2, sample_ratio), dtype=torch.long)
# Find duplicated edges ...
idx = torch.cat([num_nodes * row + col, num_nodes * added_row + added_col], dim=0)
_, inv, count = torch.unique(idx, return_inverse=True, return_counts=True)
mask = count[inv][row.size(0):] == 1
added_edge_index = torch.stack([added_row[mask], added_col[mask]], dim=0)
... # repeat if number of added edges is not sufficient (added_edge_index.size(1) < threshold).

It works for me. Really appreciate your work and your kindly reply,