def setUp(self): self.mp = paddle.static.Program() self.sp = paddle.static.Program() self.executor = paddle.static.Executor(self.place) with paddle.static.program_guard(self.mp, self.sp): rate1 = paddle.static.data('rate1', shape=self.rate1.shape, dtype=self.rate1.dtype) rate2 = paddle.static.data('rate2', shape=self.rate2.shape, dtype=self.rate2.dtype) self.p = mock.Exponential(rate1) self.q = mock.Exponential(rate2) self.feeds = {'rate1': self.rate1, 'rate2': self.rate2}
def setUp(self): self.program = paddle.static.Program() self.executor = paddle.static.Executor() with paddle.static.program_guard(self.program): rate_np = parameterize.xrand((100, 200, 99)) rate = paddle.static.data('rate', rate_np.shape, rate_np.dtype) self.mock_dist = mock.Exponential(rate) self.feeds = {'rate': rate_np}
import unittest import numpy as np import paddle import scipy.stats import config import mock_data as mock @config.place(config.DEVICES) @config.parameterize( (config.TEST_CASE_NAME, 'dist'), [('test-mock-exp', mock.Exponential(rate=paddle.rand( [100, 200, 99], dtype=config.DEFAULT_DTYPE)))]) class TestExponentialFamily(unittest.TestCase): def test_entropy(self): np.testing.assert_allclose( self.dist.entropy(), paddle.distribution.ExponentialFamily.entropy(self.dist), rtol=config.RTOL.get(config.DEFAULT_DTYPE), atol=config.ATOL.get(config.DEFAULT_DTYPE)) @config.place(config.DEVICES) @config.parameterize( (config.TEST_CASE_NAME, 'dist'), [('test-dummy', mock.DummyExpFamily(0.5, 0.5)), ('test-dirichlet',
class DummyDistribution(paddle.distribution.Distribution): pass @config.place(config.DEVICES) @config.parameterize( (config.TEST_CASE_NAME, 'p', 'q'), [('test-unregister', DummyDistribution(), DummyDistribution)]) class TestDispatch(unittest.TestCase): def test_dispatch_with_unregister(self): with self.assertRaises(NotImplementedError): paddle.distribution.kl_divergence(self.p, self.q) @config.place(config.DEVICES) @config.parameterize( (config.TEST_CASE_NAME, 'p', 'q'), [('test-diff-dist', mock.Exponential(paddle.rand((100, 200, 100)) + 1.0), mock.Exponential(paddle.rand((100, 200, 100)) + 2.0)), ('test-same-dist', mock.Exponential( paddle.to_tensor(1.0)), mock.Exponential(paddle.to_tensor(1.0)))]) class TestKLExpfamilyExpFamily(unittest.TestCase): def test_kl_expfamily_expfamily(self): np.testing.assert_allclose(paddle.distribution.kl_divergence( self.p, self.q), kl._kl_expfamily_expfamily(self.p, self.q), rtol=config.RTOL.get(config.DEFAULT_DTYPE), atol=config.ATOL.get(config.DEFAULT_DTYPE))