def setUp(self): self.func = EmbedIDGaussian(3, 2, CovarianceType.spherical, mu_c=0.1) self.func.zero_grads() self.x = numpy.array([0, 1, 0], dtype=numpy.int32) self.M = self.func.M.copy() self.C = self.func.C.copy() self.gy = numpy.random.uniform(-1, 1, (3, 2)).astype(numpy.float32) self.gz = numpy.random.uniform(-1, 1, (3, 1)).astype(numpy.float32)
class TestEmbedIDGaussian2(unittest.TestCase): ''' spherical covariance ''' def setUp(self): self.func = EmbedIDGaussian(3, 2, CovarianceType.spherical, mu_c=0.1) self.func.zero_grads() self.x = numpy.array([0, 1, 0], dtype=numpy.int32) self.M = self.func.M.copy() self.C = self.func.C.copy() self.gy = numpy.random.uniform(-1, 1, (3, 2)).astype(numpy.float32) self.gz = numpy.random.uniform(-1, 1, (3, 1)).astype(numpy.float32) def to_gpu(self): self.func.M = cuda.to_gpu(self.func.M) self.func.gM = cuda.to_gpu(self.func.gM) self.func.C = cuda.to_gpu(self.func.C) self.func.gC = cuda.to_gpu(self.func.gC) def check_forward(self, x_data): x = chainer.Variable(x_data) y, z = self.func(x) self.assertEqual(y.data.dtype, numpy.float32) self.assertEqual(z.data.dtype, numpy.float32) y_expect = numpy.empty_like(self.gy) z_expect = numpy.empty_like(self.gz) for i in six.moves.range(self.x.size): y_expect[i] = self.M[int(self.x[i])] z_expect[i] = self.C[int(self.x[i])] gradient_check.assert_allclose(y_expect, y.data, atol=0, rtol=0) gradient_check.assert_allclose(z_expect, z.data, atol=0, rtol=0) @condition.retry(1) def test_forward_cpu(self): self.check_forward(self.x) @attr.gpu @condition.retry(3) def test_forward_gpu(self): self.to_gpu() self.check_forward(cuda.to_gpu(self.x)) def check_backward(self, x_data, y_grad, z_grad): x = chainer.Variable(x_data) y, z = self.func(x) y.grad = y_grad z.grad = z_grad y.backward() func = y.creator f = lambda: func.forward((x.data,)) gM, gC = gradient_check.numerical_grad(f, (func.M, func.C), (y_grad, z_grad)) gradient_check.assert_allclose(gM, func.gM) gradient_check.assert_allclose(gC, func.gC) @condition.retry(3) def test_backward_cpu(self): self.check_backward(self.x, self.gy, self.gz) @attr.gpu @condition.retry(3) def test_backward_gpu(self): self.to_gpu() self.check_backward(cuda.to_gpu(self.x), cuda.to_gpu(self.gy), cuda.to_gpu(self.gz))