ignite icon indicating copy to clipboard operation
ignite copied to clipboard

[Inconsistency]: Fbeta metric lacks is_multilabel and advanced average parameters

Open Manimaran-tech opened this issue 8 months ago • 20 comments

🚀 Feature Request / Bug Report

Currently, the Fbeta metric in PyTorch-Ignite behaves inconsistently compared to its underlying components, Precision and Recall. While Precision and Recall support the is_multilabel flag and multiple average modes (macro, micro, weighted, samples), Fbeta only supports a boolean average and lacks the is_multilabel flag entirely.

Motivation

When a user wants to compute a multi-label F1-score, they expect to be able to do:

# This currently fails with TypeError: Fbeta() got an unexpected keyword argument 'is_multilabel'
f1 = Fbeta(beta=1.0, is_multilabel=True)

Currently, the user is forced to manually instantiate \Precision\ and \Recall\ with \verage=False\ and \is_multilabel=True\ and pass them to \Fbeta\. I noticed that even in historical issues (e.g., #2444), maintainers have provided this manual workaround instead of having a native API path in \Fbeta\. This adds unnecessary boilerplate and creates an inconsistent experience across classification metrics.

### Proposed Implementation
I would like to resolve this inconsistency by:
1. Adding \is_multilabel: bool = False\ to the \Fbeta\ signature.
2. Passing \is_multilabel\ down to the internally created \Precision\ and \Recall\ objects.
3. Updating the \verage\ parameter handling to eventually support more granular modes (micro, weighted, etc.) or documenting why it differs from \Precision/Recall\.

I have already verified this limitation locally and am prepared to submit a PR that includes:
- Updated \Fbeta\ signature and logic.
- Comprehensive unit tests for multi-label F-beta scores (which are currently missing in \	est_fbeta.py\).

@vfdev-5 I'd love to hear your thoughts on aligning this API!

Manimaran-tech avatar Mar 05 '26 18:03 Manimaran-tech

Can you please provide some code examples so we can try replicating it ourselves?

aaishwarymishra avatar Mar 06 '26 13:03 aaishwarymishra

import torch from ignite.metrics import Fbeta

def test_fbeta_multilabel(): # Attempting to initialize Fbeta with is_multilabel=True # This currently raises: TypeError: Fbeta() got an unexpected keyword argument 'is_multilabel' fbeta = Fbeta(beta=1.0, is_multilabel=True)

y_pred = torch.tensor([[0.8, 0.2, 0.9], [0.1, 0.9, 0.2]])
y_true = torch.tensor([[1, 0, 1], [0, 1, 0]])

fbeta.update((y_pred, y_true))
print("Fbeta Score:", fbeta.compute())

test_fbeta_multilabel()

try this code sample @aaishwarymishra

Manimaran-tech avatar Mar 06 '26 16:03 Manimaran-tech

@Manimaran-tech your original and code message markdown formatting is weird it is hard to understand, can you please try to fix it so I can get better idea :)

aaishwarymishra avatar Mar 06 '26 17:03 aaishwarymishra

