test_uniform_random_op.py 919 字节
Newer Older
Y
Yu Yang 已提交
1 2 3 4 5 6
import unittest
from paddle.v2.framework.op import Operator
import paddle.v2.framework.core as core
import numpy


7
class TestUniformRandomOp(unittest.TestCase):
Y
Yu Yang 已提交
8 9 10 11 12 13 14 15 16
    def test_uniform_random_cpu(self):
        self.uniform_random_test(place=core.CPUPlace())

    def test_uniform_random_gpu(self):
        if core.is_compile_gpu():
            self.uniform_random_test(place=core.GPUPlace(0))

    def uniform_random_test(self, place):
        scope = core.Scope()
D
dongzhihong 已提交
17
        scope.var('X').get_tensor()
Y
Yu Yang 已提交
18 19 20

        op = Operator(
            "uniform_random",
Q
qijun 已提交
21
            Out='X',
Q
QI JUN 已提交
22
            shape=[1000, 784],
Y
Yu Yang 已提交
23 24 25 26 27 28
            min=-5.0,
            max=10.0,
            seed=10)

        ctx = core.DeviceContext.create(place)
        op.run(scope, ctx)
Q
qijun 已提交
29
        tensor = numpy.array(scope.find_var('X').get_tensor())
Y
Yu Yang 已提交
30 31 32
        self.assertAlmostEqual(tensor.mean(), 2.5, delta=0.1)


Q
qijun 已提交
33
if __name__ == "__main__":
Y
Yu Yang 已提交
34
    unittest.main()