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 hitrate #1159

Merged
merged 39 commits into from
Apr 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
04caa31
add movielens
Jul 28, 2020
e870498
Merge remote-tracking branch 'upstream/master'
Aug 5, 2020
28c5148
commit before merge
Sep 6, 2020
034b832
Merge remote-tracking branch 'upstream/master'
Oct 8, 2020
699e97d
First hitrate metric version
denyhoof Jul 16, 2020
ef3ce1e
Merge remote-tracking branch 'upstream/master'
Oct 18, 2020
9dae326
Merge remote-tracking branch 'upstream/master'
Mar 7, 2021
3c5e53a
Merge remote-tracking branch 'upstream/master'
zkid18 Mar 27, 2021
38abe3a
metrge with upstream
zkid18 Mar 27, 2021
2e7e891
fix topk_map
zkid18 Mar 27, 2021
1c89f51
fix error in map
zkid18 Mar 27, 2021
56a4186
tests for map
zkid18 Mar 27, 2021
3c7079a
top_k ndcg
zkid18 Mar 27, 2021
372e877
edit changelog
zkid18 Mar 27, 2021
c5f4435
edit the tabs
zkid18 Mar 28, 2021
2e1d957
check codestyle
zkid18 Mar 28, 2021
2402682
check the intent
zkid18 Mar 28, 2021
534d482
check the intent
zkid18 Mar 28, 2021
f3e927b
remove trailing whitespace
zkid18 Mar 28, 2021
c35a6a3
fixed hitrate
zkid18 Apr 4, 2021
e81e31f
fixed the docs
zkid18 Apr 4, 2021
2a60b31
hitrate
zkid18 Apr 4, 2021
9b6c04a
Merge remote-tracking branch 'upstream/master'
zkid18 Apr 4, 2021
560188e
Additive margin softmax (#1131)
Atharva-Phatak Mar 27, 2021
767b031
Readme update2 (#1142)
Scitator Mar 28, 2021
b1ad3e4
updated dl_cpu(workflows)- For passing CI-Tests (#1135)
Atharva-Phatak Mar 28, 2021
a5334fe
fix: `_key_value` for schedulers in case of multiple optimizers fixed…
bagxi Mar 28, 2021
c929ab5
Github CI fixes (#1143)
Scitator Mar 28, 2021
b40d166
Engine docs (#1141)
Scitator Mar 28, 2021
bd2c46b
v21.03.1
Scitator Mar 28, 2021
c0bc738
v21.03: minimal version fix (#1147)
Scitator Mar 29, 2021
5a15977
fix: nested dicts in loaders_params/samplers_params overriding (#1150)
bagxi Mar 31, 2021
fcbcfd1
docs (#1152)
elephantmipt Apr 1, 2021
cd96186
Github CI fix (#1148)
Scitator Apr 1, 2021
074534b
Niftireader (#1151)
ssktotoro Apr 2, 2021
e906e1e
Merge branch 'master' into fix_topk_metrics
zkid18 Apr 4, 2021
c8ba69b
add changelog
zkid18 Apr 4, 2021
630c621
solved runtime error
zkid18 Apr 5, 2021
e3a4d3c
hitrate calculation
zkid18 Apr 9, 2021
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 @@ -29,6 +29,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

- minimal requirements issue ([#1147](https://github.com/catalyst-team/catalyst/issues/1147))
- nested dicts in `loaders_params`/`samplers_params` overriding fixed ([#1150](https://github.com/catalyst-team/catalyst/pull/1150))
- fixed hitrate calculation issue ([#1155]) (https://github.com/catalyst-team/catalyst/issues/1155)

## [21.03.1] - 2021-03-28

Expand Down
13 changes: 10 additions & 3 deletions catalyst/metrics/functional/_hitrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
from catalyst.metrics.functional._misc import process_recsys_components


def hitrate(outputs: torch.Tensor, targets: torch.Tensor, topk: List[int]) -> List[torch.Tensor]:
def hitrate(
outputs: torch.Tensor, targets: torch.Tensor, topk: List[int], zero_division: int = 0
) -> List[torch.Tensor]:
"""
Calculate the hit rate score given model outputs and targets.
Calculate the hit rate (aka recall) score given
model outputs and targets.
Hit-rate is a metric for evaluating ranking systems.
Generate top-N recommendations and if one of the recommendation is
actually what user has rated, you consider that a hit.
Expand All @@ -30,6 +33,9 @@ def hitrate(outputs: torch.Tensor, targets: torch.Tensor, topk: List[int]) -> Li
ground truth, labels
topk (List[int]):
Parameter fro evaluation on top-k items
zero_division (int):
value, returns in the case of the divison by zero
should be one of 0 or 1

Returns:
hitrate_at_k (List[torch.Tensor]): the hitrate score
Expand All @@ -39,7 +45,8 @@ def hitrate(outputs: torch.Tensor, targets: torch.Tensor, topk: List[int]) -> Li
targets_sort_by_outputs = process_recsys_components(outputs, targets)
for k in topk:
k = min(outputs.size(1), k)
hits_score = torch.sum(targets_sort_by_outputs[:, :k], dim=1) / k
hits_score = torch.sum(targets_sort_by_outputs[:, :k], dim=1) / targets.sum(dim=1)
hits_score = hits_score.nan_to_num(zero_division)
results.append(torch.mean(hits_score))

return results
Expand Down
30 changes: 25 additions & 5 deletions catalyst/metrics/functional/tests/test_hitrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ def test_hitrate():
"""
Tests for catalyst.metrics.hitrate metric.
"""
y_pred = [0.5, 0.2]
y_true = [1.0, 0.0]
k = [1, 2]
y_pred = [0.5, 0.2, 0.1]
y_true = [1.0, 0.0, 1.0]
k = [1, 2, 3]

hitrate_at1, hitrate_at2 = hitrate(torch.Tensor([y_pred]), torch.Tensor([y_true]), k)
assert hitrate_at1 == 1.0
hitrate_at1, hitrate_at2, hitrate_at3 = hitrate(
torch.Tensor([y_pred]), torch.Tensor([y_true]), k
)
assert hitrate_at1 == 0.5
assert hitrate_at2 == 0.5
assert hitrate_at3 == 1.0

# check 1 simple case
y_pred = [0.5, 0.2]
Expand All @@ -24,3 +27,20 @@ def test_hitrate():

hitrate_at2 = hitrate(torch.Tensor([y_pred]), torch.Tensor([y_true]), k)[0]
assert hitrate_at2 == 0.0

# check batch case
y_pred1 = [4.0, 2.0, 3.0, 1.0]
y_pred2 = [1.0, 2.0, 3.0, 4.0]
y_true1 = [0, 0, 1.0, 1.0]
y_true2 = [0, 0, 0.0, 0.0]
k = [1, 2, 3, 4]

y_pred_torch = torch.Tensor([y_pred1, y_pred2])
y_true_torch = torch.Tensor([y_true1, y_true2])

hitrate_at1, hitrate_at2, hitrate_at3, hitrate_at4 = hitrate(y_pred_torch, y_true_torch, k)

assert hitrate_at1 == 0.0
assert hitrate_at2 == 0.25
assert hitrate_at3 == 0.25
assert hitrate_at4 == 0.5