Skip to content

Cannot replicate example on MultiLabelAUCMLoss #68

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

Closed
Innoversa opened this issue Jan 11, 2025 · 2 comments
Closed

Cannot replicate example on MultiLabelAUCMLoss #68

Innoversa opened this issue Jan 11, 2025 · 2 comments

Comments

@Innoversa
Copy link

Dear developers of LibAUC,
Thank you for open-sourcing your wonderful work! As I utilize the package's MultilabelAUCLoss for my multi-label classification task, I keep encountering an error: "IndexError: too many indices for tensor of dimension 1". I encounter the same error when running the provided example on the website:

loss_fn = MultiLabelAUCMLoss(margin=1.0, num_labels=10)
y_pred = torch.randn(32, 10, requires_grad=True)
y_true = torch.empty(32, dtype=torch.long).random_(2)
loss = loss_fn(y_pred, y_true)

I couldn't find more concrete examples regarding multi-label problems, I also tried to a few debuggings, including reshaping y_true to (32, 1), send all of them into the same device. However, I've yet figured out how to feed the correct shapes to the loss.
My specific problem involves calculating loss between pred=(batch, 3) and label=(batch) for a 3-class classification.

Thank you!

@GangLii
Copy link
Collaborator

GangLii commented Jan 16, 2025

Hi Innoversa, thanks for your interest in our library. Since MultiLabelAUCMLoss is designed for multi-label classification tasks, the shape of y_true should be the same as the shape of y_pred. Therefore, there was a typo in the example code. We apologize for the caused confusion. You can try the example I've included below.

from libauc.losses import MultiLabelAUCMLos

loss_fn = MultiLabelAUCMLoss(margin=1.0, num_labels=10, device='cpu')
y_pred = torch.randn(32, 10, requires_grad=True)
y_true = torch.empty((32,10), dtype=torch.long).random_(2)
loss = loss_fn(y_pred, y_true)

If you'd like to utilize MultiLabelAUCMLoss for multi-class classification, you might need to convert the multi-class labels to a multi-label format. For example, convert labels [0,1,1,2] to [[1,0,0], [0,1,0], [0,1,0], [0,0,1]].

@Innoversa
Copy link
Author

Thank you! I was able to utilize the package after one-hod encoding the labels.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants