test_gaussian_random_op.py 1.4 KB
Newer Older
1
import unittest
D
dzhwinter 已提交
2 3 4
import numpy

import paddle.v2.fluid as fluid
Q
Qiao Longfei 已提交
5 6
import paddle.v2.fluid.core as core
from paddle.v2.fluid.op import Operator
D
dzhwinter 已提交
7
from paddle.v2.fluid.executor import Executor
8 9


10
class TestGaussianRandomOp(unittest.TestCase):
D
dzhwinter 已提交
11 12 13 14 15 16 17
    def setUp(self):
        self.op_type = "gaussian_random"
        self.inputs = {}
        self.attrs = {"shape": [1000, 784], "mean": .0, "std": 1., "seed": 10}

        self.outputs = ["Out"]

18
    def test_cpu(self):
D
dzhwinter 已提交
19
        self.gaussian_random_test(place=fluid.CPUPlace())
20 21

    def test_gpu(self):
22
        if core.is_compile_gpu():
D
dzhwinter 已提交
23
            self.gaussian_random_test(place=fluid.GPUPlace(0))
24

25
    def gaussian_random_test(self, place):
D
dongzhihong 已提交
26

D
dzhwinter 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
        program = fluid.Program()
        block = program.global_block()
        vout = block.create_var(name="Out")
        op = block.append_op(
            type=self.op_type, outputs={"Out": vout}, attrs=self.attrs)

        op.desc.infer_var_type(block.desc)
        op.desc.infer_shape(block.desc)

        fetch_list = []
        for var_name in self.outputs:
            fetch_list.append(block.var(var_name))

        exe = Executor(place)
        outs = exe.run(program, fetch_list=fetch_list)
        tensor = outs[0]

44 45
        self.assertAlmostEqual(numpy.mean(tensor), .0, delta=0.1)
        self.assertAlmostEqual(numpy.std(tensor), 1., delta=0.1)
46 47


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