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

Example of how to use scheduler #404

Open
brando90 opened this issue Dec 3, 2021 · 2 comments
Open

Example of how to use scheduler #404

brando90 opened this issue Dec 3, 2021 · 2 comments

Comments

@brando90
Copy link

brando90 commented Dec 3, 2021

I'd like to use the Adafactor scheduler as the hugging face code has (but their code does not work for CNNS).

questions as follow:
a) How do I use schedulers with pytorch-optimizer?
b) Can we add an example in the readme?
c) does adafactor have it's own scheduler here?

related:

@brando90
Copy link
Author

brando90 commented Dec 3, 2021

(btw, might it be useful to open the discussion feature from github?)

@brando90
Copy link
Author

brando90 commented Dec 3, 2021

would this work?

from torch.optim.lr_scheduler import LambdaLR

class AdafactorSchedulerUU(LambdaLR):
    """
    TODO: I think this should work for the torch_optimizer library...
    """

    def __init__(self, optimizer, initial_lr=0.0):
        def lr_lambda(_):
            return initial_lr

        for group in optimizer.param_groups:
            group["initial_lr"] = initial_lr

        super().__init__(optimizer, lr_lambda)
        for group in optimizer.param_groups:
            del group["initial_lr"]

    def get_lr(self):
        opt = self.optimizer
        lrs = [
            opt._get_lr(group, opt.state[group["params"][0]])
            for group in opt.param_groups
            if group["params"][0].grad is not None
        ]
        if len(lrs) == 0:
            lrs = self.base_lrs  # if called before stepping
        return lrs

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

1 participant