Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rank computation in the RGCN link prediction example #4688

Merged
merged 4 commits into from
May 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Added support for graph-level outputs in `to_hetero` ([#4582](https://github.com/pyg-team/pytorch_geometric/pull/4582))
- Added `CHANGELOG.md` ([#4581](https://github.com/pyg-team/pytorch_geometric/pull/4581))
### Changed
- Fixed the ranking protocol bug in the RGCN link prediction example ([#4688](https://github.com/pyg-team/pytorch_geometric/pull/4688))
- Math support in Markdown ([#4683](https://github.com/pyg-team/pytorch_geometric/pull/4683))
- Allow for `setter` properties in `Data` ([#4682](https://github.com/pyg-team/pytorch_geometric/pull/4682), [#4686](https://github.com/pyg-team/pytorch_geometric/pull/4686))
- Allow for optional `edge_weight` in `GCN2Conv` ([#4670](https://github.com/pyg-team/pytorch_geometric/pull/4670))
Expand Down
20 changes: 14 additions & 6 deletions examples/rgcn_link_pred.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@ def test():
return valid_mrr, test_mrr


@torch.no_grad()
def compute_rank(ranks):
# fair ranking prediction as the average
# of optimistic and pessimistic ranking
true = ranks[0]
optimistic = (ranks > true).sum() + 1
pessimistic = (ranks >= true).sum()
return (optimistic + pessimistic).float() * 0.5


@torch.no_grad()
def compute_mrr(z, edge_index, edge_type):
ranks = []
Expand All @@ -135,9 +145,8 @@ def compute_mrr(z, edge_index, edge_type):
eval_edge_type = torch.full_like(tail, fill_value=rel)

out = model.decode(z, eval_edge_index, eval_edge_type)
perm = out.argsort(descending=True)
rank = int((perm == 0).nonzero(as_tuple=False).view(-1)[0])
ranks.append(rank + 1)
rank = compute_rank(out)
ranks.append(rank)

# Try all nodes as heads, but delete true triplets:
head_mask = torch.ones(data.num_nodes, dtype=torch.bool)
Expand All @@ -155,9 +164,8 @@ def compute_mrr(z, edge_index, edge_type):
eval_edge_type = torch.full_like(head, fill_value=rel)

out = model.decode(z, eval_edge_index, eval_edge_type)
perm = out.argsort(descending=True)
rank = int((perm == 0).nonzero(as_tuple=False).view(-1)[0])
ranks.append(rank + 1)
rank = compute_rank(out)
ranks.append(rank)

return (1. / torch.tensor(ranks, dtype=torch.float)).mean()

Expand Down