-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
test_opt.py
230 lines (181 loc) · 6.96 KB
/
test_opt.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
# =============================================================================
from __future__ import division
import math
import unittest
import numpy as np
import functools
from singa import tensor
from singa import singa_wrap as singa
from singa import opt
from cuda_helper import gpu_dev, cpu_dev
def assertTensorEqual(x,y,decimal=6):
assert x.shape == y.shape
assert x.dtype == y.dtype
assert x.device.id() == y.device.id()
d = x.device
x.to_host()
y.to_host()
np.testing.assert_array_almost_equal(
x.data.GetFloatValue(int(x.size())),
y.data.GetFloatValue(int(y.size())),
decimal)
x.to_device(d)
y.to_device(d)
def on_cpu_gpu(func):
@functools.wraps(func)
def wrapper_decorator(*args, **kwargs):
func(*args, dev=cpu_dev, **kwargs)
if singa.USE_CUDA:
func(*args, dev=gpu_dev, **kwargs)
return wrapper_decorator
class TestDecayScheduler(unittest.TestCase):
def test_exponential_decay_cpu(self):
lr = opt.ExponentialDecay(0.1, 2, 0.5, True)
sgd1 = opt.SGD(lr=lr)
for i in range(5):
np.testing.assert_array_almost_equal(tensor.to_numpy(sgd1.lr_value), [0.1*0.5**(i//2)])
sgd1.step()
def test_exponential_decay_no_staircase_cpu(self):
lr = opt.ExponentialDecay(0.1, 2, 0.5, False)
sgd1 = opt.SGD(lr=lr)
for i in range(5):
np.testing.assert_array_almost_equal(tensor.to_numpy(sgd1.lr_value), [0.1*0.5**(i/2)])
sgd1.step()
@on_cpu_gpu
def test_const_decay_scheduler(self, dev):
c1 = opt.Constant(0.2)
step = tensor.Tensor((1,), device=dev).set_value(0)
lr_val = c1(step)
np.testing.assert_array_almost_equal(tensor.to_numpy(c1(step)) , [0.2])
step+=1
np.testing.assert_array_almost_equal(tensor.to_numpy(c1(step)) , [0.2])
class TestOptimizer(unittest.TestCase):
@on_cpu_gpu
def test_optimizer(self, dev):
o1 = opt.Optimizer(0.1)
# test step
o1.step()
o1.step()
# test get states
s1 = o1.get_states()
self.assertAlmostEqual(s1['step_counter'], 2)
# test set states
s2 = {'step_counter': 5}
o1.set_states(s2)
np.testing.assert_array_almost_equal( tensor.to_numpy(o1.step_counter), [5])
@on_cpu_gpu
def test_sgd_const_lr(self, dev=cpu_dev):
cpu_dev.EnableGraph(False)
sgd1 = opt.SGD(lr=0.1)
w_shape=(2,3)
w = tensor.Tensor(w_shape, device=dev).set_value(0.1)
g = tensor.Tensor(w_shape, device=dev).set_value(0.1)
w_step1 = w-0.1*g
sgd1.apply(w.name, w, g)
assertTensorEqual(w, w_step1)
@on_cpu_gpu
def test_RMSProp_const_lr(self, dev=cpu_dev):
cpu_dev.EnableGraph(False)
opt1 = opt.RMSProp(lr=0.1)
w_shape=(2,3)
w = tensor.Tensor(w_shape, device=dev).set_value(0.1)
g = tensor.Tensor(w_shape, device=dev).set_value(0.1)
# running_average = running_average * rho + param_grad * param_grad * (1 - rho)
# param_value = param_value - lr * param_grad / sqrt(running_average + epsilon)
running_average = 0.1 * tensor.square(g)
tmp = running_average + 1e-8
tmp = tensor.sqrt(tmp)
tmp = g / tmp
w_step1 = w - 0.1 * tmp
opt1.apply(w.name, w, g)
assertTensorEqual(w, w_step1)
@on_cpu_gpu
def test_AdaGrad_const_lr(self, dev=cpu_dev):
cpu_dev.EnableGraph(False)
opt1 = opt.AdaGrad(lr=0.1)
w_shape=(2,3)
w = tensor.Tensor(w_shape, device=dev).set_value(0.1)
g = tensor.Tensor(w_shape, device=dev).set_value(0.1)
# history = history + param_grad * param_grad
# param_value = param_value - lr * param_grad / sqrt(history + epsilon)
history = tensor.square(g)
tmp = history + 1e-8
tmp = tensor.sqrt(tmp)
tmp = g / tmp
w_step1 = w - 0.1 * tmp
opt1.apply(w.name, w, g)
assertTensorEqual(w, w_step1)
@on_cpu_gpu
def test_Adam_const_lr(self, dev=cpu_dev):
cpu_dev.EnableGraph(False)
opt1 = opt.Adam(lr=0.1)
w_shape=(2,3)
w = tensor.Tensor(w_shape, device=dev).set_value(1.0)
g = tensor.Tensor(w_shape, device=dev).set_value(0.1)
# m := beta_1 * m + (1 - beta_1) * grad
# v := beta_2 * v + (1 - beta_2) * grad * grad
# m_norm = m / (1 - beta_1 ^ step)
# v_norm = v / (1 - beta_2 ^ step)
# param := param - (lr * m_norm) / ( sqrt(v_norm) + epsilon) )
m = 0.1 * g
tmp = tensor.square(g)
v = 0.001 * tmp
m_norm = m / 0.1
v_norm = v / 0.001
tmp = tensor.sqrt(v_norm) + 1e-8
tmp = m_norm / tmp
w_step1 = w - 0.1 * tmp
opt1.apply(w.name, w, g)
assertTensorEqual(w, w_step1, decimal=5)
@on_cpu_gpu
def test_sgd_const_lr_momentum(self, dev=cpu_dev):
sgd1 = opt.SGD(lr=0.1,momentum=0.9)
w_shape=(2,3)
w = tensor.Tensor(w_shape, device=dev).set_value(0.1)
g = tensor.Tensor(w_shape, device=dev).set_value(0.01)
w_step1 = w-0.1*g
buf = g
sgd1.apply(w.name, w, g)
sgd1.step()
assertTensorEqual(w,w_step1)
buf = g + buf*0.9
w_step2 = w-0.1*buf
sgd1.apply(w.name, w, g)
assertTensorEqual(w, w_step2)
@on_cpu_gpu
def test_sgd_const_lr_momentum_weight_decay(self, dev=cpu_dev):
sgd1 = opt.SGD(lr=0.1, weight_decay=0.2)
w_shape=(2,3)
w = tensor.Tensor(w_shape, device=dev).set_value(0.1)
g = tensor.Tensor(w_shape, device=dev).set_value(0.01)
w_step1 = w-0.1*(g+0.2*w)
sgd1.apply(w.name, w, g)
assertTensorEqual(w,w_step1)
# @on_cpu_gpu
def test_sgd_const_lr_momentum_nesterov(self, dev=cpu_dev):
sgd1 = opt.SGD(lr=0.1, momentum=0.9, nesterov=True)
w_shape=(2,3)
w = tensor.Tensor(w_shape, device=dev).set_value(0.1)
g = tensor.Tensor(w_shape, device=dev).set_value(0.1)
buf = g
w_step1 = w-0.1*(g+0.9*buf)
sgd1.apply(w.name, w, g)
assertTensorEqual(w,w_step1)
if __name__ == '__main__':
unittest.main()