-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
test_exponential.py
202 lines (150 loc) · 5.48 KB
/
test_exponential.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
import unittest
import numpy
import chainer
from chainer import cuda
import chainer.functions as F
from chainer import gradient_check
from chainer import testing
from chainer.testing import attr
from chainer.testing import condition
class UnaryFunctionsTestBase(unittest.TestCase):
def make_data(self):
raise NotImplementedError
def setUp(self):
self.x, self.gy = self.make_data()
def check_forward(self, op, op_np, x_data):
x = chainer.Variable(x_data)
y = op(x)
self.assertEqual(x.data.dtype, y.data.dtype)
testing.assert_allclose(
op_np(self.x), y.data, atol=1e-7, rtol=1e-7)
def check_forward_cpu(self, op, op_np):
self.check_forward(op, op_np, self.x)
def check_forward_gpu(self, op, op_np):
self.check_forward(op, op_np, cuda.to_gpu(self.x))
def check_backward(self, op, x_data, y_grad):
gradient_check.check_backward(
op, x_data, y_grad, atol=1e-4, rtol=1e-3, dtype=numpy.float64)
def check_backward_cpu(self, op):
self.check_backward(op, self.x, self.gy)
def check_backward_gpu(self, op):
self.check_backward(op, cuda.to_gpu(self.x), cuda.to_gpu(self.gy))
def check_double_backward(self, op, x_data, y_grad, y_grad_grad):
gradient_check.check_double_backward(
op, x_data, y_grad, y_grad_grad, atol=1e-4, rtol=1e-3, dtype='d')
def check_double_backward_cpu(self, op):
self.check_double_backward(op, self.x, self.gy, self.ggy)
def check_double_backward_gpu(self, op):
self.check_double_backward(op, cuda.to_gpu(
self.x), cuda.to_gpu(self.gy), cuda.to_gpu(self.ggy))
def check_label(self, op, expected):
self.assertEqual(op().label, expected)
@testing.parameterize(*testing.product({
'shape': [(3, 2), ()],
'dtype': [numpy.float16, numpy.float32, numpy.float64],
}))
class TestExp(UnaryFunctionsTestBase):
def make_data(self):
x = numpy.random.uniform(-1, 1, self.shape).astype(self.dtype)
gy = numpy.random.uniform(-1, 1, self.shape).astype(self.dtype)
self.ggy = numpy.random.uniform(-1, 1, self.shape).astype(self.dtype)
return x, gy
@condition.retry(3)
def test_forward_cpu(self):
self.check_forward_cpu(F.exp, numpy.exp)
@attr.gpu
@condition.retry(3)
def test_forward_gpu(self):
self.check_forward_gpu(F.exp, numpy.exp)
@condition.retry(3)
def test_backward_cpu(self):
self.check_backward_cpu(F.exp)
@attr.gpu
@condition.retry(3)
def test_backward_gpu(self):
self.check_backward_gpu(F.exp)
def test_label(self):
self.check_label(F.Exp, 'exp')
@condition.retry(3)
def test_double_backward_cpu(self):
self.check_double_backward_cpu(F.exp)
@attr.gpu
@condition.retry(3)
def test_double_backward_gpu(self):
self.check_double_backward_gpu(F.exp)
@testing.parameterize(*testing.product({
'shape': [(3, 2), ()],
'dtype': [numpy.float16, numpy.float32, numpy.float64],
}))
class TestLog(UnaryFunctionsTestBase):
def make_data(self):
x = numpy.random.uniform(.5, 1, self.shape).astype(self.dtype)
gy = numpy.random.uniform(-1, 1, self.shape).astype(self.dtype)
return x, gy
@condition.retry(3)
def test_forward_cpu(self):
self.check_forward_cpu(F.log, numpy.log)
@attr.gpu
@condition.retry(3)
def test_forward_gpu(self):
self.check_forward_gpu(F.log, numpy.log)
@condition.retry(3)
def test_backward_cpu(self):
self.check_backward_cpu(F.log)
@attr.gpu
@condition.retry(3)
def test_backward_gpu(self):
self.check_backward_gpu(F.log)
def test_label(self):
self.check_label(F.Log, 'log')
@testing.parameterize(*testing.product({
'shape': [(3, 2), ()],
'dtype': [numpy.float16, numpy.float32, numpy.float64],
}))
class TestLog2(UnaryFunctionsTestBase):
def make_data(self):
x = numpy.random.uniform(.5, 1, self.shape).astype(self.dtype)
gy = numpy.random.uniform(-1, 1, self.shape).astype(self.dtype)
return x, gy
@condition.retry(3)
def test_forward_cpu(self):
self.check_forward_cpu(F.log2, numpy.log2)
@attr.gpu
@condition.retry(3)
def test_forward_gpu(self):
self.check_forward_gpu(F.log2, numpy.log2)
@condition.retry(3)
def test_backward_cpu(self):
self.check_backward_cpu(F.log2)
@attr.gpu
@condition.retry(3)
def test_backward_gpu(self):
self.check_backward_gpu(F.log2)
def test_label(self):
self.check_label(F.Log2, 'log2')
@testing.parameterize(*testing.product({
'shape': [(3, 2), ()],
'dtype': [numpy.float16, numpy.float32, numpy.float64],
}))
class TestLog10(UnaryFunctionsTestBase):
def make_data(self):
x = numpy.random.uniform(.5, 1, self.shape).astype(self.dtype)
gy = numpy.random.uniform(-1, 1, self.shape).astype(self.dtype)
return x, gy
@condition.retry(3)
def test_forward_cpu(self):
self.check_forward_cpu(F.log10, numpy.log10)
@attr.gpu
@condition.retry(3)
def test_forward_gpu(self):
self.check_forward_gpu(F.log10, numpy.log10)
@condition.retry(3)
def test_backward_cpu(self):
self.check_backward_cpu(F.log10)
@attr.gpu
@condition.retry(3)
def test_backward_gpu(self):
self.check_backward_gpu(F.log10)
def test_label(self):
self.check_label(F.Log10, 'log10')
testing.run_module(__name__, __file__)