You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

lr_generator.py 3.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  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. # ============================================================================
  15. """learning rate generator"""
  16. import math
  17. import numpy as np
  18. def get_lr(lr_init, lr_end, lr_max, warmup_epochs, total_epochs, steps_per_epoch, lr_decay_mode):
  19. """
  20. generate learning rate array
  21. Args:
  22. lr_init(float): init learning rate
  23. lr_end(float): end learning rate
  24. lr_max(float): max learning rate
  25. warmup_epochs(int): number of warmup epochs
  26. total_epochs(int): total epoch of training
  27. steps_per_epoch(int): steps of one epoch
  28. lr_decay_mode(string): learning rate decay mode, including steps, poly, cosine or default
  29. Returns:
  30. np.array, learning rate array
  31. """
  32. lr_each_step = []
  33. total_steps = steps_per_epoch * total_epochs
  34. warmup_steps = steps_per_epoch * warmup_epochs
  35. if lr_decay_mode == 'steps':
  36. decay_epoch_index = [0.3 * total_steps,
  37. 0.6 * total_steps, 0.8 * total_steps]
  38. for i in range(total_steps):
  39. if i < decay_epoch_index[0]:
  40. lr = lr_max
  41. elif i < decay_epoch_index[1]:
  42. lr = lr_max * 0.1
  43. elif i < decay_epoch_index[2]:
  44. lr = lr_max * 0.01
  45. else:
  46. lr = lr_max * 0.001
  47. lr_each_step.append(lr)
  48. elif lr_decay_mode == 'poly':
  49. if warmup_steps != 0:
  50. inc_each_step = (float(lr_max) - float(lr_init)) / \
  51. float(warmup_steps)
  52. else:
  53. inc_each_step = 0
  54. for i in range(total_steps):
  55. if i < warmup_steps:
  56. lr = float(lr_init) + inc_each_step * float(i)
  57. else:
  58. base = (1.0 - (float(i) - float(warmup_steps)) /
  59. (float(total_steps) - float(warmup_steps)))
  60. lr = float(lr_max) * base * base
  61. if lr < 0.0:
  62. lr = 0.0
  63. lr_each_step.append(lr)
  64. elif lr_decay_mode == 'cosine':
  65. decay_steps = total_steps - warmup_steps
  66. for i in range(total_steps):
  67. if i < warmup_steps:
  68. lr_inc = (float(lr_max) - float(lr_init)) / float(warmup_steps)
  69. lr = float(lr_init) + lr_inc * (i + 1)
  70. else:
  71. linear_decay = (total_steps - i) / decay_steps
  72. cosine_decay = 0.5 * \
  73. (1 + math.cos(math.pi * 2 * 0.47 * i / decay_steps))
  74. decayed = linear_decay * cosine_decay + 0.00001
  75. lr = lr_max * decayed
  76. lr_each_step.append(lr)
  77. else:
  78. for i in range(total_steps):
  79. if i < warmup_steps:
  80. lr = lr_init + (lr_max - lr_init) * i / warmup_steps
  81. else:
  82. lr = lr_max - (lr_max - lr_end) * \
  83. (i - warmup_steps) / (total_steps - warmup_steps)
  84. lr_each_step.append(lr)
  85. learning_rate = np.array(lr_each_step).astype(np.float32)
  86. return learning_rate