Add test without weights
@@ -16,11 +16,13 @@ from chainer.testing import condition
'shape': [(3, 2, 4)],
'axis': [None, 0, 1, 2, -1],
'dtype': [numpy.float16, numpy.float32, numpy.float64],
+ 'use_weights': [True, False],
}) +
testing.product({
'shape': [()],
'axis': [None],
'dtype': [numpy.float16, numpy.float32, numpy.float64],
+ 'use_weights': [True, False],
})))
class TestAverage(unittest.TestCase):
@@ -41,15 +43,17 @@ class TestAverage(unittest.TestCase):
self.gy = numpy.random.uniform(-1, 1, g_shape).astype(self.dtype)
self.w = numpy.random.uniform(-1, 1, w_shape).astype(self.dtype)
- def check_forward(self, x_data, axis=None, weights=None):
+ def check_forward(self, x_data, axis, weights):
x = chainer.Variable(x_data)
- if weights is None:
- w = None
- else:
+ if self.use_weights:
w = chainer.Variable(weights)
+ w_data = self.w
+ else:
+ w = None
+ w_data = None
y = functions.average(x, axis=axis, weights=w)
self.assertEqual(y.data.dtype, self.dtype)
- y_expect = numpy.average(self.x, axis=axis, weights=self.w)
+ y_expect = numpy.average(self.x, axis=axis, weights=w_data)
if self.dtype == numpy.float16:
options = {'atol': 1e-3, 'rtol': 1e-3}
@@ -60,28 +64,38 @@ class TestAverage(unittest.TestCase):
@condition.retry(3)
def test_forward_cpu(self):
- self.check_forward(self.x, axis=self.axis, weights=self.w)
+ self.check_forward(self.x, self.axis, self.w)
@attr.gpu
@condition.retry(3)
def test_forward_gpu(self):
self.check_forward(
- cuda.to_gpu(self.x), axis=self.axis, weights=cuda.to_gpu(self.w))
+ cuda.to_gpu(self.x), self.axis, cuda.to_gpu(self.w))
+
+ def check_backward(self, x_data, y_grad, axis, w_data):
+ if self.use_weights:
+ def f(x, w):
+ return functions.average(x, axis=axis, weights=w)
+ args = (x_data, w_data)
+ else:
+ def f(x):
+ return functions.average(x, axis=axis)
+ args = x_data
- def check_backward(self, x_data, y_grad, axis=None):
gradient_check.check_backward(
- functions.Sum(axis), x_data, y_grad, atol=1e-4,
+ f, args, y_grad, atol=1e-2, rtol=1e-2,
dtype=numpy.float64)
@condition.retry(3)
def test_backward_cpu(self):
- self.check_backward(self.x, self.gy, axis=self.axis)
+ self.check_backward(self.x, self.gy, self.axis, self.w)
@attr.gpu
@condition.retry(3)
def test_backward_gpu(self):
self.check_backward(
- cuda.to_gpu(self.x), cuda.to_gpu(self.gy), axis=self.axis)
+ cuda.to_gpu(self.x), cuda.to_gpu(self.gy), self.axis,
+ cuda.to_gpu(self.w))
testing.run_module(__name__, __file__)
