How to remove a specific edge from edge_index #9440
-
I want to remove a specific edge from the As per official documentation, The A full scan of the returned removed_edge_index_list = [
(src, dst) for src, dst in edge_index.T if not (src == seed_index and dst in positive_dst_node)
]
removed_edge_index = torch.tensor(removed_edge_index_list).T But this is slow when |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @haru-256 It is non-trival to remove edges from a graph stored as COO format. But there is a tricky way to remove edges from import torch
from torch_geometric.utils import coalesce
edge_index = torch.tensor([[1, 2, 3, 4 ,5],
[2, 3, 4, 5, 6]])
removed_edge_index = torch.tensor([[1, 3],
[2, 4]])
all_edge_index = torch.cat([edge_index, removed_edge_index], dim=1)
# mark removed edges as 1 and 0 otherwise
all_edge_weights = torch.cat([torch.zeros(edge_index.size(1)), torch.ones(removed_edge_index.size(1))])
all_edge_index, all_edge_weights = coalesce(all_edge_index, all_edge_weights)
# remove edges indicated by 1
remaining_edge_index = all_edge_index[:, all_edge_weights ==0]
remaining_edge_index
tensor([[2, 4, 5],
[3, 5, 6]]) |
Beta Was this translation helpful? Give feedback.
Hi @haru-256
It is non-trival to remove edges from a graph stored as COO format. But there is a tricky way to remove edges from
edge_index
by merging them first: