test_poisson_op.py 6.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#   Copyright (c) 2021 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
import paddle
import numpy as np
from op_test import OpTest
import math
H
hong 已提交
20
from paddle.fluid.framework import _test_eager_guard
21 22

paddle.enable_static()
zhouweiwei2014's avatar
zhouweiwei2014 已提交
23
paddle.seed(100)
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40


def output_hist(out, lam, a, b):
    prob = []
    bin = []
    for i in range(a, b + 1):
        prob.append((lam**i) * math.exp(-lam) / math.factorial(i))
        bin.append(i)
    bin.append(b + 0.1)

    hist, _ = np.histogram(out, bin)
    hist = hist.astype("float32")
    hist = hist / float(out.size)
    return hist, prob


class TestPoissonOp1(OpTest):
41

42 43 44 45 46
    def setUp(self):
        self.op_type = "poisson"
        self.config()

        self.attrs = {}
zhouweiwei2014's avatar
zhouweiwei2014 已提交
47 48
        self.inputs = {'X': np.full([2048, 1024], self.lam, dtype=self.dtype)}
        self.outputs = {'Out': np.ones([2048, 1024], dtype=self.dtype)}
49 50 51 52 53 54 55 56 57

    def config(self):
        self.lam = 10
        self.a = 5
        self.b = 15
        self.dtype = "float64"

    def verify_output(self, outs):
        hist, prob = output_hist(np.array(outs[0]), self.lam, self.a, self.b)
58
        np.testing.assert_allclose(hist, prob, rtol=0.01)
59 60 61 62 63 64 65 66

    def test_check_output(self):
        self.check_output_customized(self.verify_output)

    def test_check_grad_normal(self):
        self.check_grad(
            ['X'],
            'Out',
zhouweiwei2014's avatar
zhouweiwei2014 已提交
67
            user_defined_grads=[np.zeros([2048, 1024], dtype=self.dtype)],
68
            user_defined_grad_outputs=[
zhouweiwei2014's avatar
zhouweiwei2014 已提交
69
                np.random.rand(2048, 1024).astype(self.dtype)
70 71 72 73
            ])


class TestPoissonOp2(TestPoissonOp1):
74

75 76 77
    def config(self):
        self.lam = 5
        self.a = 1
zhouweiwei2014's avatar
zhouweiwei2014 已提交
78
        self.b = 8
79 80 81 82
        self.dtype = "float32"


class TestPoissonAPI(unittest.TestCase):
83

84 85 86 87 88 89 90 91 92 93 94 95 96 97
    def test_static(self):
        with paddle.static.program_guard(paddle.static.Program(),
                                         paddle.static.Program()):
            x_np = np.random.rand(10, 10)
            x = paddle.static.data(name="x", shape=[10, 10], dtype='float64')
            y = paddle.poisson(x)

            exe = paddle.static.Executor()
            y_np = exe.run(paddle.static.default_main_program(),
                           feed={"x": x_np},
                           fetch_list=[y])
            self.assertTrue(np.min(y_np) >= 0)

    def test_dygraph(self):
H
hong 已提交
98 99 100 101 102 103 104 105 106 107 108
        with paddle.fluid.dygraph.base.guard():
            x = paddle.randn([10, 10], dtype='float32')
            y = paddle.poisson(x)
            self.assertTrue(np.min(y.numpy()) >= 0)

            with _test_eager_guard():
                x = paddle.randn([10, 10], dtype='float32')
                x.stop_gradient = False
                y = paddle.poisson(x)
                y.backward()
                self.assertTrue(np.min(y.numpy()) >= 0)
109
                np.testing.assert_array_equal(np.zeros_like(x), x.gradient())
110 111

    def test_fixed_random_number(self):
112
        # Test GPU Fixed random number, which is generated by 'curandStatePhilox4_32_10_t'
113 114 115
        if not paddle.is_compiled_with_cuda():
            return

116
        print("Test Fixed Random number on GPU------>")
117 118 119 120 121 122 123 124 125 126 127
        paddle.disable_static()
        paddle.set_device('gpu')
        paddle.seed(2021)
        x = paddle.full([32, 3, 1024, 768], 10., dtype="float32")
        y = paddle.poisson(x)
        y_np = y.numpy()

        expect = [
            13., 13., 11., 8., 12., 6., 9., 15., 16., 6., 13., 12., 9., 15.,
            17., 8., 11., 16., 11., 10.
        ]
128
        np.testing.assert_array_equal(y_np[0, 0, 0, 0:20], expect)
129 130 131 132 133

        expect = [
            15., 7., 12., 8., 14., 10., 10., 11., 11., 11., 21., 6., 9., 13.,
            13., 11., 6., 9., 12., 12.
        ]
134
        np.testing.assert_array_equal(y_np[8, 1, 300, 200:220], expect)
135 136 137 138 139

        expect = [
            10., 15., 9., 6., 4., 13., 10., 10., 13., 12., 9., 7., 10., 14., 7.,
            10., 8., 5., 10., 14.
        ]
140
        np.testing.assert_array_equal(y_np[16, 1, 600, 400:420], expect)
141 142 143 144 145

        expect = [
            10., 9., 14., 12., 8., 9., 7., 8., 11., 10., 13., 8., 12., 9., 7.,
            8., 11., 11., 12., 5.
        ]
146
        np.testing.assert_array_equal(y_np[24, 2, 900, 600:620], expect)
147 148 149 150 151

        expect = [
            15., 5., 11., 13., 12., 12., 13., 16., 9., 9., 7., 9., 13., 11.,
            15., 6., 11., 9., 10., 10.
        ]
152
        np.testing.assert_array_equal(y_np[31, 2, 1023, 748:768], expect)
153 154 155 156 157 158 159 160

        x = paddle.full([16, 1024, 1024], 5., dtype="float32")
        y = paddle.poisson(x)
        y_np = y.numpy()
        expect = [
            4., 5., 2., 9., 8., 7., 4., 7., 4., 7., 6., 3., 10., 7., 5., 7., 2.,
            5., 5., 6.
        ]
161
        np.testing.assert_array_equal(y_np[0, 0, 100:120], expect)
162 163 164 165 166

        expect = [
            1., 4., 8., 11., 6., 5., 4., 4., 7., 4., 4., 7., 11., 6., 5., 3.,
            4., 6., 3., 3.
        ]
167
        np.testing.assert_array_equal(y_np[4, 300, 300:320], expect)
168 169 170 171 172

        expect = [
            7., 5., 4., 6., 8., 5., 6., 7., 7., 7., 3., 10., 5., 10., 4., 5.,
            8., 7., 5., 7.
        ]
173
        np.testing.assert_array_equal(y_np[8, 600, 600:620], expect)
174 175 176 177 178

        expect = [
            8., 6., 7., 4., 3., 0., 4., 6., 6., 4., 3., 10., 5., 1., 3., 8., 8.,
            2., 1., 4.
        ]
179
        np.testing.assert_array_equal(y_np[12, 900, 900:920], expect)
180 181 182 183 184

        expect = [
            2., 1., 14., 3., 6., 5., 2., 2., 6., 5., 7., 4., 8., 4., 8., 4., 5.,
            7., 1., 7.
        ]
185
        np.testing.assert_array_equal(y_np[15, 1023, 1000:1020], expect)
186 187 188 189 190
        paddle.enable_static()


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