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.

utils.py 2.6 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. """training utils"""
  16. import math
  17. import numpy as np
  18. from mindspore import dtype as mstype
  19. from mindspore import Tensor
  20. def get_lr(cfg, dataset_size):
  21. if cfg.cell == "lstm":
  22. lr = get_lr_lstm(0, cfg.lstm_lr_init, cfg.lstm_lr_end, cfg.lstm_lr_max, cfg.lstm_lr_warm_up_epochs,
  23. cfg.lstm_num_epochs, dataset_size, cfg.lstm_lr_adjust_epochs)
  24. lr_ret = Tensor(lr, mstype.float32)
  25. else:
  26. lr_ret = cfg.lr
  27. return lr_ret
  28. def get_lr_lstm(global_step, lr_init, lr_end, lr_max, warmup_epochs, total_epochs, steps_per_epoch, lr_adjust_epoch):
  29. """
  30. generate learning rate array
  31. Args:
  32. global_step(int): total steps of the training
  33. lr_init(float): init learning rate
  34. lr_end(float): end learning rate
  35. lr_max(float): max learning rate
  36. warmup_epochs(float): number of warmup epochs
  37. total_epochs(int): total epoch of training
  38. steps_per_epoch(int): steps of one epoch
  39. lr_adjust_epoch(int): lr adjust in lr_adjust_epoch, after that, the lr is lr_end
  40. Returns:
  41. np.array, learning rate array
  42. """
  43. lr_each_step = []
  44. total_steps = steps_per_epoch * total_epochs
  45. warmup_steps = steps_per_epoch * warmup_epochs
  46. adjust_steps = lr_adjust_epoch * steps_per_epoch
  47. for i in range(total_steps):
  48. if i < warmup_steps:
  49. lr = lr_init + (lr_max - lr_init) * i / warmup_steps
  50. elif i < adjust_steps:
  51. lr = lr_end + \
  52. (lr_max - lr_end) * \
  53. (1. + math.cos(math.pi * (i - warmup_steps) / (adjust_steps - warmup_steps))) / 2.
  54. else:
  55. lr = lr_end
  56. if lr < 0.0:
  57. lr = 0.0
  58. lr_each_step.append(lr)
  59. current_step = global_step
  60. lr_each_step = np.array(lr_each_step).astype(np.float32)
  61. learning_rate = lr_each_step[current_step:]
  62. return learning_rate