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 65d400c63262bfc6f835836d49d992c45bc3ab41..9eb2fe6cbd1a15e5381174e99966bb6af47a63e8 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__':