Skip to content

Commit

Permalink
Merge branch 'master' into tagdataset/add-llm-exp-pred
Browse files Browse the repository at this point in the history
  • Loading branch information
xnuohz authored Jan 21, 2025
2 parents 5f853ba + 9c73f99 commit 1f47744
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
17 changes: 17 additions & 0 deletions test/metrics/test_link_pred_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,20 @@ def test_link_pred_metric_collection(num_src_nodes, num_dst_nodes, num_edges):
metric_collection.update(pred_index_mat, edge_label_index)
assert metric_collection.compute() == expected
metric_collection.reset()


def test_empty_ground_truth():
pred = torch.rand(10, 5)
pred_index_mat = pred.argsort(dim=1)
edge_label_index = torch.empty(2, 0, dtype=torch.long)
edge_label_weight = torch.empty(0)

metric = LinkPredMAP(k=5)
metric.update(pred_index_mat, edge_label_index)
assert metric.compute() == 0
metric.reset()

metric = LinkPredNDCG(k=5, weighted=True)
metric.update(pred_index_mat, edge_label_index, edge_label_weight)
assert metric.compute() == 0
metric.reset()
8 changes: 8 additions & 0 deletions torch_geometric/metrics/link_pred.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ def pred_rel_mat(self) -> Tensor:
if hasattr(self, '_pred_rel_mat'):
return self._pred_rel_mat # type: ignore

if self.edge_label_index[1].numel() == 0:
self._pred_rel_mat = torch.zeros_like(
self.pred_index_mat,
dtype=torch.bool if self.edge_label_weight is None else
torch.get_default_dtype(),
)
return self._pred_rel_mat

# Flatten both prediction and ground-truth indices, and determine
# overlaps afterwards via `torch.searchsorted`.
max_index = max( # type: ignore
Expand Down

0 comments on commit 1f47744

Please sign in to comment.