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 4.9 kB

5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 linear_warmup_lr(current_step, warmup_steps, base_lr, init_lr):
  19. """linear_warmup_lr"""
  20. lr_inc = (float(base_lr) - float(init_lr)) / float(warmup_steps)
  21. lr = float(init_lr) + lr_inc * current_step
  22. return lr
  23. def cosine_annealing_lr(lr, steps_per_epoch, warmup_epochs, max_epoch, T_max, eta_min=0, num_periods=0.5):
  24. """linear_warmup_lr"""
  25. base_lr = lr
  26. warmup_init_lr = 0
  27. total_steps = int(max_epoch * steps_per_epoch)
  28. warmup_steps = int(warmup_epochs * steps_per_epoch)
  29. decay_steps = total_steps - warmup_steps
  30. lr_each_step = []
  31. for i in range(total_steps):
  32. if i < warmup_steps:
  33. lr = linear_warmup_lr(i + 1, warmup_steps, base_lr, warmup_init_lr)
  34. else:
  35. # linear_decay = (total_steps - i) / decay_steps
  36. cosine_decay = 0.5 * (1 + math.cos(math.pi * i / decay_steps))
  37. decayed = cosine_decay
  38. lr = base_lr * decayed
  39. lr_each_step.append(lr)
  40. return np.array(lr_each_step).astype(np.float32)
  41. def warmup_cosine_annealing_lr(lr, steps_per_epoch, warmup_epochs, max_epoch, T_max, eta_min=0, num_periods=0.5):
  42. """warmup_cosine_annealing_lr"""
  43. base_lr = lr
  44. warmup_init_lr = 0
  45. total_steps = int(max_epoch * steps_per_epoch * 0.99)
  46. warmup_steps = int(warmup_epochs * steps_per_epoch)
  47. decay_steps = total_steps - warmup_steps
  48. lr_each_step = []
  49. for i in range(total_steps):
  50. if i < warmup_steps:
  51. lr = linear_warmup_lr(i + 1, warmup_steps, base_lr, warmup_init_lr)
  52. else:
  53. linear_decay = (total_steps - i) / decay_steps
  54. cosine_decay = 0.5 * (1 + math.cos(math.pi * 2 * num_periods * i / decay_steps))
  55. decayed = linear_decay * cosine_decay
  56. lr = base_lr * decayed + 0.000005
  57. lr_each_step.append(lr)
  58. return np.array(lr_each_step).astype(np.float32)
  59. def get_lr(global_step, lr_init, lr_end, lr_max, warmup_epochs, total_epochs, steps_per_epoch, lr_decay_mode):
  60. """
  61. generate learning rate array
  62. Args:
  63. global_step(int): total steps of the training
  64. lr_init(float): init learning rate
  65. lr_end(float): end learning rate
  66. lr_max(float): max learning rate
  67. warmup_epochs(int): number of warmup epochs
  68. total_epochs(int): total epoch of training
  69. steps_per_epoch(int): steps of one epoch
  70. lr_decay_mode(string): learning rate decay mode, including steps, poly or default
  71. Returns:
  72. np.array, learning rate array
  73. """
  74. lr_each_step = []
  75. total_steps = steps_per_epoch * total_epochs
  76. warmup_steps = steps_per_epoch * warmup_epochs
  77. if lr_decay_mode == 'steps':
  78. decay_epoch_index = [0.3 * total_steps, 0.6 * total_steps, 0.8 * total_steps]
  79. for i in range(total_steps):
  80. if i < decay_epoch_index[0]:
  81. lr = lr_max
  82. elif i < decay_epoch_index[1]:
  83. lr = lr_max * 0.1
  84. elif i < decay_epoch_index[2]:
  85. lr = lr_max * 0.01
  86. else:
  87. lr = lr_max * 0.001
  88. lr_each_step.append(lr)
  89. elif lr_decay_mode == 'poly':
  90. if warmup_steps != 0:
  91. inc_each_step = (float(lr_max) - float(lr_init)) / float(warmup_steps)
  92. else:
  93. inc_each_step = 0
  94. for i in range(total_steps):
  95. if i < warmup_steps:
  96. lr = float(lr_init) + inc_each_step * float(i)
  97. else:
  98. base = (1.0 - (float(i) - float(warmup_steps)) / (float(total_steps) - float(warmup_steps)))
  99. lr = float(lr_max) * base * base
  100. if lr < 0.0:
  101. lr = 0.0
  102. lr_each_step.append(lr)
  103. else:
  104. for i in range(total_steps):
  105. if i < warmup_steps:
  106. lr = lr_init + (lr_max - lr_init) * i / warmup_steps
  107. else:
  108. lr = lr_max - (lr_max - lr_end) * (i - warmup_steps) / (total_steps - warmup_steps)
  109. lr_each_step.append(lr)
  110. current_step = global_step
  111. lr_each_step = np.array(lr_each_step).astype(np.float32)
  112. learning_rate = lr_each_step[current_step:]
  113. return learning_rate