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.

warmup_step_lr.py 3.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. """
  16. warm up step learning rate.
  17. """
  18. from collections import Counter
  19. import numpy as np
  20. from .linear_warmup import linear_warmup_lr
  21. def lr_steps(global_step, lr_init, lr_max, warmup_epochs, total_epochs, steps_per_epoch):
  22. """Set learning rate."""
  23. lr_each_step = []
  24. total_steps = steps_per_epoch*total_epochs
  25. warmup_steps = steps_per_epoch*warmup_epochs
  26. if warmup_steps != 0:
  27. inc_each_step = (float(lr_max) - float(lr_init)) / float(warmup_steps)
  28. else:
  29. inc_each_step = 0
  30. for i in range(total_steps):
  31. if i < warmup_steps:
  32. lr_value = float(lr_init) + inc_each_step*float(i)
  33. else:
  34. base = (1.0 - (float(i) - float(warmup_steps)) / (float(total_steps) - float(warmup_steps)))
  35. lr_value = float(lr_max)*base*base
  36. if lr_value < 0.0:
  37. lr_value = 0.0
  38. lr_each_step.append(lr_value)
  39. current_step = global_step
  40. lr_each_step = np.array(lr_each_step).astype(np.float32)
  41. learning_rate = lr_each_step[current_step:]
  42. return learning_rate
  43. def warmup_step_lr(lr, lr_epochs, steps_per_epoch, warmup_epochs, max_epoch, gamma=0.1):
  44. """warmup_step_lr"""
  45. base_lr = lr
  46. warmup_init_lr = 0
  47. total_steps = int(max_epoch*steps_per_epoch)
  48. warmup_steps = int(warmup_epochs*steps_per_epoch)
  49. milestones = lr_epochs
  50. milestones_steps = []
  51. for milestone in milestones:
  52. milestones_step = milestone*steps_per_epoch
  53. milestones_steps.append(milestones_step)
  54. lr_each_step = []
  55. lr = base_lr
  56. milestones_steps_counter = Counter(milestones_steps)
  57. for i in range(total_steps):
  58. if i < warmup_steps:
  59. lr = linear_warmup_lr(i + 1, warmup_steps, base_lr, warmup_init_lr)
  60. else:
  61. lr = lr*gamma**milestones_steps_counter[i]
  62. lr_each_step.append(lr)
  63. return np.array(lr_each_step).astype(np.float32)
  64. def multi_step_lr(lr, milestones, steps_per_epoch, max_epoch, gamma=0.1):
  65. return warmup_step_lr(lr, milestones, steps_per_epoch, 0, max_epoch, gamma=gamma)
  66. def step_lr(lr, epoch_size, steps_per_epoch, max_epoch, gamma=0.1):
  67. lr_epochs = []
  68. for i in range(1, max_epoch):
  69. if i % epoch_size == 0:
  70. lr_epochs.append(i)
  71. return multi_step_lr(lr, lr_epochs, steps_per_epoch, max_epoch, gamma=gamma)

MindArmour关注AI的安全和隐私问题。致力于增强模型的安全可信、保护用户的数据隐私。主要包含3个模块:对抗样本鲁棒性模块、Fuzz Testing模块、隐私保护与评估模块。 对抗样本鲁棒性模块 对抗样本鲁棒性模块用于评估模型对于对抗样本的鲁棒性,并提供模型增强方法用于增强模型抗对抗样本攻击的能力,提升模型鲁棒性。对抗样本鲁棒性模块包含了4个子模块:对抗样本的生成、对抗样本的检测、模型防御、攻防评估。