@aaishwarymishra Apologies for the weird formatting! I accidentally used backslashes (\) instead of backticks (`) for the inline code and code blocks, which broke the markdown rendering. I have just edited and fixed the original description. Please let me know your thoughts on the proposal!

Manimaran-tech avatar Mar 06 '26 18:03 Manimaran-tech

      P = Precision(average=False, is_multilabel=True)
      R = Recall(average=False, is_multilabel=True)
      metric = Fbeta(beta=1.0, average=False, precision=P, recall=R)
      metric.attach(default_evaluator, "f-beta")
      y_true = torch.tensor([2, 0, 2, 1, 0, 1])
      y_pred = torch.tensor([
          [0.0266, 0.1719, 0.3055],
          [0.6886, 0.3978, 0.8176],
          [0.9230, 0.0197, 0.8395],
          [0.1785, 0.2670, 0.6084],
          [0.8448, 0.7177, 0.7288],
          [0.7748, 0.9542, 0.8573],
      ])

We can create fbeta without precision and recall as the metric creates default instances of them if not passed, if user wants to has specific configuration like is_multilabel he can pass the instance himself, like in the above example.

aaishwarymishra avatar Mar 07 '26 06:03 aaishwarymishra

Hi @aaishwarymishra! Thanks for taking a look.

Yes, passing pre-configured Precision and Recall objects is absolutely a valid workaround. My point is about the API developer experience and inconsistency.

Both Precision and Recall have is_multilabel out-of-the-box natively, yet the higher-level metric composed of them (Fbeta) drops support for this parameter, forcing the users to dig into understanding how Fbeta handles precision and recall under the hood and then write that extra boilerplate code.

I think adding is_multilabel directly to Fbeta makes the API much more intuitive, consistent with its underlying metrics, and improves the UX. And since the fix is lightweight, it brings it in line with the other metrics naturally! Let me know what you think.

Manimaran-tech avatar Mar 07 '26 07:03 Manimaran-tech

@vfdev-5 what do you think?

aaishwarymishra avatar Mar 07 '26 08:03 aaishwarymishra

@Manimaran-tech how do you expect to handle the following:

Fbeta accepts pre-instantiated Precision and Recall as well. How would we handle the following conflict then?

P = Precision(average=False, is_multilabel=False)
R = Recall(average=False, is_multilabel=False)

# What happens if the flag conflicts with the provided metrics?
f1 = Fbeta(beta=1.0, is_multilabel=True, precision=P, recall=R)

TahaZahid05 avatar Mar 07 '26 13:03 TahaZahid05

Hey @TahaZahid05, great question! The conflict scenario you mentioned is straightforward to handle — and Fbeta actually already solves the exact same kind of conflict for the average parameter today.

Current behavior for average conflict (already in fbeta.py):

# fbeta.py, lines 167-168
elif precision._average:
    raise ValueError("Input precision metric should have average=False")

If a user passes Precision(average=True) to Fbeta, it raises a ValueError. We apply the same validation pattern for is_multilabel:

# Proposed addition
if precision is not None and precision._is_multilabel != is_multilabel:
    raise ValueError(
        f"Input precision metric has is_multilabel={precision._is_multilabel}, "
        f"but Fbeta was called with is_multilabel={is_multilabel}"
    )

So for your example:

P = Precision(average=False, is_multilabel=False)
R = Recall(average=False, is_multilabel=False)
f1 = Fbeta(beta=1.0, is_multilabel=True, precision=P, recall=R)
# → Raises ValueError: Input precision metric has is_multilabel=False, but Fbeta was called with is_multilabel=True

No silent bugs, no ambiguous behavior — just a clean, explicit error, consistent with how the library already handles parameter conflicts.

Existing precedent inside the library — ClassificationReport:

The strongest evidence is that ClassificationReport already accepts is_multilabel at its top level and passes it down to Precision and Recall internally:

# classification_report.py, lines 120-124
precision = Precision(average=False, is_multilabel=is_multilabel, output_transform=output_transform, device=device)
recall = Recall(average=False, is_multilabel=is_multilabel, output_transform=output_transform, device=device)
fbeta = Fbeta(beta, average=False, precision=precision, recall=recall)

The library's own higher-level metric already needs to do this workaround because Fbeta is missing is_multilabel. Adding is_multilabel to Fbeta would make both Fbeta and ClassificationReport cleaner and more consistent.

@vfdev-5 would love to hear your thoughts on this approach!

Manimaran-tech avatar Mar 07 '26 14:03 Manimaran-tech

Hi @vfdev-5 and everyone, this looks like a cool issue to work on.

It seems like the tech proposal from @Manimaran-tech is already well settled, adding is_multilabel: bool = False to Fbeta, passing through Precision/Recall and raising a ValueError on conflict when the pre instantiated metrics are in, like the current average conflict check and consistent with the way that ClassificationReport handles it.

@vfdev-5 since your input was asked earlier, but I didnt see a response, are there any issues or objections or can I start implementing?

virvihuta avatar Jul 27 '26 19:07 virvihuta

Hi @vfdev-5,

I'd be interested in working on this issue if it's still available.

I went through the discussion, and my understanding is that the intended approach would be to:

Add is_multilabel to Fbeta. Pass it to the internally created Precision and Recall instances when they aren't provided explicitly. Raise a ValueError if user-provided Precision or Recall instances conflict with the is_multilabel value passed to Fbeta, similar to the existing validation for average. Add the corresponding unit tests for the new behavior.

Before I start working on it, I just wanted to confirm that this aligns with the intended direction and that no one else is actively implementing it. If so, I'd be happy to work on a PR.

SreyasM24 avatar Jul 29 '26 18:07 SreyasM24

Hi @SreyasM24, I actually commented on this two days ago and Im planning to work on it, following the same design approach you showed. Id be happy to loop you in if there is a way to split it up, but otherwise Id like to go ahead with my implementation. @vfdev-5, would be great to get a call on this if you have time.

virvihuta avatar Jul 29 '26 19:07 virvihuta

Hi @SreyasM24, I actually commented on this two days ago and Im planning to work on it, following the same design approach you showed. Id be happy to loop you in if there is a way to split it up, but otherwise Id like to go ahead with my implementation. @vfdev-5, would be great to get a call on this if you have time.

Thanks for the update! Let's see what @vfdev-5 suggests. If there's a sensible way to split the work, I'd be be happy to contribute. Otherwise, feel free to continue with your implementation..

SreyasM24 avatar Jul 30 '26 03:07 SreyasM24

@virvihuta @SreyasM24 I'm not against improving Fbeta by adding all options for average and new arg is_multilabel, the updates should be consistent with Precision and Recall and tested against sklearn.

vfdev-5 avatar Jul 30 '26 10:07 vfdev-5

Thanks @vfdev-5. Makes sense, Ill is_multilabel on Fbeta consistent with Precision and Recall existing pattern and add tests validating against sklearn fbeta_score for the multilabel cases. @SreyasM24 happy to have you review once its up, or if you would rather split it (for example you take the sklearn-comparison tests while I handle the core Fbeta changes), let me know and we can coordinate.

virvihuta avatar Jul 30 '26 10:07 virvihuta

Thanks @vfdev-5. Makes sense, Ill is_multilabel on Fbeta consistent with Precision and Recall existing pattern and add tests validating against sklearn fbeta_score for the multilabel cases. @SreyasM24 happy to have you review once its up, or if you would rather split it (for example you take the sklearn-comparison tests while I handle the core Fbeta changes), let me know and we can coordinate.

Thanks! I'd be happy to help with the sklearn comparison tests. Let me first look through the existing test structure and see how best to add the multilabel test cases. I'll share what I come up with so we can coordinate.

SreyasM24 avatar Jul 30 '26 13:07 SreyasM24

Sounds good @SreyasM24, Ill start on the Fbeta changes now, will share progress once its in a good shape.

virvihuta avatar Jul 30 '26 14:07 virvihuta

@vfdev-5 just to make sure I scope this right, when you say "adding all options for average," do you mean expanding Fbeta's average parameter to match Precision/Recall full set (None, bool, 'macro', 'micro', 'weighted', 'samples'), on top of adding is_multilabel? Can you confirm before I start so I build the right thing.

virvihuta avatar Jul 30 '26 14:07 virvihuta

Do you mean expanding Fbeta's average parameter to match Precision/Recall full set (None, bool, 'macro', 'micro', 'weighted', 'samples'), on top of adding is_multilabel?

yes, if this is possible. Take sklearn as a reference

vfdev-5 avatar Jul 31 '26 12:07 vfdev-5

@vfdev-5 quick question, right now Fbeta averages the per class Fbeta scores at the end (sub-metrics always use average=False). For micro/weighted/samples to match sklearn, I think the internal precision/recall need to be built with that average instead. Does that seem right?

virvihuta avatar Aug 01 '26 14:08 virvihuta