test_random_op.py 999 字节
Newer Older
D
dongzhihong 已提交
1 2 3 4 5 6 7 8 9 10 11
import unittest
import paddle.v2.framework.create_op_creation_methods as creation
import paddle.v2.framework.core as core
from op_test_util import OpTestMeta
import numpy


class TestRandomOp(unittest.TestCase):
    def test_random(self):
        scope = core.Scope(None)
        # Out = scope.create_var("Out")
D
dongzhihong 已提交
12
        op = creation.op_creations.gaussian_random(
D
dongzhihong 已提交
13
            shape=[1000, 1000], mean=5.0, std=1.0, Out="Out")
D
dongzhihong 已提交
14 15 16 17
        for out in op.outputs():
            if scope.get_var(out) is None:
                scope.create_var(out).get_tensor()

D
dongzhihong 已提交
18
        tensor = scope.get_var("Out").get_tensor()
D
dongzhihong 已提交
19 20 21 22
        op.infer_shape(scope)
        self.assertEqual([1000, 1000], tensor.shape())
        ctx = core.DeviceContext.cpu_context()
        op.run(scope, ctx)
D
dongzhihong 已提交
23
        tensor_array = numpy.array(tensor)
D
dongzhihong 已提交
24 25
        self.assertAlmostEqual(numpy.mean(tensor_array), 5.0, places=3)
        self.assertAlmostEqual(numpy.std(tensor_array), 1.0, places=3)
D
dongzhihong 已提交
26 27 28 29


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