Skip to content

Conversation

@ryoji-kubo
Copy link
Contributor

Example use case:

from torch_geometric.utils import k_hop_subgraph
import torch

edge_index = torch.tensor([[1, 2, 3],
                           [0, 1, 1]])
# get the 2-hop neighbors of node 0 in the directed graph.
_, edge_index, _, edge_mask = k_hop_subgraph(0, 2, edge_index, relabel_nodes=False, directed=True)

This gives the following result:

Expected Outcome:

>>> edge_index
tensor([[1, 2, 3],
        [0, 1, 1]])

>>> edge_mask
tensor([True,  True,  True])

Actual Outcome:

>>> edge_index
tensor([[2, 3],
        [1, 1]])
>>> edge_mask
tensor([False,  True,  True])

This stems from the fact that the line torch.index_select(node_mask, 0, row, out=edge_mask) overwrites edge_mask, effectively only marking the edges used in the final hop as True.

To fix this, I have added preserved_edge_mask that will mark the edges used in each hop as True.

@ryoji-kubo ryoji-kubo requested a review from wsad1 as a code owner November 1, 2024 08:34
Copy link
Member

@rusty1s rusty1s left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks :) Also added a test.

@rusty1s rusty1s changed the title fixing edge mask for directed graphs in k_hop_subgraph Fixing edge_mask handling for directed graphs in k_hop_subgraph Nov 13, 2024
@codecov
Copy link

codecov bot commented Nov 13, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 87.33%. Comparing base (f5c8293) to head (923f35c).
Report is 68 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #9756      +/-   ##
==========================================
- Coverage   88.13%   87.33%   -0.81%     
==========================================
  Files         482      482              
  Lines       31421    31427       +6     
==========================================
- Hits        27693    27446     -247     
- Misses       3728     3981     +253     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@rusty1s rusty1s merged commit 850dc44 into pyg-team:master Nov 13, 2024
18 checks passed
@ryoji-kubo ryoji-kubo deleted the k_hop_subgraph_fix branch December 10, 2024 16:40
mattjhayes3 pushed a commit to mattjhayes3/pytorch_geometric that referenced this pull request Dec 14, 2024
…yg-team#9756)

Example use case:

```python
from torch_geometric.utils import k_hop_subgraph
import torch

edge_index = torch.tensor([[1, 2, 3],
                           [0, 1, 1]])
# get the 2-hop neighbors of node 0 in the directed graph.
_, edge_index, _, edge_mask = k_hop_subgraph(0, 2, edge_index, relabel_nodes=False, directed=True)
```

This gives the following result:

Expected Outcome:
```python
>>> edge_index
tensor([[1, 2, 3],
        [0, 1, 1]])

>>> edge_mask
tensor([True,  True,  True])
```

Actual Outcome:
```python
>>> edge_index
tensor([[2, 3],
        [1, 1]])
>>> edge_mask
tensor([False,  True,  True])
```

This stems from the fact that the line `torch.index_select(node_mask, 0,
row, out=edge_mask)` overwrites `edge_mask`, effectively only marking
the edges used in the final hop as `True`.

To fix this, I have added `preserved_edge_mask ` that will mark the
edges used in each hop as `True`.

---------

Co-authored-by: rusty1s <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants