def _domain(self): domain = self.transforms[0]._domain # Compute the lower bound of input dimensions for chain transform. # # Suppose the dimensions of input tensor is N, and chain [t0,...ti,...tm], # ti(in) denotes ti.domain.event_rank, ti(out) denotes ti.codomain.event_rank, # delta(ti) denotes (ti(out) - ti(in)). # For transform ti, N shoud satisfy the constraint: # N + delta(t0) + delta(t1)...delta(t(i-1)) >= ti(in) # So, for all transform in chain, N shoud satisfy follow constraints: # t0: N >= t0(in) # t1: N >= t1(in) - delta(t0) # ... # tm: N >= tm(in) - ... - delta(ti) - ... - delta(t0) # # Above problem can be solved more effectively use dynamic programming. # Let N(i) denotes lower bound of transform ti, than the state # transition equation is: # N(i) = max{N(i+1)-delta(ti), ti(in)} event_rank = self.transforms[-1]._codomain.event_rank for t in reversed(self.transforms): event_rank -= t._codomain.event_rank - t._domain.event_rank event_rank = max(event_rank, t._domain.event_rank) return variable.Independent(domain, event_rank - domain.event_rank)
def _codomain(self): codomain = self.transforms[-1]._codomain event_rank = self.transforms[0]._domain.event_rank for t in self.transforms: event_rank += t._codomain.event_rank - t._domain.event_rank event_rank = max(event_rank, t._codomain.event_rank) return variable.Independent(codomain, event_rank - codomain.event_rank)
def setUp(self): self._var = variable.Independent(self.base, self.rank)
def _domain(self): return variable.Independent(variable.real, 1)
def _codomain(self): return variable.Independent(variable.real, len(self._out_event_shape))
def _codomain(self): return variable.Independent(self._base._codomain, self._reinterpreted_batch_rank)
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)
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)