lr_scheduler.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import math
  15. from paddle.optimizer.lr import LRScheduler
  16. class CyclicalCosineDecay(LRScheduler):
  17. def __init__(self,
  18. learning_rate,
  19. T_max,
  20. cycle=1,
  21. last_epoch=-1,
  22. eta_min=0.0,
  23. verbose=False):
  24. """
  25. Cyclical cosine learning rate decay
  26. A learning rate which can be referred in https://arxiv.org/pdf/2012.12645.pdf
  27. Args:
  28. learning rate(float): learning rate
  29. T_max(int): maximum epoch num
  30. cycle(int): period of the cosine decay
  31. last_epoch (int, optional): The index of last epoch. Can be set to restart training. Default: -1, means initial learning rate.
  32. eta_min(float): minimum learning rate during training
  33. verbose(bool): whether to print learning rate for each epoch
  34. """
  35. super(CyclicalCosineDecay, self).__init__(learning_rate, last_epoch,
  36. verbose)
  37. self.cycle = cycle
  38. self.eta_min = eta_min
  39. def get_lr(self):
  40. if self.last_epoch == 0:
  41. return self.base_lr
  42. reletive_epoch = self.last_epoch % self.cycle
  43. lr = self.eta_min + 0.5 * (self.base_lr - self.eta_min) * \
  44. (1 + math.cos(math.pi * reletive_epoch / self.cycle))
  45. return lr