test_gaussian_random_op.py 7.4 KB
Newer Older
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2
#
D
dzhwinter 已提交
3 4 5
# 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
D
dzhwinter 已提交
6
#
D
dzhwinter 已提交
7
#     http://www.apache.org/licenses/LICENSE-2.0
D
dzhwinter 已提交
8
#
D
dzhwinter 已提交
9 10 11 12 13 14
# 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.

15 16
from __future__ import print_function

17
import unittest
18
import numpy as np
D
dzhwinter 已提交
19

20 21 22 23
import paddle.fluid as fluid
import paddle.fluid.core as core
from paddle.fluid.op import Operator
from paddle.fluid.executor import Executor
24
from op_test import OpTest
25 26


27
class TestGaussianRandomOp(OpTest):
D
dzhwinter 已提交
28 29
    def setUp(self):
        self.op_type = "gaussian_random"
30
        self.set_attrs()
D
dzhwinter 已提交
31
        self.inputs = {}
M
mozga-intel 已提交
32 33
        self.use_mkldnn = False
        self.attrs = {
34
            "shape": [123, 92],
35 36
            "mean": self.mean,
            "std": self.std,
M
mozga-intel 已提交
37 38 39
            "seed": 10,
            "use_mkldnn": self.use_mkldnn
        }
D
dzhwinter 已提交
40

41
        self.outputs = {'Out': np.zeros((123, 92), dtype='float32')}
D
dzhwinter 已提交
42

43 44 45 46
    def set_attrs(self):
        self.mean = 1.0
        self.std = 2.

47 48
    def test_check_output(self):
        self.check_output_customized(self.verify_output)
49

50 51 52 53 54 55 56 57 58 59 60 61 62
    def verify_output(self, outs):
        self.assertEqual(outs[0].shape, (123, 92))
        hist, _ = np.histogram(outs[0], range=(-3, 5))
        hist = hist.astype("float32")
        hist /= float(outs[0].size)
        data = np.random.normal(size=(123, 92), loc=1, scale=2)
        hist2, _ = np.histogram(data, range=(-3, 5))
        hist2 = hist2.astype("float32")
        hist2 /= float(outs[0].size)
        self.assertTrue(
            np.allclose(
                hist, hist2, rtol=0, atol=0.01),
            "hist: " + str(hist) + " hist2: " + str(hist2))
63

D
dongzhihong 已提交
64

65 66 67 68 69 70
class TestMeanStdAreInt(TestGaussianRandomOp):
    def set_attrs(self):
        self.mean = 1
        self.std = 2


71 72 73 74 75 76 77 78 79 80 81
# Situation 2: Attr(shape) is a list(with tensor)
class TestGaussianRandomOp_ShapeTensorList(TestGaussianRandomOp):
    def setUp(self):
        '''Test gaussian_random op with specified value
        '''
        self.op_type = "gaussian_random"
        self.init_data()
        shape_tensor_list = []
        for index, ele in enumerate(self.shape):
            shape_tensor_list.append(("x" + str(index), np.ones(
                (1)).astype('int32') * ele))
D
dzhwinter 已提交
82

83 84 85 86 87 88 89
        self.attrs = {
            'shape': self.infer_shape,
            'mean': self.mean,
            'std': self.std,
            'seed': self.seed,
            'use_mkldnn': self.use_mkldnn
        }
D
dzhwinter 已提交
90

91 92
        self.inputs = {"ShapeTensorList": shape_tensor_list}
        self.outputs = {'Out': np.zeros((123, 92), dtype='float32')}
D
dzhwinter 已提交
93

94 95 96 97 98 99 100
    def init_data(self):
        self.shape = [123, 92]
        self.infer_shape = [-1, 92]
        self.use_mkldnn = False
        self.mean = 1.0
        self.std = 2.0
        self.seed = 10
D
dzhwinter 已提交
101

102 103
    def test_check_output(self):
        self.check_output_customized(self.verify_output)
104

M
mozga-intel 已提交
105

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
class TestGaussianRandomOp2_ShapeTensorList(
        TestGaussianRandomOp_ShapeTensorList):
    def init_data(self):
        self.shape = [123, 92]
        self.infer_shape = [-1, -1]
        self.use_mkldnn = False
        self.mean = 1.0
        self.std = 2.0
        self.seed = 10


