test_uniform_random_op.py 950 字节
Newer Older
Y
Yu Yang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
import unittest
from paddle.v2.framework.op import Operator
import paddle.v2.framework.core as core
import numpy


class UniformRandomTest(unittest.TestCase):
    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()
        scope.new_var("X").get_tensor()

        op = Operator(
            "uniform_random",
            Out="X",
            dims=[1000, 784],
            min=-5.0,
            max=10.0,
            seed=10)

        op.infer_shape(scope)
        ctx = core.DeviceContext.create(place)
        op.run(scope, ctx)
        tensor = numpy.array(scope.find_var("X").get_tensor())
        self.assertAlmostEqual(tensor.mean(), 2.5, delta=0.1)


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