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

add support to F1 metric values #439

Merged
merged 1 commit into from
Dec 11, 2024
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 src/pytorch_ie/metrics/f1.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def _compute(self) -> Dict[str, Dict[str, float]]:
p = tp / (tp + fp)
r = tp / (tp + fn)
f1 = 2 * p * r / (p + r)
res[label] = {"f1": f1, "p": p, "r": r}
res[label] = {"f1": f1, "p": p, "r": r, "s": tp + fn}
if self.per_label and label in self.labels:
res["MACRO"]["f1"] += f1 / len(self.labels)
res["MACRO"]["p"] += p / len(self.labels)
Expand Down
18 changes: 9 additions & 9 deletions tests/metrics/test_f1.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def test_f1(documents):
metric(documents)
# tp, fp, fn for micro
assert dict(metric.counts) == {"MICRO": (3, 2, 0)}
assert metric.compute() == {"MICRO": {"f1": 0.7499999999999999, "p": 0.6, "r": 1.0}}
assert metric.compute() == {"MICRO": {"f1": 0.7499999999999999, "p": 0.6, "r": 1.0, "s": 3}}


def test_f1_per_label(documents):
Expand All @@ -67,10 +67,10 @@ def test_f1_per_label(documents):
}
assert metric.compute() == {
"MACRO": {"f1": 0.5555555555555556, "p": 0.5, "r": 0.6666666666666666},
"MICRO": {"f1": 0.7499999999999999, "p": 0.6, "r": 1.0},
"cat": {"f1": 0.0, "p": 0.0, "r": 0.0},
"company": {"f1": 0.6666666666666666, "p": 0.5, "r": 1.0},
"animal": {"f1": 1.0, "p": 1.0, "r": 1.0},
"MICRO": {"f1": 0.7499999999999999, "p": 0.6, "r": 1.0, "s": 3},
"animal": {"f1": 1.0, "p": 1.0, "r": 1.0, "s": 2},
"cat": {"f1": 0.0, "p": 0.0, "r": 0.0, "s": 0},
"company": {"f1": 0.6666666666666666, "p": 0.5, "r": 1.0, "s": 1},
}


Expand All @@ -86,10 +86,10 @@ def test_f1_per_label_inferred(documents):
}
assert metric.compute() == {
"MACRO": {"f1": 0.5555555555555556, "p": 0.5, "r": 0.6666666666666666},
"MICRO": {"f1": 0.7499999999999999, "p": 0.6, "r": 1.0},
"animal": {"f1": 1.0, "p": 1.0, "r": 1.0},
"cat": {"f1": 0.0, "p": 0.0, "r": 0.0},
"company": {"f1": 0.6666666666666666, "p": 0.5, "r": 1.0},
"MICRO": {"f1": 0.7499999999999999, "p": 0.6, "r": 1.0, "s": 3},
"animal": {"f1": 1.0, "p": 1.0, "r": 1.0, "s": 2},
"cat": {"f1": 0.0, "p": 0.0, "r": 0.0, "s": 0},
"company": {"f1": 0.6666666666666666, "p": 0.5, "r": 1.0, "s": 1},
}


Expand Down
Loading