def test_inverse_log_det_jacobian(self, input): exe = paddle.static.Executor() sp = paddle.static.Program() mp = paddle.static.Program() with paddle.static.program_guard(mp, sp): t = transform.ExpTransform() static_input = paddle.static.data('input', input.shape, input.dtype) output = t.inverse_log_det_jacobian(static_input) exe.run(sp) [output] = exe.run(mp, feed={'input': input}, fetch_list=[output]) np.testing.assert_allclose(output, self._np_inverse_jacobian(input), rtol=config.RTOL.get(str(input.dtype)), atol=config.ATOL.get(str(input.dtype)))
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.ExpTransform()
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) @param.place(config.DEVICES) @param.param_cls( (param.TEST_CASE_NAME, 'base', 'reinterpreted_batch_rank', 'x'), [ ('rank-over-zero', transform.ExpTransform(), 2, np.random.rand( 2, 3, 3)), ]) class TestIndependentTransform(unittest.TestCase): def setUp(self): self._t = transform.IndependentTransform(self.base, self.reinterpreted_batch_rank) @param.param_func([(0, 0, TypeError), (paddle.distribution.Transform(), -1, ValueError)]) def test_init_exception(self, base, rank, exc): with self.assertRaises(exc): paddle.distribution.IndependentTransform(base, rank) def test_is_injective(self): self.assertEqual(self._t._is_injective(), self.base._is_injective())
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)
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) @param.place(config.DEVICES) @param.param_cls( (param.TEST_CASE_NAME, 'base', 'reinterpreted_batch_rank', 'x'), [ ('rank-over-zero', transform.ExpTransform(), 2, np.random.rand( 2, 3, 3)), ]) class TestIndependentTransform(unittest.TestCase): def setUp(self): self._t = transform.IndependentTransform(self.base, self.reinterpreted_batch_rank) def test_is_injective(self): self.assertEqual(self._t._is_injective(), self.base._is_injective()) def test_domain(self): self.assertTrue(isinstance(self._t._domain, variable.Independent)) self.assertEqual( self._t._domain.event_rank, self.base._domain.event_rank + self.reinterpreted_batch_rank)