lr_scheduler.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # Copyright (c) 2022 NVIDIA Corporation. All rights reserved.
  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 sys
  15. import logging
  16. import paddle
  17. class Cosine:
  18. """
  19. Cosine learning rate decay.
  20. lr = eta_min + 0.5 * (learning_rate - eta_min) * (cos(epoch * (PI / epochs)) + 1)
  21. Args:
  22. args(Namespace): Arguments obtained from ArgumentParser.
  23. step_each_epoch(int): The number of steps in each epoch.
  24. last_epoch (int, optional): The index of last epoch. Can be set to restart training.
  25. Default: -1, meaning initial learning rate.
  26. """
  27. def __init__(self, args, step_each_epoch, last_epoch=-1):
  28. super().__init__()
  29. if args.warmup_epochs >= args.epochs:
  30. args.warmup_epochs = args.epochs
  31. self.learning_rate = args.lr
  32. self.T_max = (args.epochs - args.warmup_epochs) * step_each_epoch
  33. self.eta_min = 0.0
  34. self.last_epoch = last_epoch
  35. self.warmup_steps = round(args.warmup_epochs * step_each_epoch)
  36. self.warmup_start_lr = args.warmup_start_lr
  37. def __call__(self):
  38. learning_rate = paddle.optimizer.lr.CosineAnnealingDecay(
  39. learning_rate=self.learning_rate,
  40. T_max=self.T_max,
  41. eta_min=self.eta_min,
  42. last_epoch=self.
  43. last_epoch) if self.T_max > 0 else self.learning_rate
  44. if self.warmup_steps > 0:
  45. learning_rate = paddle.optimizer.lr.LinearWarmup(
  46. learning_rate=learning_rate,
  47. warmup_steps=self.warmup_steps,
  48. start_lr=self.warmup_start_lr,
  49. end_lr=self.learning_rate,
  50. last_epoch=self.last_epoch)
  51. return learning_rate
  52. def build_lr_scheduler(args, step_each_epoch):
  53. """
  54. Build a learning rate scheduler.
  55. Args:
  56. args(Namespace): Arguments obtained from ArgumentParser.
  57. step_each_epoch(int): The number of steps in each epoch.
  58. return:
  59. lr(paddle.optimizer.lr.LRScheduler): A learning rate scheduler.
  60. """
  61. # Turn last_epoch to last_step, since we update lr each step instead of each epoch.
  62. last_step = args.start_epoch * step_each_epoch - 1
  63. learning_rate_mod = sys.modules[__name__]
  64. lr = getattr(learning_rate_mod, args.lr_scheduler)(args, step_each_epoch,
  65. last_step)
  66. if not isinstance(lr, paddle.optimizer.lr.LRScheduler):
  67. lr = lr()
  68. logging.info("build lr %s success..", lr)
  69. return lr