test_gaussian_random_op.py 977 字节
Newer Older
1 2
import unittest
import paddle.v2.framework.core as core
D
dongzhihong 已提交
3
from paddle.v2.framework.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()
Q
qijun 已提交
17
        scope.new_var('Out').get_tensor()
D
dongzhihong 已提交
18

19 20
        op = Operator(
            "gaussian_random",
Q
qijun 已提交
21
            Out='Out',
22 23 24
            dims=[1000, 784],
            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()