8000 pre-commit update (fix failing builds) by ValterH · Pull Request #306 · snap-stanford/relbench · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

pre-commit update (fix failing builds) #306

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

Merged
merged 4 commits into from
Jul 1, 2025
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
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ repos:
args: ["--profile", "black"]

- repo: https://github.com/PyCQA/docformatter
rev: v1.7.5
rev: v1.7.7
hooks:
- id: docformatter
additional_dependencies: [tomli]
Expand Down
22 changes: 14 additions & 8 deletions relbench/metrics.py
8000
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ def link_prediction_map(
maps = (precision_mat * pred_isin).sum(axis=1) / clipped_dst_count
return maps.mean()


def link_prediction_ndcg(
pred_isin: NDArray[np.int_],
dst_count: NDArray[np.int_],
Expand All @@ -190,24 +191,29 @@ def link_prediction_ndcg(
eval_k = pred_isin.shape[1]

# Compute the discounted multiplier (1 / log2(i + 2) for i = 0, ..., k-1)
discounted_multiplier = np.concatenate((
np.zeros(1),
1 / np.log2(np.arange(1, eval_k + 1) + 1)
))
discounted_multiplier = np.concatenate(
(np.zeros(1), 1 / np.log2(np.arange(1, eval_k + 1) + 1))
)

# Compute Discounted Cumulative Gain (DCG)
discounted_cumulative_gain = (pred_isin * discounted_multiplier[1:eval_k + 1]).sum(axis=1)
discounted_cumulative_gain = (
pred_isin * discounted_multiplier[1 : eval_k + 1]
).sum(axis=1)

# Clip dst_count to the range [0, eval_k]
clipped_dst_count = np.clip(dst_count, 0, eval_k)

# Compute Ideal Discounted Cumulative Gain (IDCG)
ideal_discounted_multiplier_cumsum = np.cumsum(discounted_multiplier)
ideal_discounted_cumulative_gain = ideal_discounted_multiplier_cumsum[clipped_dst_count]
ideal_discounted_cumulative_gain = ideal_discounted_multiplier_cumsum[
clipped_dst_count
]

# Avoid division by zero
ideal_discounted_cumulative_gain = np.clip(ideal_discounted_cumulative_gain, 1e-10, None)
ideal_discounted_cumulative_gain = np.clip(
ideal_discounted_cumulative_gain, 1e-10, None
)

# Compute NDCG
ndcg_scores = discounted_cumulative_gain / ideal_discounted_cumulative_gain
return ndcg_scores.mean()
return ndcg_scores.mean()
2 changes: 1 addition & 1 deletion test/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

from relbench.metrics import (
link_prediction_map,
link_prediction_ndcg,
link_prediction_precision,
link_prediction_recall,
link_prediction_ndcg,
)


Expand Down
6 changes: 2 additions & 4 deletions tutorials/train_model.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -621,16 +621,14 @@
"\n",
"\n",
"class GloveTextEmbedding:\n",
" def __init__(self, device: Optional[torch.device\n",
" ] = None):\n",
" def __init__(self, device: Optional[torch.device] = None):\n",
" self.model = SentenceTransformer(\n",
" \"sentence-transformers/average_word_embeddings_glove.6B.300d\",\n",
" device=device,\n",
" )\n",
"\n",
" def __call__(self, sentences: List[str]) -> Tensor:\n",
" return torch.from_numpy(self.model.encode(sentences))\n",
"\n"
" return torch.from_numpy(self.model.encode(sentences))"
]
},
{
Expand Down
0