-
Notifications
You must be signed in to change notification settings - Fork 402
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
[metrics] AUROC Metric can't handle 0 observations of a class with multiclass classifier #348
Comments
I've found a hack for this by subclassing AUROC: class FixedAUC(torchmetrics.AUROC):
def update(self, preds: torch.Tensor, target: torch.Tensor):
num_classes = preds.shape[1]
zero_obs_mask = torch.tensor([(target == c).sum() > 0 for c in range(num_classes)])
preds = preds[:, zero_obs_mask]
target = target[:, zero_obs_mask]
self.num_classes = zero_obs_mask.sum().int()
super().update(torch.softmax(preds, axis=-1).data, target.argmax(axis=-1).int().data) I will be making a cleaner fix in torch and asking for a PR |
Hi! thanks for your contribution!, great first issue! |
Hi @BeyondTheProof, I transferred the issue to the torchmetrics repo. |
Hi @SkafteNicki, thank you for the response and transferring the issue! I am just calling the |
Could you provide an example of how you are using the metric? |
In my subclassed lightning module, I have metrics that are calculated at the end of each epoch:
When I calculate all my losses, I also calculate some other metrics (if there are any bugs, it is only because I modified it to simplify as much as possible):
I hope this helps. If there's anything else you need, please let me know! |
@SkafteNicki In this subclass, there is an error that arises when some batches have only two classes, others have more. I implemented a much cleaner subclass by just subclassing Metrics:
|
@BeyondTheProof I thinks that it will be better to add the line
|
Hi @BeyondTheProof, thanks for getting back to me. It indeed seems to be a problem, even though I would consider this a very corner case. Would you be up for sending a fix to our current AUROC implementation? |
Hi @SkafteNicki, good point about the user warning. Also, in the implementation above, the target only allows 2D tensors, i.e., a |
Thanks for the catch @KimDaeUng, you're totally right! |
@SkafteNicki Submitted a PR here: #376 Thanks! |
Hi @SkafteNicki, just following up on this. I have an approval from Borda, but still need one more :) |
The tests are failing in the PR though? |
Ah, sorry, I didn't catch that. Will fix. Thank you! |
I'm attempting to calculate AUROC for a multiclass problem where some classes are very rare, occasionally never seen, and I'm getting the following error:
raise ValueError("No positive samples in targets, true positive value should be meaningless")
In the case of 0 observations, I feel the
average='weighted'
should work, since the contribution to the final AUROC should be 0 regardless. One can think of other scenarios where there are a very high number of classes, some of which will happen to not be seen in some dataset.Originally posted by @BeyondTheProof in Lightning-AI/pytorch-lightning#2210 (comment)
The text was updated successfully, but these errors were encountered: