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

Support for pytorch-lightning #8

Closed
RoyBenjamin opened this issue Jan 17, 2022 · 5 comments
Closed

Support for pytorch-lightning #8

RoyBenjamin opened this issue Jan 17, 2022 · 5 comments
Labels
enhancement New feature or request

Comments

@RoyBenjamin
Copy link

RoyBenjamin commented Jan 17, 2022

I'm using pytorch-lightning and am experiencing this issue TypeError: step() got an unexpected keyword argument 'closure' when using PESG. Would appreciate your assistance if you managed to use LibAUC and pytorch-lightning together.

Thanks for your time and amazing work!

Usage is as follows (some lines of code omitted to keep it concise):

import pytorch_lightning as pl
...
# for AUC margin loss
from libauc.losses import AUCMLoss
from libauc.optimizers import PESG

# implementation for the model used in self.classifier
...

class Module(pl.LightningModule):
# implementation of methods unrelated to the error message
...

  def configure_optimizers(self):
    opt = PESG(self.classifier, a=self.criterion.a, b=self.criterion.b, alpha=self.criterion.alpha,
               lr=self.learning_rate, gamma=self.gamma, margin=self.margin, weight_decay=self.weight_decay)
    return opt
@ahmed1996said
Copy link

Any progress with this? @RoyBenjamin

@RoyBenjamin
Copy link
Author

RoyBenjamin commented Feb 7, 2022

Unfortunately no.

Any progress with this? @RoyBenjamin

@yzhuoning yzhuoning added the enhancement New feature or request label Feb 8, 2022
@yzhuoning
Copy link
Collaborator

Thanks for your feedback! I just pushed an update that allows you to work with pytorch-lightning. You can use pip install -U libauc==1.1.9rc0 to update your local library. Let me know if this works for you.

@RoyBenjamin
Copy link
Author

RoyBenjamin commented Feb 20, 2022

From my initial testing it seems to work perfect, I have one fix to suggest. For libuc/losses/losses.py in AUCM_MultiLabel change to what I have here. This solves a device issue, the reason it is written this way is for consistency with what you have for AUCMLoss.

def __init__(self, margin=1.0, imratio=None, device=None):
    super(AUCMLoss, self).__init__()
    if not device:
        self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    else:
        self.device = device
    self.margin = margin
    self.p = imratio
    # https://discuss.pytorch.org/t/valueerror-cant-optimize-a-non-leaf-tensor/21751
    self.a = torch.zeros(1, dtype=torch.float32, device=self.device, requires_grad=True).to(self.device)
    self.b = torch.zeros(1, dtype=torch.float32, device=self.device, requires_grad=True).to(self.device)
    self.alpha = torch.zeros(1, dtype=torch.float32, device=self.device, requires_grad=True).to(self.device)

@yzhuoning
Copy link
Collaborator

Good suggestion! I will fix it in next update. Please reopen the issue if you have other questions.

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

No branches or pull requests

3 participants