learning_rate.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. from __future__ import absolute_import
  15. from __future__ import division
  16. from __future__ import print_function
  17. from __future__ import unicode_literals
  18. from paddle.optimizer import lr
  19. from .lr_scheduler import CyclicalCosineDecay
  20. class Linear(object):
  21. """
  22. Linear learning rate decay
  23. Args:
  24. lr (float): The initial learning rate. It is a python float number.
  25. epochs(int): The decay step size. It determines the decay cycle.
  26. end_lr(float, optional): The minimum final learning rate. Default: 0.0001.
  27. power(float, optional): Power of polynomial. Default: 1.0.
  28. last_epoch (int, optional): The index of last epoch. Can be set to restart training. Default: -1, means initial learning rate.
  29. """
  30. def __init__(self,
  31. learning_rate,
  32. epochs,
  33. step_each_epoch,
  34. end_lr=0.0,
  35. power=1.0,
  36. warmup_epoch=0,
  37. last_epoch=-1,
  38. **kwargs):
  39. super(Linear, self).__init__()
  40. self.learning_rate = learning_rate
  41. self.epochs = epochs * step_each_epoch
  42. self.end_lr = end_lr
  43. self.power = power
  44. self.last_epoch = last_epoch
  45. self.warmup_epoch = round(warmup_epoch * step_each_epoch)
  46. def __call__(self):
  47. learning_rate = lr.PolynomialDecay(
  48. learning_rate=self.learning_rate,
  49. decay_steps=self.epochs,
  50. end_lr=self.end_lr,
  51. power=self.power,
  52. last_epoch=self.last_epoch)
  53. if self.warmup_epoch > 0:
  54. learning_rate = lr.LinearWarmup(
  55. learning_rate=learning_rate,
  56. warmup_steps=self.warmup_epoch,
  57. start_lr=0.0,
  58. end_lr=self.learning_rate,
  59. last_epoch=self.last_epoch)
  60. return learning_rate
  61. class Cosine(object):
  62. """
  63. Cosine learning rate decay
  64. lr = 0.05 * (math.cos(epoch * (math.pi / epochs)) + 1)
  65. Args:
  66. lr(float): initial learning rate
  67. step_each_epoch(int): steps each epoch
  68. epochs(int): total training epochs
  69. last_epoch (int, optional): The index of last epoch. Can be set to restart training. Default: -1, means initial learning rate.
  70. """
  71. def __init__(self,
  72. learning_rate,
  73. step_each_epoch,
  74. epochs,
  75. warmup_epoch=0,
  76. last_epoch=-1,
  77. **kwargs):
  78. super(Cosine, self).__init__()
  79. self.learning_rate = learning_rate
  80. self.T_max = step_each_epoch * epochs
  81. self.last_epoch = last_epoch
  82. self.warmup_epoch = round(warmup_epoch * step_each_epoch)
  83. def __call__(self):
  84. learning_rate = lr.CosineAnnealingDecay(
  85. learning_rate=self.learning_rate,
  86. T_max=self.T_max,
  87. last_epoch=self.last_epoch)
  88. if self.warmup_epoch > 0:
  89. learning_rate = lr.LinearWarmup(
  90. learning_rate=learning_rate,
  91. warmup_steps=self.warmup_epoch,
  92. start_lr=0.0,
  93. end_lr=self.learning_rate,
  94. last_epoch=self.last_epoch)
  95. return learning_rate
  96. class Step(object):
  97. """
  98. Piecewise learning rate decay
  99. Args:
  100. step_each_epoch(int): steps each epoch
  101. learning_rate (float): The initial learning rate. It is a python float number.
  102. step_size (int): the interval to update.
  103. gamma (float, optional): The Ratio that the learning rate will be reduced. ``new_lr = origin_lr * gamma`` .
  104. It should be less than 1.0. Default: 0.1.
  105. last_epoch (int, optional): The index of last epoch. Can be set to restart training. Default: -1, means initial learning rate.
  106. """
  107. def __init__(self,
  108. learning_rate,
  109. step_size,
  110. step_each_epoch,
  111. gamma,
  112. warmup_epoch=0,
  113. last_epoch=-1,
  114. **kwargs):
  115. super(Step, self).__init__()
  116. self.step_size = step_each_epoch * step_size
  117. self.learning_rate = learning_rate
  118. self.gamma = gamma
  119. self.last_epoch = last_epoch
  120. self.warmup_epoch = round(warmup_epoch * step_each_epoch)
  121. def __call__(self):
  122. learning_rate = lr.StepDecay(
  123. learning_rate=self.learning_rate,
  124. step_size=self.step_size,
  125. gamma=self.gamma,
  126. last_epoch=self.last_epoch)
  127. if self.warmup_epoch > 0:
  128. learning_rate = lr.LinearWarmup(
  129. learning_rate=learning_rate,
  130. warmup_steps=self.warmup_epoch,
  131. start_lr=0.0,
  132. end_lr=self.learning_rate,
  133. last_epoch=self.last_epoch)
  134. return learning_rate
  135. class Piecewise(object):
  136. """
  137. Piecewise learning rate decay
  138. Args:
  139. boundaries(list): A list of steps numbers. The type of element in the list is python int.
  140. values(list): A list of learning rate values that will be picked during different epoch boundaries.
  141. The type of element in the list is python float.
  142. last_epoch (int, optional): The index of last epoch. Can be set to restart training. Default: -1, means initial learning rate.
  143. """
  144. def __init__(self,
  145. step_each_epoch,
  146. decay_epochs,
  147. values,
  148. warmup_epoch=0,
  149. last_epoch=-1,
  150. **kwargs):
  151. super(Piecewise, self).__init__()
  152. self.boundaries = [step_each_epoch * e for e in decay_epochs]
  153. self.values = values
  154. self.last_epoch = last_epoch
  155. self.warmup_epoch = round(warmup_epoch * step_each_epoch)
  156. def __call__(self):
  157. learning_rate = lr.PiecewiseDecay(
  158. boundaries=self.boundaries,
  159. values=self.values,
  160. last_epoch=self.last_epoch)
  161. if self.warmup_epoch > 0:
  162. learning_rate = lr.LinearWarmup(
  163. learning_rate=learning_rate,
  164. warmup_steps=self.warmup_epoch,
  165. start_lr=0.0,
  166. end_lr=self.values[0],
  167. last_epoch=self.last_epoch)
  168. return learning_rate
  169. class CyclicalCosine(object):
  170. """
  171. Cyclical cosine learning rate decay
  172. Args:
  173. learning_rate(float): initial learning rate
  174. step_each_epoch(int): steps each epoch
  175. epochs(int): total training epochs
  176. cycle(int): period of the cosine learning rate
  177. last_epoch (int, optional): The index of last epoch. Can be set to restart training. Default: -1, means initial learning rate.
  178. """
  179. def __init__(self,
  180. learning_rate,
  181. step_each_epoch,
  182. epochs,
  183. cycle,
  184. warmup_epoch=0,
  185. last_epoch=-1,
  186. **kwargs):
  187. super(CyclicalCosine, self).__init__()
  188. self.learning_rate = learning_rate
  189. self.T_max = step_each_epoch * epochs
  190. self.last_epoch = last_epoch
  191. self.warmup_epoch = round(warmup_epoch * step_each_epoch)
  192. self.cycle = round(cycle * step_each_epoch)
  193. def __call__(self):
  194. learning_rate = CyclicalCosineDecay(
  195. learning_rate=self.learning_rate,
  196. T_max=self.T_max,
  197. cycle=self.cycle,
  198. last_epoch=self.last_epoch)
  199. if self.warmup_epoch > 0:
  200. learning_rate = lr.LinearWarmup(
  201. learning_rate=learning_rate,
  202. warmup_steps=self.warmup_epoch,
  203. start_lr=0.0,
  204. end_lr=self.learning_rate,
  205. last_epoch=self.last_epoch)
  206. return learning_rate