From 358d6bc90f0c4b463d66bc7ae25116b0f814dfb0 Mon Sep 17 00:00:00 2001 From: Chen Weihang Date: Wed, 18 Nov 2020 12:46:35 +0800 Subject: [PATCH] Fix test_weight_decay_extend random failed on windows (#28643) * add debuging code * change seed & add debug message --- .../contrib/tests/test_weight_decay_extend.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/python/paddle/fluid/contrib/tests/test_weight_decay_extend.py b/python/paddle/fluid/contrib/tests/test_weight_decay_extend.py index 65d400c6326..9eb2fe6cbd1 100644 --- a/python/paddle/fluid/contrib/tests/test_weight_decay_extend.py +++ b/python/paddle/fluid/contrib/tests/test_weight_decay_extend.py @@ -24,6 +24,8 @@ import contextlib paddle.enable_static() +SEED = 2020 + def fake_imdb_reader(word_dict_size, sample_num, @@ -87,6 +89,11 @@ def bow_net(data, class TestWeightDecay(unittest.TestCase): def setUp(self): + # set seed + np.random.seed(SEED) + paddle.seed(SEED) + paddle.framework.random._manual_program_seed(SEED) + # configs self.word_dict_len = 5147 batch_size = 2 reader = fake_imdb_reader(self.word_dict_len, batch_size * 100) @@ -114,8 +121,6 @@ class TestWeightDecay(unittest.TestCase): return param_sum def check_weight_decay(self, place, model): - paddle.seed(1) - paddle.framework.random._manual_program_seed(1) main_prog = fluid.framework.Program() startup_prog = fluid.framework.Program() @@ -137,8 +142,6 @@ class TestWeightDecay(unittest.TestCase): return param_sum def check_weight_decay2(self, place, model): - paddle.seed(1) - paddle.framework.random._manual_program_seed(1) main_prog = fluid.framework.Program() startup_prog = fluid.framework.Program() @@ -173,7 +176,12 @@ class TestWeightDecay(unittest.TestCase): param_sum2 = self.check_weight_decay2(place, model) for i in range(len(param_sum1)): - assert np.isclose(a=param_sum1[i], b=param_sum2[i], rtol=5e-5) + self.assertTrue( + np.allclose(param_sum1[i], param_sum2[i]), + "Current place: {}, i: {}, sum1: {}, sum2: {}".format( + place, i, param_sum1[i][~np.isclose(param_sum1[ + i], param_sum2[i])], param_sum2[i][~np.isclose( + param_sum1[i], param_sum2[i])])) if __name__ == '__main__': -- GitLab