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

Problem with PolynomialLR #15

Closed
Nacriema opened this issue Nov 10, 2021 · 5 comments
Closed

Problem with PolynomialLR #15

Nacriema opened this issue Nov 10, 2021 · 5 comments

Comments

@Nacriema
Copy link

Thank you very much for sharing the code with us. Recently, I inspect and test all the code that you provided. I notice that your custom PolynomialLR inside the schedulers packet returns the result the same as the ConstantLR does. Please let me know if it is a bug. Thank you in advance.

@monniert
Copy link
Owner

Hi thanks for the interest in the project! Indeed after a quick inspection this part is buggy, it is a feature I started to implement but I have never used it in the end. I will remove it, if you are interested in other schedulers there are now great built-in modules in pytorch (see https://pytorch.org/docs/stable/optim.html)
Best, Tom

@Nacriema
Copy link
Author

Hi, thank you for your response. Your code is very clean, I learned a lot from your coding style. Thank you very much !

@Nacriema
Copy link
Author

Nacriema commented Nov 11, 2021

Hi, from this discussion thread: https://discuss.pytorch.org/t/solved-learning-rate-decay/6825/4, I have been implemented the PolynomialLR scheduler, but I'm not sure if it is correct. Could you please check it, thank you in advance:

class PolynomialLR(_LRScheduler):
    def __init__(self, optimizer, max_iter, decay_iter=1, power=0.9, last_epoch=-1):
        self.decay_iter = decay_iter
        self.max_iter = max_iter
        self.power = power
        super(PolynomialLR, self).__init__(optimizer, last_epoch)

    def get_lr(self):
        if self.last_epoch % self.decay_iter == 0 or self.last_epoch % self.max_iter == 0:
            factor = (1 - self.last_epoch / float(self.max_iter)) ** self.power
            return [base_lr * factor for base_lr in self.base_lrs]
        return [group['lr'] for group in self.optimizer.param_groups]

    def __str__(self):
        params = [
            'optimizer: {}'.format(self.optimizer.__class__.__name__),
            'decay_iter: {}'.format(self.decay_iter),
            'max_iter: {}'.format(self.max_iter),
            'gamma: {}'.format(self.gamma),
        ]
        return '{}({})'.format(self.__class__.__name__, ', '.join(params))

@monniert
Copy link
Owner

Mmh from a quick look it looks good to me, to be sure you should try on a basic example and print the LR when it changes during training

@Nacriema
Copy link
Author

Hi, this is the learning rate scheme I've got for testing:

  • With max_iter=100, decay_iter=1, power=1.0
  • With max_iter=100, decay_iter=5, power=1.0
  • With max_iter=100, decay_iter=1, power=2.0
  • With max_iter=100, decay_iter=5, power=2.0

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