class TestGaussianRandomOp3_ShapeTensorList(
        TestGaussianRandomOp_ShapeTensorList):
    def init_data(self):
        self.shape = [123, 92]
        self.infer_shape = [123, -1]
        self.use_mkldnn = True
        self.mean = 1.0
        self.std = 2.0
        self.seed = 10


class TestGaussianRandomOp4_ShapeTensorList(
        TestGaussianRandomOp_ShapeTensorList):
    def init_data(self):
        self.shape = [123, 92]
        self.infer_shape = [123, -1]
        self.use_mkldnn = False
        self.mean = 1.0
        self.std = 2.0
        self.seed = 10
137

138 139 140

# Situation 3: shape is a tensor
class TestGaussianRandomOp1_ShapeTensor(TestGaussianRandomOp):
141
    def setUp(self):
142 143
        '''Test gaussian_random op with specified value
        '''
144
        self.op_type = "gaussian_random"
145
        self.init_data()
146
        self.use_mkldnn = False
147 148

        self.inputs = {"ShapeTensor": np.array(self.shape).astype("int32")}
149
        self.attrs = {
150 151 152 153
            'mean': self.mean,
            'std': self.std,
            'seed': self.seed,
            'use_mkldnn': self.use_mkldnn
154
        }
155
        self.outputs = {'Out': np.zeros((123, 92), dtype='float32')}
156

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
    def init_data(self):
        self.shape = [123, 92]
        self.use_mkldnn = False
        self.mean = 1.0
        self.std = 2.0
        self.seed = 10


# Test python API
class TestGaussianRandomAPI(unittest.TestCase):
    def test_api(self):
        positive_2_int32 = fluid.layers.fill_constant([1], "int32", 2000)

        positive_2_int64 = fluid.layers.fill_constant([1], "int64", 500)
        shape_tensor_int32 = fluid.data(
            name="shape_tensor_int32", shape=[2], dtype="int32")

        shape_tensor_int64 = fluid.data(
            name="shape_tensor_int64", shape=[2], dtype="int64")

        out_1 = fluid.layers.gaussian_random(
            shape=[2000, 500], dtype="float32", mean=0.0, std=1.0, seed=10)

        out_2 = fluid.layers.gaussian_random(
            shape=[2000, positive_2_int32],
            dtype="float32",
            mean=0.,
            std=1.0,
            seed=10)

        out_3 = fluid.layers.gaussian_random(
            shape=[2000, positive_2_int64],
            dtype="float32",
            mean=0.,
            std=1.0,
            seed=10)

        out_4 = fluid.layers.gaussian_random(
            shape=shape_tensor_int32,
            dtype="float32",
            mean=0.,
            std=1.0,
            seed=10)

        out_5 = fluid.layers.gaussian_random(
            shape=shape_tensor_int64,
            dtype="float32",
            mean=0.,
            std=1.0,
            seed=10)

        out_6 = fluid.layers.gaussian_random(
            shape=shape_tensor_int64,
            dtype=np.float32,
            mean=0.,
            std=1.0,
            seed=10)

        exe = fluid.Executor(place=fluid.CPUPlace())
        res_1, res_2, res_3, res_4, res_5, res_6 = exe.run(
            fluid.default_main_program(),
            feed={
                "shape_tensor_int32": np.array([2000, 500]).astype("int32"),
                "shape_tensor_int64": np.array([2000, 500]).astype("int64"),
            },
            fetch_list=[out_1, out_2, out_3, out_4, out_5, out_6])

        self.assertAlmostEqual(np.mean(res_1), 0.0, delta=0.1)
        self.assertAlmostEqual(np.std(res_1), 1., delta=0.1)
        self.assertAlmostEqual(np.mean(res_2), 0.0, delta=0.1)
        self.assertAlmostEqual(np.std(res_2), 1., delta=0.1)
        self.assertAlmostEqual(np.mean(res_3), 0.0, delta=0.1)
        self.assertAlmostEqual(np.std(res_3), 1., delta=0.1)
        self.assertAlmostEqual(np.mean(res_4), 0.0, delta=0.1)
        self.assertAlmostEqual(np.std(res_5), 1., delta=0.1)
        self.assertAlmostEqual(np.mean(res_5), 0.0, delta=0.1)
        self.assertAlmostEqual(np.std(res_5), 1., delta=0.1)
        self.assertAlmostEqual(np.mean(res_6), 0.0, delta=0.1)
        self.assertAlmostEqual(np.std(res_6), 1., delta=0.1)
236 237


Q
qijun 已提交
238
if __name__ == "__main__":
239
    unittest.main()