• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 """
16import pytest
17
18from mindspore.nn import dynamic_lr as dr
19
20milestone = [10, 20, 30]
21learning_rates = [0.1, 0.05, 0.01]
22learning_rate = 0.1
23end_learning_rate = 0.01
24decay_rate = 0.9
25total_step = 30
26step_per_epoch = 3
27decay_epoch = 2
28min_lr = 0.01
29max_lr = 0.1
30power = 0.5
31warmup_epoch = 2
32
33class TestInputs:
34    def test_milestone1(self):
35        milestone1 = 1
36        with pytest.raises(TypeError):
37            dr.piecewise_constant_lr(milestone1, learning_rates)
38
39    def test_milestone2(self):
40        milestone1 = [20, 10, 1]
41        with pytest.raises(ValueError):
42            dr.piecewise_constant_lr(milestone1, learning_rates)
43
44        milestone2 = [1.0, 2.0, True]
45        with pytest.raises(TypeError):
46            dr.piecewise_constant_lr(milestone2, learning_rates)
47
48    def test_learning_rates1(self):
49        lr = True
50        with pytest.raises(TypeError):
51            dr.piecewise_constant_lr(milestone, lr)
52
53    def test_learning_rates2(self):
54        lr = [1, 2, 1]
55        with pytest.raises(TypeError):
56            dr.piecewise_constant_lr(milestone, lr)
57
58    def test_learning_rate_type(self):
59        lr = True
60        with pytest.raises(TypeError):
61            dr.exponential_decay_lr(lr, decay_rate, total_step, step_per_epoch, decay_epoch)
62
63        with pytest.raises(TypeError):
64            dr.polynomial_decay_lr(lr, end_learning_rate, total_step, step_per_epoch, decay_epoch, power)
65
66    def test_learning_rate_value(self):
67        lr = -1.0
68        with pytest.raises(ValueError):
69            dr.exponential_decay_lr(lr, decay_rate, total_step, step_per_epoch, decay_epoch)
70
71        with pytest.raises(ValueError):
72            dr.polynomial_decay_lr(lr, end_learning_rate, total_step, step_per_epoch, decay_epoch, power)
73
74    def test_end_learning_rate_type(self):
75        lr = True
76        with pytest.raises(TypeError):
77            dr.polynomial_decay_lr(learning_rate, lr, total_step, step_per_epoch, decay_epoch, power)
78
79    def test_end_learning_rate_value(self):
80        lr = -1.0
81        with pytest.raises(ValueError):
82            dr.polynomial_decay_lr(learning_rate, lr, total_step, step_per_epoch, decay_epoch, power)
83
84    def test_decay_rate_type(self):
85        rate = 'a'
86        with pytest.raises(TypeError):
87            dr.exponential_decay_lr(learning_rate, rate, total_step, step_per_epoch, decay_epoch)
88
89    def test_decay_rate_value(self):
90        rate = -1.0
91        with pytest.raises(ValueError):
92            dr.exponential_decay_lr(learning_rate, rate, total_step, step_per_epoch, decay_epoch)
93
94    def test_total_step1(self):
95        total_step1 = 2.0
96        with pytest.raises(TypeError):
97            dr.exponential_decay_lr(learning_rate, decay_rate, total_step1, step_per_epoch, decay_epoch)
98
99        with pytest.raises(TypeError):
100            dr.cosine_decay_lr(min_lr, max_lr, total_step1, step_per_epoch, decay_epoch)
101
102        with pytest.raises(TypeError):
103            dr.polynomial_decay_lr(learning_rate, end_learning_rate, total_step1, step_per_epoch, decay_epoch, power)
104
105    def test_total_step2(self):
106        total_step1 = -1
107        with pytest.raises(ValueError):
108            dr.exponential_decay_lr(learning_rate, decay_rate, total_step1, step_per_epoch, decay_epoch)
109
110        with pytest.raises(ValueError):
111            dr.cosine_decay_lr(min_lr, max_lr, total_step1, step_per_epoch, decay_epoch)
112
113        with pytest.raises(ValueError):
114            dr.polynomial_decay_lr(learning_rate, end_learning_rate, total_step1, step_per_epoch, decay_epoch, power)
115
116    def test_step_per_epoch1(self):
117        step_per_epoch1 = True
118        with pytest.raises(TypeError):
119            dr.exponential_decay_lr(learning_rate, decay_rate, total_step, step_per_epoch1, decay_epoch)
120
121        with pytest.raises(TypeError):
122            dr.cosine_decay_lr(min_lr, max_lr, total_step, step_per_epoch1, decay_epoch)
123
124        with pytest.raises(TypeError):
125            dr.polynomial_decay_lr(learning_rate, end_learning_rate, total_step, step_per_epoch1, decay_epoch, power)
126
127    def test_step_per_epoch2(self):
128        step_per_epoch1 = -1
129        with pytest.raises(ValueError):
130            dr.exponential_decay_lr(learning_rate, decay_rate, total_step, step_per_epoch1, decay_epoch)
131
132        with pytest.raises(ValueError):
133            dr.cosine_decay_lr(min_lr, max_lr, total_step, step_per_epoch1, decay_epoch)
134
135        with pytest.raises(ValueError):
136            dr.polynomial_decay_lr(learning_rate, end_learning_rate, total_step, step_per_epoch1, decay_epoch, power)
137
138    def test_decay_epoch1(self):
139        decay_epoch1 = 'm'
140        with pytest.raises(TypeError):
141            dr.exponential_decay_lr(learning_rate, decay_rate, total_step, step_per_epoch, decay_epoch1)
142
143        with pytest.raises(TypeError):
144            dr.cosine_decay_lr(min_lr, max_lr, total_step, step_per_epoch, decay_epoch1)
145
146        with pytest.raises(TypeError):
147            dr.polynomial_decay_lr(learning_rate, end_learning_rate, total_step, step_per_epoch, decay_epoch1, power)
148
149    def test_decay_epoch2(self):
150        decay_epoch1 = -1
151        with pytest.raises(ValueError):
152            dr.exponential_decay_lr(learning_rate, decay_rate, total_step, step_per_epoch, decay_epoch1)
153
154        with pytest.raises(ValueError):
155            dr.cosine_decay_lr(min_lr, max_lr, total_step, step_per_epoch, decay_epoch1)
156
157        with pytest.raises(ValueError):
158            dr.polynomial_decay_lr(learning_rate, end_learning_rate, total_step, step_per_epoch, decay_epoch1, power)
159
160    def test_is_stair(self):
161        is_stair = 1
162        with pytest.raises(TypeError):
163            dr.exponential_decay_lr(learning_rate, decay_rate, total_step, step_per_epoch, decay_epoch, is_stair)
164
165    def test_min_lr_type(self):
166        min_lr1 = True
167        with pytest.raises(TypeError):
168            dr.cosine_decay_lr(min_lr1, max_lr, total_step, step_per_epoch, decay_epoch)
169
170    def test_min_lr_value(self):
171        min_lr1 = -1.0
172        with pytest.raises(ValueError):
173            dr.cosine_decay_lr(min_lr1, max_lr, total_step, step_per_epoch, decay_epoch)
174
175    def test_max_lr_type(self):
176        max_lr1 = 'a'
177        with pytest.raises(TypeError):
178            dr.cosine_decay_lr(min_lr, max_lr1, total_step, step_per_epoch, decay_epoch)
179
180    def test_max_lr_value(self):
181        max_lr1 = -1.0
182        with pytest.raises(ValueError):
183            dr.cosine_decay_lr(min_lr, max_lr1, total_step, step_per_epoch, decay_epoch)
184
185    def test_power(self):
186        power1 = True
187        with pytest.raises(TypeError):
188            dr.polynomial_decay_lr(learning_rate, end_learning_rate, total_step, step_per_epoch, decay_epoch, power1)
189
190    def test_update_decay_epoch(self):
191        update_decay_epoch = 1
192        with pytest.raises(TypeError):
193            dr.polynomial_decay_lr(learning_rate, end_learning_rate, total_step, step_per_epoch, decay_epoch,
194                                   power, update_decay_epoch)
195
196
197def test_learning_rate():
198    lr = dr.piecewise_constant_lr(milestone, learning_rates)
199    assert len(lr) == milestone[-1]
200
201
202def test_exponential_decay():
203    lr1 = dr.exponential_decay_lr(learning_rate, decay_rate, total_step, step_per_epoch, decay_epoch)
204    assert len(lr1) == total_step
205
206    lr2 = dr.exponential_decay_lr(learning_rate, decay_rate, total_step, step_per_epoch, decay_epoch, True)
207    assert len(lr2) == total_step
208
209
210def test_enatural_exp_decay():
211    lr1 = dr.natural_exp_decay_lr(learning_rate, decay_rate, total_step, step_per_epoch, decay_epoch)
212    assert len(lr1) == total_step
213
214    lr2 = dr.natural_exp_decay_lr(learning_rate, decay_rate, total_step, step_per_epoch, decay_epoch, True)
215    assert len(lr2) == total_step
216
217
218def test_inverse_decay():
219    lr1 = dr.inverse_decay_lr(learning_rate, decay_rate, total_step, step_per_epoch, decay_epoch)
220    assert len(lr1) == total_step
221
222    lr2 = dr.inverse_decay_lr(learning_rate, decay_rate, total_step, step_per_epoch, decay_epoch, True)
223    assert len(lr2) == total_step
224
225
226def test_cosine_decay():
227    lr = dr.cosine_decay_lr(min_lr, max_lr, total_step, step_per_epoch, decay_epoch)
228    assert len(lr) == total_step
229
230
231def test_polynomial_decay():
232    lr1 = dr.polynomial_decay_lr(learning_rate, end_learning_rate, total_step, step_per_epoch, decay_epoch, power)
233    assert len(lr1) == total_step
234    lr2 = dr.polynomial_decay_lr(learning_rate, end_learning_rate, total_step, step_per_epoch, decay_epoch, power,
235                                 True)
236    assert len(lr2) == total_step
237
238
239def test_warmup():
240    lr1 = dr.warmup_lr(learning_rate, total_step, step_per_epoch, warmup_epoch)
241    assert len(lr1) == total_step
242