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.

generator_lr.py 1.5 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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 numpy as np
  17. def get_lr(current_step, lr_max, total_epochs, steps_per_epoch):
  18. """
  19. generate learning rate array
  20. Args:
  21. current_step(int): current steps of the training
  22. lr_max(float): max learning rate
  23. total_epochs(int): total epoch of training
  24. steps_per_epoch(int): steps of one epoch
  25. Returns:
  26. np.array, learning rate array
  27. """
  28. lr_each_step = []
  29. total_steps = steps_per_epoch * total_epochs
  30. decay_epoch_index = [0.8 * total_steps]
  31. for i in range(total_steps):
  32. if i < decay_epoch_index[0]:
  33. lr = lr_max
  34. else:
  35. lr = lr_max * 0.1
  36. lr_each_step.append(lr)
  37. lr_each_step = np.array(lr_each_step).astype(np.float32)
  38. learning_rate = lr_each_step[current_step:]
  39. return learning_rate