From 0fa0080d4293892e51c9574445ac4e1a665808e7 Mon Sep 17 00:00:00 2001 From: silingtong123 <35439432+silingtong123@users.noreply.github.com> Date: Sat, 12 Oct 2019 15:23:30 +0800 Subject: [PATCH] add unittest of seed failed work (#20487) cherry-pick (#20485) * add unittest of seed failed work * tadd unittest of seed failed work * fix the parameter seed failed to work --- python/paddle/fluid/layers/nn.py | 2 +- .../tests/unittests/test_uniform_random_op.py | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index 1fdccdb285b..1d91630eb2f 100755 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -17236,7 +17236,7 @@ def uniform_random(shape, dtype='float32', min=-1.0, max=1.0, seed=0): helper = LayerHelper("uniform_random", **locals()) inputs = dict() - attrs = dict() + attrs = {'seed': seed, 'min': min, 'max': max} if in_dygraph_mode(): attrs = {'shape': shape} else: diff --git a/python/paddle/fluid/tests/unittests/test_uniform_random_op.py b/python/paddle/fluid/tests/unittests/test_uniform_random_op.py index ca18a07753b..92ac6be3c2d 100644 --- a/python/paddle/fluid/tests/unittests/test_uniform_random_op.py +++ b/python/paddle/fluid/tests/unittests/test_uniform_random_op.py @@ -243,6 +243,32 @@ class TestUniformRandomOp_attr_tensor_API(unittest.TestCase): outs = exe.run(train_program, fetch_list=[ret]) +class TestUniformRandomOp_API_seed(unittest.TestCase): + def test_attr_tensor_API(self): + startup_program = fluid.Program() + train_program = fluid.Program() + with fluid.program_guard(train_program, startup_program): + _min = 5 + _max = 10 + _seed = 10 + ret = fluid.layers.nn.uniform_random( + [2, 3, 2], min=_min, max=_max, seed=_seed) + ret_2 = fluid.layers.nn.uniform_random( + [2, 3, 2], min=_min, max=_max, seed=_seed) + res = fluid.layers.equal(ret, ret_2) + place = fluid.CPUPlace() + if fluid.core.is_compiled_with_cuda(): + place = fluid.CUDAPlace(0) + exe = fluid.Executor(place) + + exe.run(startup_program) + ret_value, cmp_value = exe.run(train_program, fetch_list=[ret, res]) + self.assertTrue(np.array(cmp_value).all()) + for i in ret_value.flatten(): + self.assertGreaterEqual(i, _min) + self.assertLess(i, _max) + + class TestUniformRandomOpSelectedRowsShapeTensor(unittest.TestCase): def get_places(self): places = [core.CPUPlace()] -- GitLab