Skip to content

feat: Add average parameter to Accuracy for per-label multilabel output - #3810

Open
zongyang078 wants to merge 7 commits into
pytorch:masterfrom
zongyang078:feat/accuracy-average-param
Open

feat: Add average parameter to Accuracy for per-label multilabel output#3810
zongyang078 wants to merge 7 commits into
pytorch:masterfrom
zongyang078:feat/accuracy-average-param

Conversation

@zongyang078

Copy link
Copy Markdown

Fixes #513

Description:
Add average parameter to Accuracy for multilabel classification, aligning its API with Precision/Recall which already support per-label output via _BasePrecisionRecall.

When average=False and is_multilabel=True, compute() returns a per-label accuracy tensor of shape (num_labels,) using elementwise comparison, rather than the default subset accuracy scalar. Default behavior (average=None) is unchanged.

Prior attempts (PR #516, PR #542) stalled in 2019 over API design — both proposed a labelwise=True boolean flag, which was rejected. This PR uses the average parameter convention already established by Precision/Recall.

Scoped to not touch Accuracy's internal structure (no _prepare_output), staying orthogonal to #3568 / #3610.

Also updates two pre-existing tests (test_accuracy.py::test_accumulator_device, test_running_average.py::test_integration_batchwise) that assumed _num_correct is always a tensor right after reset() — it's now a plain int until the first update(), matching the int | torch.Tensor pattern Precision/Recall already use for _numerator/_denominator.

Check list:

  • [ x ] New tests are added (if a new feature is added)
  • [ x ] New doc strings: description and/or example code are in RST format
  • [ x ] Documentation is updated (if required)

Add average parameter to Accuracy, aligning its API with Precision/Recall
for multilabel classification. When average=False and is_multilabel=True,
compute() returns a per-label accuracy tensor instead of a scalar.

Fixes pytorch#513
…ment

- cast() the num_correct accumulator before .item() in compute(), matching
  the pattern Precision already uses for the same int|Tensor duality
- drop a comment that just restated the line below it
- test_running_average.py: compare _num_correct in a type-agnostic way,
  since it may now be a plain int (right after reset(), before update())
  instead of always a tensor

Fixes pytorch#513
@github-actions github-actions Bot added the module: metrics Metrics module label Jul 16, 2026
Comment thread ignite/metrics/accuracy.py Outdated
Comment thread ignite/metrics/accuracy.py Outdated
Comment thread ignite/metrics/accuracy.py Outdated
Comment thread ignite/metrics/accuracy.py Outdated
@aaishwarymishra

Copy link
Copy Markdown
Collaborator

My bad I missed the updates

Comment thread ignite/metrics/accuracy.py Outdated
:class:`~ignite.engine.engine.Engine`'s ``process_function``'s output into the
form expected by the metric. This can be useful if, for example, you have a multi-output model and
you want to compute the metric with respect to one of the outputs.
subset_accuracy: if True (default), computes subset accuracy for multilabel data

@aaishwarymishra aaishwarymishra Aug 13, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont actually like this name its confusing it does not tell what it does by just its name, maybe we can try label_average or something similar I am not sure.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed to per_label: bool = False — polarity now matches what it returns (per_label=True → per-label tensor).

@aaishwarymishra

Copy link
Copy Markdown
Collaborator

Also I feel many of the tests are redundant and can be removed or be paramatized. Sorry for late response, i missed the updates on the pr

… tests

Renames the parameter so its True/False polarity matches what it returns
(per-label tensor vs the existing scalar), instead of naming it after only
one side of the toggle. Also collapses the all_correct/all_wrong/
label_with_no_positives tests into one parametrized test, and drops the
module-level engine-integration test that duplicated
TestDistributed.test_integration_multilabel_per_label's gloo_cpu coverage.
@zongyang078

Copy link
Copy Markdown
Author

Also I feel many of the tests are redundant and can be removed or be paramatized. Sorry for late response, i missed the updates on the pr

Merged the all_correct/all_wrong/no_positives tests into one parametrized test, and dropped the duplicate module-level engine test (verified TestDistributed's gloo_cpu case already covers the same reset-per-epoch scenario by default).

@aaishwarymishra

Copy link
Copy Markdown
Collaborator

@vfdev-5 LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

module: metrics Metrics module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Label-wise metrics (Accuracy etc.) for multi-label problems

2 participants