test_gaussian_random_op.py 966 字节
Newer Older
1
import unittest
Q
Qiao Longfei 已提交
2 3
import paddle.v2.fluid.core as core
from paddle.v2.fluid.op import Operator
4 5 6
import numpy


7
class TestGaussianRandomOp(unittest.TestCase):
8
    def test_cpu(self):
9
        self.gaussian_random_test(place=core.CPUPlace())
10 11

    def test_gpu(self):
12 13
        if core.is_compile_gpu():
            self.gaussian_random_test(place=core.GPUPlace(0))
14

15
    def gaussian_random_test(self, place):
16
        scope = core.Scope()
D
dongzhihong 已提交
17
        scope.var('Out').get_tensor()
D
dongzhihong 已提交
18

19 20
        op = Operator(
            "gaussian_random",
Q
qijun 已提交
21
            Out='Out',
22
            shape=[1000, 784],
23 24
            mean=.0,
            std=1.,
D
dongzhihong 已提交
25 26
            seed=10)

27 28
        context = core.DeviceContext.create(place)
        op.run(scope, context)
Q
qijun 已提交
29
        tensor = numpy.array(scope.find_var('Out').get_tensor())
30 31
        self.assertAlmostEqual(numpy.mean(tensor), .0, delta=0.1)
        self.assertAlmostEqual(numpy.std(tensor), 1., delta=0.1)
32 33


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