-
Notifications
You must be signed in to change notification settings - Fork 26
/
clr_test.py
62 lines (60 loc) · 2.68 KB
/
clr_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""Functional test for learning rate decay."""
import math
import clr as learning_rate_decay
from tensorflow.python.framework import test_util
class CyclicLearningRateTest(test_util.TensorFlowTestCase):
"""Functional test for Cyclic Learning Rate"""
def np_cyclic_learning_rate(self, step, lr, max_lr, step_size, mode):
cycle = math.floor(1. + step / (2. * step_size))
x = math.fabs(step / step_size - 2. * cycle + 1.)
clr = (max_lr - lr) * max(0., 1. - x)
if mode == 'triangular2':
clr = clr/(math.pow(2, (cycle - 1)))
if mode == 'exp_range':
clr = clr * math.pow(.99994, step)
return clr + lr
@test_util.run_in_graph_and_eager_modes()
def test_triangular(self):
step = 5
lr = 0.01
max_lr = 0.1
step_size = 20.
cyclic_lr = learning_rate_decay.cyclic_learning_rate(step,
lr, max_lr,
step_size,
mode='triangular')
expected = self.np_cyclic_learning_rate(step,
lr, max_lr,
step_size,
mode='triangular')
self.assertAllClose(self.evaluate(cyclic_lr), expected, 1e-6)
@test_util.run_in_graph_and_eager_modes()
def test_triangular2(self):
step = 5
lr = 0.01
max_lr = 0.1
step_size = 20.
cyclic_lr = learning_rate_decay.cyclic_learning_rate(step,
lr, max_lr,
step_size,
mode='triangular2')
expected = self.np_cyclic_learning_rate(step,
lr, max_lr,
step_size,
mode='triangular2')
self.assertAllClose(self.evaluate(cyclic_lr), expected, 1e-6)
@test_util.run_in_graph_and_eager_modes()
def test_exp_range(self):
step = 5
lr = 0.01
max_lr = 0.1
step_size = 20.
cyclic_lr = learning_rate_decay.cyclic_learning_rate(step,
lr, max_lr,
step_size,
mode='exp_range')
expected = self.np_cyclic_learning_rate(step,
lr, max_lr,
step_size,
mode='exp_range')
self.assertAllClose(self.evaluate(cyclic_lr), expected, 1e-6)