From b71833ea4a98a1f9cbee6b769c25b72072f20539 Mon Sep 17 00:00:00 2001 From: Zhou Wei <1183042833@qq.com> Date: Mon, 1 Aug 2022 17:36:52 +0800 Subject: [PATCH] [UT]fix test_poisson op random fail (#44763) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复poisson op单测随机挂 原因:由于随机OP的无法直接验证数值正确性,该单测随机采样100万个样本,统计落到直方图各区间的数量,计算出粗略的概率密度函数,与标准概率密度函数对比,这种测试方式会有一定误差。 当采样数量越小,误差越大,因此该PR增大采样样本数量(100万->200万),误差进一步减小在rtol范围内。 --- .../fluid/tests/unittests/test_poisson_op.py | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_poisson_op.py b/python/paddle/fluid/tests/unittests/test_poisson_op.py index 812770fac3a..03259e8d5a1 100644 --- a/python/paddle/fluid/tests/unittests/test_poisson_op.py +++ b/python/paddle/fluid/tests/unittests/test_poisson_op.py @@ -39,13 +39,14 @@ def output_hist(out, lam, a, b): class TestPoissonOp1(OpTest): + def setUp(self): self.op_type = "poisson" self.config() self.attrs = {} - self.inputs = {'X': np.full([1024, 1024], self.lam, dtype=self.dtype)} - self.outputs = {'Out': np.ones([1024, 1024], dtype=self.dtype)} + self.inputs = {'X': np.full([2048, 1024], self.lam, dtype=self.dtype)} + self.outputs = {'Out': np.ones([2048, 1024], dtype=self.dtype)} def config(self): self.lam = 10 @@ -55,10 +56,8 @@ class TestPoissonOp1(OpTest): def verify_output(self, outs): hist, prob = output_hist(np.array(outs[0]), self.lam, self.a, self.b) - self.assertTrue( - np.allclose( - hist, prob, rtol=0.01), - "actual: {}, expected: {}".format(hist, prob)) + self.assertTrue(np.allclose(hist, prob, rtol=0.01), + "actual: {}, expected: {}".format(hist, prob)) def test_check_output(self): self.check_output_customized(self.verify_output) @@ -67,22 +66,23 @@ class TestPoissonOp1(OpTest): self.check_grad( ['X'], 'Out', - user_defined_grads=[np.zeros( - [1024, 1024], dtype=self.dtype)], + user_defined_grads=[np.zeros([2048, 1024], dtype=self.dtype)], user_defined_grad_outputs=[ - np.random.rand(1024, 1024).astype(self.dtype) + np.random.rand(2048, 1024).astype(self.dtype) ]) class TestPoissonOp2(TestPoissonOp1): + def config(self): self.lam = 5 self.a = 1 - self.b = 9 + self.b = 8 self.dtype = "float32" class TestPoissonAPI(unittest.TestCase): + def test_static(self): with paddle.static.program_guard(paddle.static.Program(), paddle.static.Program()): -- GitLab