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.

test_dynamic_lr.py 9.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. """ Test Dynamic Learning Rate """
  16. import pytest
  17. from mindspore.nn import dynamic_lr as dr
  18. milestone = [10, 20, 30]
  19. learning_rates = [0.1, 0.05, 0.01]
  20. learning_rate = 0.1
  21. end_learning_rate = 0.01
  22. decay_rate = 0.9
  23. total_step = 30
  24. step_per_epoch = 3
  25. decay_epoch = 2
  26. min_lr = 0.01
  27. max_lr = 0.1
  28. power = 0.5
  29. warmup_epoch = 2
  30. class TestInputs:
  31. def test_milestone1(self):
  32. milestone1 = 1
  33. with pytest.raises(TypeError):
  34. dr.piecewise_constant_lr(milestone1, learning_rates)
  35. def test_milestone2(self):
  36. milestone1 = [20, 10, 1]
  37. with pytest.raises(ValueError):
  38. dr.piecewise_constant_lr(milestone1, learning_rates)
  39. milestone2 = [1.0, 2.0, True]
  40. with pytest.raises(TypeError):
  41. dr.piecewise_constant_lr(milestone2, learning_rates)
  42. def test_learning_rates1(self):
  43. lr = True
  44. with pytest.raises(TypeError):
  45. dr.piecewise_constant_lr(milestone, lr)
  46. def test_learning_rates2(self):
  47. lr = [1, 2, 1]
  48. with pytest.raises(TypeError):
  49. dr.piecewise_constant_lr(milestone, lr)
  50. def test_learning_rate_type(self):
  51. lr = True
  52. with pytest.raises(TypeError):
  53. dr.exponential_decay_lr(lr, decay_rate, total_step, step_per_epoch, decay_epoch)
  54. with pytest.raises(TypeError):
  55. dr.polynomial_decay_lr(lr, end_learning_rate, total_step, step_per_epoch, decay_epoch, power)
  56. def test_learning_rate_value(self):
  57. lr = -1.0
  58. with pytest.raises(ValueError):
  59. dr.exponential_decay_lr(lr, decay_rate, total_step, step_per_epoch, decay_epoch)
  60. with pytest.raises(ValueError):
  61. dr.polynomial_decay_lr(lr, end_learning_rate, total_step, step_per_epoch, decay_epoch, power)
  62. def test_end_learning_rate_type(self):
  63. lr = True
  64. with pytest.raises(TypeError):
  65. dr.polynomial_decay_lr(learning_rate, lr, total_step, step_per_epoch, decay_epoch, power)
  66. def test_end_learning_rate_value(self):
  67. lr = -1.0
  68. with pytest.raises(ValueError):
  69. dr.polynomial_decay_lr(learning_rate, lr, total_step, step_per_epoch, decay_epoch, power)
  70. def test_decay_rate_type(self):
  71. rate = 'a'
  72. with pytest.raises(TypeError):
  73. dr.exponential_decay_lr(learning_rate, rate, total_step, step_per_epoch, decay_epoch)
  74. def test_decay_rate_value(self):
  75. rate = -1.0
  76. with pytest.raises(ValueError):
  77. dr.exponential_decay_lr(learning_rate, rate, total_step, step_per_epoch, decay_epoch)
  78. def test_total_step1(self):
  79. total_step1 = 2.0
  80. with pytest.raises(TypeError):
  81. dr.exponential_decay_lr(learning_rate, decay_rate, total_step1, step_per_epoch, decay_epoch)
  82. with pytest.raises(TypeError):
  83. dr.cosine_decay_lr(min_lr, max_lr, total_step1, step_per_epoch, decay_epoch)
  84. with pytest.raises(TypeError):
  85. dr.polynomial_decay_lr(learning_rate, end_learning_rate, total_step1, step_per_epoch, decay_epoch, power)
  86. def test_total_step2(self):
  87. total_step1 = -1
  88. with pytest.raises(ValueError):
  89. dr.exponential_decay_lr(learning_rate, decay_rate, total_step1, step_per_epoch, decay_epoch)
  90. with pytest.raises(ValueError):
  91. dr.cosine_decay_lr(min_lr, max_lr, total_step1, step_per_epoch, decay_epoch)
  92. with pytest.raises(ValueError):
  93. dr.polynomial_decay_lr(learning_rate, end_learning_rate, total_step1, step_per_epoch, decay_epoch, power)
  94. def test_step_per_epoch1(self):
  95. step_per_epoch1 = True
  96. with pytest.raises(TypeError):
  97. dr.exponential_decay_lr(learning_rate, decay_rate, total_step, step_per_epoch1, decay_epoch)
  98. with pytest.raises(TypeError):
  99. dr.cosine_decay_lr(min_lr, max_lr, total_step, step_per_epoch1, decay_epoch)
  100. with pytest.raises(TypeError):
  101. dr.polynomial_decay_lr(learning_rate, end_learning_rate, total_step, step_per_epoch1, decay_epoch, power)
  102. def test_step_per_epoch2(self):
  103. step_per_epoch1 = -1
  104. with pytest.raises(ValueError):
  105. dr.exponential_decay_lr(learning_rate, decay_rate, total_step, step_per_epoch1, decay_epoch)
  106. with pytest.raises(ValueError):
  107. dr.cosine_decay_lr(min_lr, max_lr, total_step, step_per_epoch1, decay_epoch)
  108. with pytest.raises(ValueError):
  109. dr.polynomial_decay_lr(learning_rate, end_learning_rate, total_step, step_per_epoch1, decay_epoch, power)
  110. def test_decay_epoch1(self):
  111. decay_epoch1 = 'm'
  112. with pytest.raises(TypeError):
  113. dr.exponential_decay_lr(learning_rate, decay_rate, total_step, step_per_epoch, decay_epoch1)
  114. with pytest.raises(TypeError):
  115. dr.cosine_decay_lr(min_lr, max_lr, total_step, step_per_epoch, decay_epoch1)
  116. with pytest.raises(TypeError):
  117. dr.polynomial_decay_lr(learning_rate, end_learning_rate, total_step, step_per_epoch, decay_epoch1, power)
  118. def test_decay_epoch2(self):
  119. decay_epoch1 = -1
  120. with pytest.raises(ValueError):
  121. dr.exponential_decay_lr(learning_rate, decay_rate, total_step, step_per_epoch, decay_epoch1)
  122. with pytest.raises(ValueError):
  123. dr.cosine_decay_lr(min_lr, max_lr, total_step, step_per_epoch, decay_epoch1)
  124. with pytest.raises(ValueError):
  125. dr.polynomial_decay_lr(learning_rate, end_learning_rate, total_step, step_per_epoch, decay_epoch1, power)
  126. def test_is_stair(self):
  127. is_stair = 1
  128. with pytest.raises(TypeError):
  129. dr.exponential_decay_lr(learning_rate, decay_rate, total_step, step_per_epoch, decay_epoch, is_stair)
  130. def test_min_lr_type(self):
  131. min_lr1 = True
  132. with pytest.raises(TypeError):
  133. dr.cosine_decay_lr(min_lr1, max_lr, total_step, step_per_epoch, decay_epoch)
  134. def test_min_lr_value(self):
  135. min_lr1 = -1.0
  136. with pytest.raises(ValueError):
  137. dr.cosine_decay_lr(min_lr1, max_lr, total_step, step_per_epoch, decay_epoch)
  138. def test_max_lr_type(self):
  139. max_lr1 = 'a'
  140. with pytest.raises(TypeError):
  141. dr.cosine_decay_lr(min_lr, max_lr1, total_step, step_per_epoch, decay_epoch)
  142. def test_max_lr_value(self):
  143. max_lr1 = -1.0
  144. with pytest.raises(ValueError):
  145. dr.cosine_decay_lr(min_lr, max_lr1, total_step, step_per_epoch, decay_epoch)
  146. def test_power(self):
  147. power1 = True
  148. with pytest.raises(TypeError):
  149. dr.polynomial_decay_lr(learning_rate, end_learning_rate, total_step, step_per_epoch, decay_epoch, power1)
  150. def test_update_decay_epoch(self):
  151. update_decay_epoch = 1
  152. with pytest.raises(TypeError):
  153. dr.polynomial_decay_lr(learning_rate, end_learning_rate, total_step, step_per_epoch, decay_epoch,
  154. power, update_decay_epoch)
  155. def test_learning_rate():
  156. lr = dr.piecewise_constant_lr(milestone, learning_rates)
  157. assert len(lr) == milestone[-1]
  158. def test_exponential_decay():
  159. lr1 = dr.exponential_decay_lr(learning_rate, decay_rate, total_step, step_per_epoch, decay_epoch)
  160. assert len(lr1) == total_step
  161. lr2 = dr.exponential_decay_lr(learning_rate, decay_rate, total_step, step_per_epoch, decay_epoch, True)
  162. assert len(lr2) == total_step
  163. def test_enatural_exp_decay():
  164. lr1 = dr.natural_exp_decay_lr(learning_rate, decay_rate, total_step, step_per_epoch, decay_epoch)
  165. assert len(lr1) == total_step
  166. lr2 = dr.natural_exp_decay_lr(learning_rate, decay_rate, total_step, step_per_epoch, decay_epoch, True)
  167. assert len(lr2) == total_step
  168. def test_inverse_decay():
  169. lr1 = dr.inverse_decay_lr(learning_rate, decay_rate, total_step, step_per_epoch, decay_epoch)
  170. assert len(lr1) == total_step
  171. lr2 = dr.inverse_decay_lr(learning_rate, decay_rate, total_step, step_per_epoch, decay_epoch, True)
  172. assert len(lr2) == total_step
  173. def test_cosine_decay():
  174. lr = dr.cosine_decay_lr(min_lr, max_lr, total_step, step_per_epoch, decay_epoch)
  175. assert len(lr) == total_step
  176. def test_polynomial_decay():
  177. lr1 = dr.polynomial_decay_lr(learning_rate, end_learning_rate, total_step, step_per_epoch, decay_epoch, power)
  178. assert len(lr1) == total_step
  179. lr2 = dr.polynomial_decay_lr(learning_rate, end_learning_rate, total_step, step_per_epoch, decay_epoch, power,
  180. True)
  181. assert len(lr2) == total_step
  182. def test_warmup():
  183. lr1 = dr.warmup_lr(learning_rate, total_step, step_per_epoch, warmup_epoch)
  184. assert len(lr1) == total_step