def check_backward(self, f): x = chainer.Variable(f(self.x)) y = gradient_clip(x, self.a, self.b) y.creator.forward((x.data,)) y.grad = f(self.y_grad) y.backward() assert_allclose(y.data, x.data) assert_allclose(x.grad, f(self.x_grad_ab)) y = gradient_clip(x, self.a) y.creator.forward((x.data,)) y.grad = f(self.y_grad) y.backward() assert_allclose(y.data, x.data) assert_allclose(x.grad, f(self.x_grad_a)) @condition.retry(100) def test_backward_cpu(self): self.check_backward(lambda x: x) @attr.gpu @condition.retry(100) def test_backward_gpu(self): self.check_backward(cuda.to_gpu) run_module(__name__, __file__)
class TestReshape(unittest.TestCase): out_shape = (2, 2, 6) def setUp(self): self.x = numpy.random.uniform(-1, 1, (4, 3, 2)).astype(numpy.float32) self.gy = numpy.random.uniform(-1, 1, (2, 2, 6)).astype(numpy.float32) def check_forward(self, x_data): shape = self.out_shape x = chainer.Variable(x_data) y = functions.reshape(x, shape) self.assertEqual(y.data.dtype, numpy.float32) self.assertTrue((self.x.reshape(shape) == cuda.to_cpu(y.data)).all()) def test_forward_cpu(self): self.check_forward(self.x) @attr.gpu def test_forward_gpu(self): self.check_forward(cuda.to_gpu(self.x)) def check_backward(self, x_data, y_grad): gradient_check.check_backward(functions.Reshape(self.gy.shape), x_data, y_grad) class TestReshapeUnknownDimension(TestReshape): out_shape = (2, -1, 6) testing.run_module(__name__, __file__)
y_grad, grads, eps=1e-2, **self.check_double_backward_options) def test_double_backward_cpu(self): self.check_double_backward(self.x, self.W, self.b, self.gy, self.ggx, self.ggW, self.ggb) def test_double_backward_cpu_nobias(self): self.check_double_backward(self.x, self.W, None, self.gy, self.ggx, self.ggW, None) @attr.gpu def test_double_backward_gpu(self): self.check_double_backward(cuda.to_gpu(self.x), cuda.to_gpu(self.W), cuda.to_gpu(self.b), cuda.to_gpu(self.gy), cuda.to_gpu(self.ggx), cuda.to_gpu(self.ggW), cuda.to_gpu(self.ggb)) @attr.gpu def test_double_backward_gpu_nobias(self): self.check_double_backward(cuda.to_gpu(self.x), cuda.to_gpu(self.W), None, cuda.to_gpu(self.gy), cuda.to_gpu(self.ggx), cuda.to_gpu(self.ggW), None) testing.run_module(__name__, __file__)