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.2 kB

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