Skip to content

Commit

Permalink
[OpAttr]Fix dropout2d/3d static API (PaddlePaddle#46434)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aurelius84 authored Sep 23, 2022
1 parent cbf3f4b commit 55f73ba
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
35 changes: 26 additions & 9 deletions python/paddle/fluid/tests/unittests/test_dropout_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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])
Expand All @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion python/paddle/nn/functional/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1116,7 +1116,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(
Expand Down

0 comments on commit 55f73ba

Please sign in to comment.