コード例 #1
0
 def test_forward_log_det_jacobian(self):
     input = np.random.random((10, ))
     with self.assertRaises(NotImplementedError):
         exe = paddle.static.Executor()
         sp = paddle.static.Program()
         mp = paddle.static.Program()
         with paddle.static.program_guard(mp, sp):
             t = transform.AbsTransform()
             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}, fetch_list=[output])
コード例 #2
0
 def test_forward(self, input, expected):
     exe = paddle.static.Executor()
     sp = paddle.static.Program()
     mp = paddle.static.Program()
     with paddle.static.program_guard(mp, sp):
         t = transform.AbsTransform()
         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)))
コード例 #3
0
 def test_inverse_log_det_jacobian(self, input, expected):
     exe = paddle.static.Executor()
     sp = paddle.static.Program()
     mp = paddle.static.Program()
     with paddle.static.program_guard(mp, sp):
         t = transform.AbsTransform()
         static_input = paddle.static.data('input', input.shape,
                                           input.dtype)
         actual0, actual1 = t.inverse_log_det_jacobian(static_input)
     exe.run(sp)
     [actual0, actual1] = exe.run(mp,
                                  feed={'input': input},
                                  fetch_list=[actual0, actual1])
     expected0, expected1 = expected
     np.testing.assert_allclose(actual0,
                                expected0,
                                rtol=config.RTOL.get(str(input.dtype)),
                                atol=config.ATOL.get(str(input.dtype)))
     np.testing.assert_allclose(actual1,
                                expected1,
                                rtol=config.RTOL.get(str(input.dtype)),
                                atol=config.ATOL.get(str(input.dtype)))
コード例 #4
0
 def setUp(self):
     self._t = transform.AbsTransform()
コード例 #5
0
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)
コード例 #6
0
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)