Skip to content

How to remove a specific edge from edge_index #9440

Answered by EdisonLeeeee
haru-256 asked this question in Q&A
Discussion options

You must be logged in to vote

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:

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)

# remo…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@haru-256
Comment options

Answer selected by haru-256
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants