diff --git a/python/paddle/fluid/tests/unittests/test_dropout_op.py b/python/paddle/fluid/tests/unittests/test_dropout_op.py index eb696420fe03b..e3ef0fd61c5ac 100644 --- a/python/paddle/fluid/tests/unittests/test_dropout_op.py +++ b/python/paddle/fluid/tests/unittests/test_dropout_op.py @@ -1112,16 +1112,18 @@ def test_backward_upscale_train_2_eager(self): class TestDropOutWithProbTensor(unittest.TestCase): def setUp(self): - shapes = [[10, 10], [10, 10, 10], [10, 10, 10, 10]] - self.inputs = [ - np.random.random(shape).astype("float32") for shape in shapes - ] + self.init_info() + self.input = np.random.random(self.shape).astype("float32") self.place = paddle.CUDAPlace( 0) if paddle.is_compiled_with_cuda() else paddle.CPUPlace() + def init_info(self): + self.shape = [10, 10] + self.api = paddle.nn.functional.dropout + def api_case(self, x): p = paddle.assign([0.5]) - out = paddle.nn.functional.dropout(x=x, p=p, training=True) + out = self.api(x=x, p=p, training=True) return out def run_static(self, x): @@ -1131,6 +1133,8 @@ def run_static(self, x): with program_guard(main_program): input = paddle.static.data(shape=x.shape, name='x', dtype='float32') out = self.api_case(input) + sgd = paddle.optimizer.SGD(learning_rate=0.1) + sgd.minimize(paddle.mean(out)) exe = paddle.static.Executor(self.place) res = exe.run(feed={'x': x}, fetch_list=[out]) @@ -1144,10 +1148,23 @@ def run_dygraph(self, x): return out def test_p_tensor(self): - for x in self.inputs: - static_res = self.run_static(x) - dygraph_res = self.run_dygraph(x) - np.testing.assert_array_equal(static_res, dygraph_res) + static_res = self.run_static(self.input) + dygraph_res = self.run_dygraph(self.input) + np.testing.assert_array_equal(static_res, dygraph_res) + + +class TestDropOut2DWithProbTensor(TestDropOutWithProbTensor): + + def init_info(self): + self.shape = [2, 3, 10, 10] + self.api = paddle.nn.functional.dropout2d + + +class TestDropOut3DWithProbTensor(TestDropOutWithProbTensor): + + def init_info(self): + self.shape = [2, 3, 8, 8, 8] + self.api = paddle.nn.functional.dropout3d class TestRandomValue(unittest.TestCase): diff --git a/python/paddle/nn/functional/common.py b/python/paddle/nn/functional/common.py index f9f05d94756ff..007f2ee185314 100644 --- a/python/paddle/nn/functional/common.py +++ b/python/paddle/nn/functional/common.py @@ -1119,7 +1119,7 @@ def get_attrs(prog, dropout_prob, is_test, seed): dtype = x.dtype keep_prob = 1 - p if training: - if p == 1.: + if in_dynamic_mode() and p == 1.: return paddle.scale(x, scale=0.) scale_input = paddle.scale(