test_distribution_gumbel_static.py 6.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest

17
import config
18
import numpy as np
19
import parameterize
20 21 22 23 24 25 26 27 28
import scipy.stats

import paddle
from paddle.distribution.gumbel import Gumbel

paddle.enable_static()


@parameterize.place(config.DEVICES)
29 30 31 32 33 34 35
@parameterize.parameterize_cls(
    (parameterize.TEST_CASE_NAME, 'loc', 'scale'),
    [
        ('one-dim', parameterize.xrand((4,)), parameterize.xrand((4,))),
        ('multi-dim', parameterize.xrand((5, 3)), parameterize.xrand((5, 3))),
    ],
)
36 37 38 39 40 41 42
class TestGumbel(unittest.TestCase):
    def setUp(self):
        startup_program = paddle.static.Program()
        main_program = paddle.static.Program()
        executor = paddle.static.Executor(self.place)
        with paddle.static.program_guard(main_program, startup_program):
            loc = paddle.static.data('loc', self.loc.shape, self.loc.dtype)
43 44 45
            scale = paddle.static.data(
                'scale', self.scale.shape, self.scale.dtype
            )
46 47 48 49 50 51 52 53 54 55 56
            self._dist = Gumbel(loc=loc, scale=scale)
            self.sample_shape = [50000]
            mean = self._dist.mean
            var = self._dist.variance
            stddev = self._dist.stddev
            entropy = self._dist.entropy()
            samples = self._dist.sample(self.sample_shape)
        fetch_list = [mean, var, stddev, entropy, samples]
        self.feeds = {'loc': self.loc, 'scale': self.scale}

        executor.run(startup_program)
57 58 59 60 61 62 63
        [
            self.mean,
            self.var,
            self.stddev,
            self.entropy,
            self.samples,
        ] = executor.run(main_program, feed=self.feeds, fetch_list=fetch_list)
64 65 66

    def test_mean(self):
        self.assertEqual(str(self.mean.dtype).split('.')[-1], self.scale.dtype)
67 68 69 70 71 72
        np.testing.assert_allclose(
            self.mean,
            self._np_mean(),
            rtol=config.RTOL.get(str(self.scale.dtype)),
            atol=config.ATOL.get(str(self.scale.dtype)),
        )
73 74 75

    def test_variance(self):
        self.assertEqual(str(self.var.dtype).split('.')[-1], self.scale.dtype)
76 77 78 79 80 81
        np.testing.assert_allclose(
            self.var,
            self._np_variance(),
            rtol=config.RTOL.get(str(self.scale.dtype)),
            atol=config.ATOL.get(str(self.scale.dtype)),
        )
82 83 84

    def test_stddev(self):
        self.assertEqual(
85 86 87 88 89 90 91 92
            str(self.stddev.dtype).split('.')[-1], self.scale.dtype
        )
        np.testing.assert_allclose(
            self.stddev,
            self._np_stddev(),
            rtol=config.RTOL.get(str(self.scale.dtype)),
            atol=config.ATOL.get(str(self.scale.dtype)),
        )
93 94 95

    def test_entropy(self):
        self.assertEqual(
96 97
            str(self.entropy.dtype).split('.')[-1], self.scale.dtype
        )
98 99 100 101

    def test_sample(self):
        self.assertEqual(self.samples.dtype, self.scale.dtype)

102 103 104 105 106 107 108 109 110 111 112 113
        np.testing.assert_allclose(
            self.samples.mean(axis=0),
            scipy.stats.gumbel_r.mean(self.loc, scale=self.scale),
            rtol=0.1,
            atol=config.ATOL.get(str(self.scale.dtype)),
        )
        np.testing.assert_allclose(
            self.samples.var(axis=0),
            scipy.stats.gumbel_r.var(self.loc, scale=self.scale),
            rtol=0.1,
            atol=config.ATOL.get(str(self.scale.dtype)),
        )
114 115 116 117 118 119 120 121 122

    def _np_mean(self):
        return self.loc + self.scale * np.euler_gamma

    def _np_stddev(self):
        return np.sqrt(self._np_variance())

    def _np_variance(self):
        return np.divide(
123 124
            np.multiply(np.power(self.scale, 2), np.power(np.pi, 2)), 6
        )
125 126 127 128 129 130 131

    def _np_entropy(self):
        return np.log(self.scale) + 1 + np.euler_gamma


@parameterize.place(config.DEVICES)
@parameterize.parameterize_cls(
132 133 134 135 136 137 138 139
    (parameterize.TEST_CASE_NAME, 'loc', 'scale', 'value'),
    [
        (
            'value-float',
            np.array([0.1, 0.4]),
            np.array([1.0, 4.0]),
            np.array([3.0, 7.0]),
        ),
140
        ('value-int', np.array([0.1, 0.4]), np.array([1, 4]), np.array([3, 7])),
141 142 143 144 145 146 147 148
        (
            'value-multi-dim',
            np.array([0.1, 0.4]),
            np.array([1, 4]),
            np.array([[5.0, 4], [6, 2]]),
        ),
    ],
)
149 150 151 152 153 154 155 156
class TestGumbelPDF(unittest.TestCase):
    def setUp(self):
        startup_program = paddle.static.Program()
        main_program = paddle.static.Program()
        executor = paddle.static.Executor(self.place)

        with paddle.static.program_guard(main_program, startup_program):
            loc = paddle.static.data('loc', self.loc.shape, self.loc.dtype)
157 158 159 160 161 162
            scale = paddle.static.data(
                'scale', self.scale.shape, self.scale.dtype
            )
            value = paddle.static.data(
                'value', self.value.shape, self.value.dtype
            )
163 164 165 166 167 168 169 170
            self._dist = Gumbel(loc=loc, scale=scale)
            prob = self._dist.prob(value)
            log_prob = self._dist.log_prob(value)
            cdf = self._dist.cdf(value)
        fetch_list = [prob, log_prob, cdf]
        self.feeds = {'loc': self.loc, 'scale': self.scale, 'value': self.value}

        executor.run(startup_program)
171 172 173
        [self.prob, self.log_prob, self.cdf] = executor.run(
            main_program, feed=self.feeds, fetch_list=fetch_list
        )
174 175

    def test_prob(self):
176 177 178 179 180 181
        np.testing.assert_allclose(
            self.prob,
            scipy.stats.gumbel_r.pdf(self.value, self.loc, self.scale),
            rtol=config.RTOL.get(str(self.loc.dtype)),
            atol=config.ATOL.get(str(self.loc.dtype)),
        )
182 183

    def test_log_prob(self):
184 185 186 187 188 189
        np.testing.assert_allclose(
            self.log_prob,
            scipy.stats.gumbel_r.logpdf(self.value, self.loc, self.scale),
            rtol=config.RTOL.get(str(self.loc.dtype)),
            atol=config.ATOL.get(str(self.loc.dtype)),
        )
190 191

    def test_cdf(self):
192 193 194 195 196 197
        np.testing.assert_allclose(
            self.cdf,
            scipy.stats.gumbel_r.cdf(self.value, self.loc, self.scale),
            rtol=0.3,
            atol=config.ATOL.get(str(self.loc.dtype)),
        )
198 199 200 201


if __name__ == '__main__':
    unittest.main()