def test_forward_log_det_jacobian(self): input = np.random.random(self.scale.shape) exe = paddle.static.Executor() sp = paddle.static.Program() mp = paddle.static.Program() with paddle.static.program_guard(mp, sp): loc = paddle.static.data('loc', self.loc.shape, self.loc.dtype) scale = paddle.static.data('scale', self.scale.shape, self.scale.dtype) t = transform.AffineTransform(loc, scale) static_input = paddle.static.data('input', input.shape, input.dtype) output = t.forward_log_det_jacobian(static_input) exe.run(sp) [output] = exe.run(mp, feed={ 'input': input, 'loc': self.loc, 'scale': self.scale }, fetch_list=[output]) np.testing.assert_allclose(output, self._np_forward_jacobian(input), rtol=config.RTOL.get(str(self.loc.dtype)), atol=config.ATOL.get(str(self.loc.dtype)))
def setUp(self): sp = paddle.static.Program() mp = paddle.static.Program() with paddle.static.program_guard(mp, sp): loc = paddle.static.data('loc', self.loc.shape, self.loc.dtype) scale = paddle.static.data('scale', self.scale.shape, self.scale.dtype) self._t = transform.AffineTransform(loc, scale)
class TestChainTransform(unittest.TestCase): @param.param_func([(paddle.distribution.Transform, TypeError), ([0], TypeError)]) def test_init_exception(self, transforms, exception): with self.assertRaises(exception): paddle.distribution.ChainTransform(transforms) @param.param_func(((transform.ChainTransform( (transform.AbsTransform(), transform.AffineTransform(paddle.rand([1]), paddle.rand([1])))), False), (transform.ChainTransform(( transform.AffineTransform(paddle.rand([1]), paddle.rand([1])), transform.ExpTransform(), )), True))) def test_is_injective(self, chain, expected): self.assertEqual(chain._is_injective(), expected) @param.param_func(((transform.ChainTransform( (transform.IndependentTransform(transform.ExpTransform(), 1), transform.IndependentTransform(transform.ExpTransform(), 10), transform.IndependentTransform(transform.ExpTransform(), 8))), variable.Independent(variable.real, 10)), )) def test_domain(self, input, expected): self.assertIsInstance(input._domain, type(expected)) self.assertEqual(input._domain.event_rank, expected.event_rank) self.assertEqual(input._domain.is_discrete, expected.is_discrete) @param.param_func(((transform.ChainTransform( (transform.IndependentTransform(transform.ExpTransform(), 9), transform.IndependentTransform(transform.ExpTransform(), 4), transform.IndependentTransform(transform.ExpTransform(), 5))), variable.Independent(variable.real, 9)), )) def test_codomain(self, input, expected): self.assertIsInstance(input._codomain, variable.Independent) self.assertEqual(input._codomain.event_rank, expected.event_rank) self.assertEqual(input._codomain.is_discrete, expected.is_discrete) @param.param_func([ (transform.ChainTransform( (transform.AffineTransform(paddle.to_tensor(0.0), paddle.to_tensor(1.0)), transform.ExpTransform())), np.array([0., 1., 2., 3.]), np.exp(np.array([0., 1., 2., 3.]) * 1.0)), (transform.ChainTransform( (transform.ExpTransform(), transform.TanhTransform())), np.array([[0., -1., 2., -3.], [-5., 6., 7., -8.]]), np.tanh(np.exp(np.array([[0., -1., 2., -3.], [-5., 6., 7., -8.]])))) ]) def test_forward(self, chain, input, expected): np.testing.assert_allclose(chain.forward( paddle.to_tensor(input)).numpy(), expected, rtol=config.RTOL.get(str(input.dtype)), atol=config.ATOL.get(str(input.dtype))) @param.param_func([ (transform.ChainTransform( (transform.AffineTransform(paddle.to_tensor(0.0), paddle.to_tensor(-1.0)), transform.ExpTransform())), np.array([0., 1., 2., 3.]), np.log(np.array([0., 1., 2., 3.])) / (-1.0)), (transform.ChainTransform( (transform.ExpTransform(), transform.TanhTransform())), np.array([[0., 1., 2., 3.], [5., 6., 7., 8.]]), np.log(np.arctanh(np.array([[0., 1., 2., 3.], [5., 6., 7., 8.]])))) ]) def test_inverse(self, chain, input, expected): np.testing.assert_allclose(chain.inverse( paddle.to_tensor(input)).numpy(), expected, rtol=config.RTOL.get(str(input.dtype)), atol=config.ATOL.get(str(input.dtype))) @param.param_func([ (transform.ChainTransform( (transform.AffineTransform(paddle.to_tensor(0.0), paddle.to_tensor(-1.0)), transform.PowerTransform(paddle.to_tensor(2.0)))), np.array([1., 2., 3.]), np.log(2. * np.array([1., 2., 3.]))), ]) def test_forward_log_det_jacobian(self, chain, input, expected): np.testing.assert_allclose(chain.forward_log_det_jacobian( paddle.to_tensor(input)).numpy(), expected, rtol=config.RTOL.get(str(input.dtype)), atol=config.ATOL.get(str(input.dtype))) @param.param_func([ (transform.ChainTransform( (transform.AffineTransform(paddle.to_tensor(0.0), paddle.to_tensor(-1.0)), transform.ExpTransform())), (2, 3, 5), (2, 3, 5)), ]) def test_forward_shape(self, chain, shape, expected_shape): self.assertEqual(chain.forward_shape(shape), expected_shape) @param.param_func([ (transform.ChainTransform( (transform.AffineTransform(paddle.to_tensor(0.0), paddle.to_tensor(-1.0)), transform.ExpTransform())), (2, 3, 5), (2, 3, 5)), ]) def test_inverse_shape(self, chain, shape, expected_shape): self.assertEqual(chain.inverse_shape(shape), expected_shape)
def setUp(self): self._t = transform.AffineTransform(paddle.to_tensor(self.loc), paddle.to_tensor(self.scale))
class TestChainTransform(unittest.TestCase): @param.param_func(((transform.ChainTransform( (transform.AbsTransform(), transform.AffineTransform(paddle.rand([1]), paddle.rand([1])))), False), (transform.ChainTransform(( transform.AffineTransform(paddle.rand([1]), paddle.rand([1])), transform.ExpTransform(), )), True))) def test_is_injective(self, chain, expected): self.assertEqual(chain._is_injective(), expected) @param.param_func(((transform.ChainTransform( (transform.IndependentTransform(transform.ExpTransform(), 1), transform.IndependentTransform(transform.ExpTransform(), 10), transform.IndependentTransform(transform.ExpTransform(), 8))), variable.Independent(variable.real, 10)), )) def test_domain(self, input, expected): self.assertIsInstance(input._domain, type(expected)) self.assertEqual(input._domain.event_rank, expected.event_rank) self.assertEqual(input._domain.is_discrete, expected.is_discrete) @param.param_func(((transform.ChainTransform( (transform.IndependentTransform(transform.ExpTransform(), 9), transform.IndependentTransform(transform.ExpTransform(), 4), transform.IndependentTransform(transform.ExpTransform(), 5))), variable.Independent(variable.real, 9)), )) def test_codomain(self, input, expected): self.assertIsInstance(input._codomain, variable.Independent) self.assertEqual(input._codomain.event_rank, expected.event_rank) self.assertEqual(input._codomain.is_discrete, expected.is_discrete) @param.param_func([ (transform.ChainTransform( (transform.ExpTransform(), transform.TanhTransform())), np.array([[0., -1., 2., -3.], [-5., 6., 7., -8.]]), np.tanh(np.exp(np.array([[0., -1., 2., -3.], [-5., 6., 7., -8.]])))) ]) def test_forward(self, chain, input, expected): exe = paddle.static.Executor() sp = paddle.static.Program() mp = paddle.static.Program() with paddle.static.program_guard(mp, sp): t = chain static_input = paddle.static.data('input', input.shape, input.dtype) output = t.forward(static_input) exe.run(sp) [output] = exe.run(mp, feed={'input': input}, fetch_list=[output]) np.testing.assert_allclose(output, expected, rtol=config.RTOL.get(str(input.dtype)), atol=config.ATOL.get(str(input.dtype))) @param.param_func([ (transform.ChainTransform( (transform.ExpTransform(), transform.TanhTransform())), np.array([[0., 1., 2., 3.], [5., 6., 7., 8.]]), np.log(np.arctanh(np.array([[0., 1., 2., 3.], [5., 6., 7., 8.]])))) ]) def test_inverse(self, chain, input, expected): exe = paddle.static.Executor() sp = paddle.static.Program() mp = paddle.static.Program() with paddle.static.program_guard(mp, sp): t = chain static_input = paddle.static.data('input', input.shape, input.dtype) output = t.inverse(static_input) exe.run(sp) [output] = exe.run(mp, feed={'input': input}, fetch_list=[output]) np.testing.assert_allclose(output, expected, rtol=config.RTOL.get(str(input.dtype)), atol=config.ATOL.get(str(input.dtype))) @param.param_func([ (transform.ChainTransform( (transform.AffineTransform(paddle.full([1], 0.0), paddle.full([1], -1.0)), transform.ExpTransform())), (2, 3, 5), (2, 3, 5)), ]) def test_forward_shape(self, chain, shape, expected_shape): self.assertEqual(chain.forward_shape(shape), expected_shape) @param.param_func([ (transform.ChainTransform( (transform.AffineTransform(paddle.full([1], 0.0), paddle.full([1], -1.0)), transform.ExpTransform())), (2, 3, 5), (2, 3, 5)), ]) def test_inverse_shape(self, chain, shape, expected_shape): self.assertEqual(chain.forward_shape(shape), expected_shape)