Skip to content

Commit 3ccc7f5

Browse files
authored
remove dropout from fluid (#48319)
* remove dropout
1 parent 75ffc0f commit 3ccc7f5

37 files changed

+211
-311
lines changed

python/paddle/fluid/contrib/layers/rnn_impl.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -378,9 +378,9 @@ def get_single_direction_output(
378378

379379
step_input = new_hidden
380380
if dropout_prob is not None and dropout_prob > 0.0:
381-
step_input = layers.dropout(
381+
step_input = paddle.nn.functional.dropout(
382382
step_input,
383-
dropout_prob=dropout_prob,
383+
p=dropout_prob,
384384
)
385385

386386
rnn.step_output(step_input)
@@ -680,10 +680,10 @@ def get_single_direction_output(
680680

681681
step_input = new_hidden
682682
if dropout_prob is not None and dropout_prob > 0.0:
683-
step_input = layers.dropout(
683+
step_input = paddle.nn.functional.dropout(
684684
step_input,
685-
dropout_prob=dropout_prob,
686-
dropout_implementation='upscale_in_train',
685+
p=dropout_prob,
686+
mode='upscale_in_train',
687687
)
688688

689689
rnn.step_output(step_input)

python/paddle/fluid/contrib/tests/test_image_classification_fp16.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@ def conv_block(input, num_filter, groups, dropouts):
9393
conv4 = conv_block(conv3, 512, 3, [0.4, 0.4, 0])
9494
conv5 = conv_block(conv4, 512, 3, [0.4, 0.4, 0])
9595

96-
drop = fluid.layers.dropout(x=conv5, dropout_prob=0.5)
96+
drop = paddle.nn.functional.dropout(x=conv5, p=0.5)
9797
fc1 = fluid.layers.fc(input=drop, size=4096, act=None)
9898
bn = paddle.static.nn.batch_norm(input=fc1, act='relu')
99-
drop2 = fluid.layers.dropout(x=bn, dropout_prob=0.5)
99+
drop2 = paddle.nn.functional.dropout(x=bn, p=0.5)
100100
fc2 = fluid.layers.fc(input=drop2, size=4096, act=None)
101101
return fc2
102102

python/paddle/fluid/layers/nn.py

-134
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@
6666
'fc',
6767
'embedding',
6868
'conv2d',
69-
'dropout',
7069
'split',
7170
'l2_normalize',
7271
'row_conv',
@@ -750,139 +749,6 @@ def _pull_box_sparse(
750749
return outs
751750

752751

753-
@deprecated(since="2.0.0", update_to="paddle.nn.functional.dropout")
754-
def dropout(
755-
x,
756-
dropout_prob,
757-
is_test=None,
758-
seed=None,
759-
name=None,
760-
dropout_implementation="downgrade_in_infer",
761-
):
762-
"""
763-
764-
Computes dropout.
765-
766-
Drop or keep each element of `x` independently. Dropout is a regularization
767-
technique for reducing overfitting by preventing neuron co-adaption during
768-
training. The dropout operator randomly sets (according to the given dropout
769-
probability) the outputs of some units to zero, while others are remain
770-
unchanged.
771-
772-
dropout op can be removed from the program to make the program more efficient.
773-
774-
Args:
775-
x (Variable): The input tensor variable. The data type is float16 or float32 or float64.
776-
dropout_prob (float): Probability of setting units to zero.
777-
is_test (bool): A flag indicating whether it is in test phrase or not.
778-
Default None, in dynamic graph, it use global tracer mode; in static graph, it means False.
779-
seed (int): A Python integer used to create random seeds. If this
780-
parameter is set to None, a random seed is used.
781-
NOTE: If an integer seed is given, always the same output
782-
units will be dropped. DO NOT use a fixed seed in training.Default: None.
783-
name (str|None): A name for this layer(optional). If set None, the layer
784-
will be named automatically.
785-
dropout_implementation(string): ['downgrade_in_infer'(default)|'upscale_in_train']
786-
787-
1. downgrade_in_infer(default), downgrade the outcome at inference
788-
789-
- train: out = input * mask
790-
- inference: out = input * (1.0 - dropout_prob)
791-
792-
(mask is a tensor same shape with input, value is 0 or 1
793-
ratio of 0 is dropout_prob)
794-
2. upscale_in_train, upscale the outcome at training time
795-
796-
- train: out = input * mask / ( 1.0 - dropout_prob )
797-
- inference: out = input
798-
799-
(mask is a tensor same shape with input, value is 0 or 1
800-
ratio of 0 is dropout_prob)
801-
802-
803-
Returns:
804-
A Variable holding Tensor representing the dropout, has same shape and data type with `x`.
805-
806-
Examples:
807-
808-
.. code-block:: python
809-
810-
import paddle
811-
import paddle.fluid as fluid
812-
813-
paddle.enable_static()
814-
x = fluid.data(name="data", shape=[None, 32, 32], dtype="float32")
815-
dropped = fluid.layers.dropout(x, dropout_prob=0.5)
816-
"""
817-
if not isinstance(dropout_prob, (float, int, Variable)):
818-
raise TypeError(
819-
"dropout_prob argument should be a number(int|float) or Variable"
820-
)
821-
# fast return for p == 0
822-
if isinstance(dropout_prob, (int, float)) and dropout_prob == 0:
823-
return x
824-
825-
if _non_static_mode():
826-
if (
827-
seed is None or seed == 0
828-
) and default_main_program().random_seed != 0:
829-
seed = default_main_program().random_seed
830-
if is_test is None:
831-
is_test = not _dygraph_tracer()._train_mode
832-
out, mask = _legacy_C_ops.dropout(
833-
x,
834-
'dropout_prob',
835-
dropout_prob,
836-
'is_test',
837-
is_test,
838-
'fix_seed',
839-
seed is not None,
840-
'seed',
841-
seed if seed is not None else 0,
842-
'dropout_implementation',
843-
dropout_implementation,
844-
)
845-
return out
846-
847-
def get_attrs(prog, dropout_prob, is_test, seed):
848-
if (seed is None or seed == 0) and prog.random_seed != 0:
849-
seed = prog.random_seed
850-
if isinstance(dropout_prob, Variable) and not dropout_prob.shape != [1]:
851-
raise TypeError(
852-
"Required dropout_prob.shape == [1] if type(dropout_prob) is Variable, but received dropout_prob.shape = {}".format(
853-
dropout_prob.shape
854-
)
855-
)
856-
attrs = {
857-
'dropout_prob': dropout_prob,
858-
'is_test': is_test,
859-
'fix_seed': seed is not None,
860-
'seed': seed if seed is not None else 0,
861-
'dropout_implementation': dropout_implementation,
862-
}
863-
return attrs
864-
865-
helper = LayerHelper('dropout', **locals())
866-
check_variable_and_dtype(
867-
x, 'x', ['float16', 'float32', 'float64'], 'dropout'
868-
)
869-
870-
out = helper.create_variable_for_type_inference(dtype=x.dtype)
871-
mask = helper.create_variable_for_type_inference(
872-
dtype=core.VarDesc.VarType.UINT8, stop_gradient=True
873-
)
874-
875-
attrs = get_attrs(helper.main_program, dropout_prob, is_test, seed)
876-
877-
helper.append_op(
878-
type='dropout',
879-
inputs={'X': [x]},
880-
outputs={'Out': [out], 'Mask': [mask]},
881-
attrs=attrs,
882-
)
883-
return out
884-
885-
886752
def conv2d(
887753
input,
888754
num_filters,

python/paddle/fluid/nets.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ def __extend_list__(obj):
260260
tmp = paddle.static.nn.batch_norm(input=tmp, act=conv_act)
261261
drop_rate = conv_batchnorm_drop_rate[i]
262262
if abs(drop_rate) > 1e-5:
263-
tmp = layers.dropout(x=tmp, dropout_prob=drop_rate)
263+
tmp = paddle.nn.functional.dropout(x=tmp, p=drop_rate)
264264

265265
if pool_type == 'max':
266266
pool_out = paddle.nn.functional.max_pool2d(
@@ -637,8 +637,6 @@ def __combine_heads(x):
637637
weights = paddle.reshape(x=x, shape=product.shape)
638638

639639
if dropout_rate:
640-
weights = layers.dropout(
641-
weights, dropout_prob=dropout_rate, is_test=False
642-
)
640+
weights = paddle.nn.functional.dropout(weights, p=dropout_rate)
643641
ctx_multiheads = paddle.matmul(weights, v)
644642
return __combine_heads(ctx_multiheads)

python/paddle/fluid/tests/book/test_image_classification.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,10 @@ def conv_block(input, num_filter, groups, dropouts):
9292
conv4 = conv_block(conv3, 512, 3, [0.4, 0.4, 0])
9393
conv5 = conv_block(conv4, 512, 3, [0.4, 0.4, 0])
9494

95-
drop = fluid.layers.dropout(x=conv5, dropout_prob=0.5)
95+
drop = paddle.nn.functional.dropout(x=conv5, p=0.5)
9696
fc1 = fluid.layers.fc(input=drop, size=4096, act=None)
9797
bn = paddle.static.nn.batch_norm(input=fc1, act='relu')
98-
drop2 = fluid.layers.dropout(x=bn, dropout_prob=0.5)
98+
drop2 = paddle.nn.functional.dropout(x=bn, p=0.5)
9999
fc2 = fluid.layers.fc(input=drop2, size=4096, act=None)
100100
return fc2
101101

python/paddle/fluid/tests/unittests/collective/fleet/parallel_dygraph_transformer.py

+8-16
Original file line numberDiff line numberDiff line change
@@ -257,11 +257,9 @@ def forward(self, prev_out, out, process_cmd, dropout_rate=0.0):
257257
out = self._layer_norm(out)
258258
elif cmd == "d": # add dropout
259259
if dropout_rate:
260-
out = fluid.layers.dropout(
260+
out = paddle.nn.functional.dropout(
261261
out,
262-
dropout_prob=dropout_rate,
263-
seed=ModelHyperParams.dropout_seed,
264-
is_test=False,
262+
p=dropout_rate,
265263
)
266264
return out
267265

@@ -276,11 +274,9 @@ def __init__(self, d_inner_hid, d_hid, dropout_rate):
276274
def forward(self, x):
277275
hidden = self._i2h(x)
278276
if self._dropout_rate:
279-
hidden = fluid.layers.dropout(
277+
hidden = paddle.nn.functional.dropout(
280278
hidden,
281-
dropout_prob=self._dropout_rate,
282-
seed=ModelHyperParams.dropout_seed,
283-
is_test=False,
279+
p=self._dropout_rate,
284280
)
285281
out = self._h2o(hidden)
286282
return out
@@ -352,11 +348,9 @@ def forward(self, queries, keys, values, attn_bias):
352348
product += attn_bias
353349
weights = paddle.nn.functional.softmax(product)
354350
if self._dropout_rate:
355-
weights_droped = fluid.layers.dropout(
351+
weights_droped = paddle.nn.functional.dropout(
356352
weights,
357-
dropout_prob=self._dropout_rate,
358-
seed=ModelHyperParams.dropout_seed,
359-
is_test=False,
353+
p=self._dropout_rate,
360354
)
361355
out = paddle.matmul(weights_droped, transpose_v)
362356
else:
@@ -548,11 +542,9 @@ def forward(self, src_word, src_pos):
548542
src_pos_emb.stop_gradient = True
549543
enc_input = src_word_emb + src_pos_emb
550544
return (
551-
fluid.layers.dropout(
545+
paddle.nn.functional.dropout(
552546
enc_input,
553-
dropout_prob=self._dropout_rate,
554-
seed=ModelHyperParams.dropout_seed,
555-
is_test=False,
547+
p=self._dropout_rate,
556548
)
557549
if self._dropout_rate
558550
else enc_input

python/paddle/fluid/tests/unittests/dist_se_resnext.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ def net(self, input, class_dim=1000):
113113
)
114114

115115
pool = paddle.nn.functional.adaptive_avg_pool2d(x=conv, output_size=1)
116-
drop = fluid.layers.dropout(x=pool, dropout_prob=0.2)
116+
drop = paddle.nn.functional.dropout(x=pool, p=0.2)
117+
117118
stdv = 1.0 / math.sqrt(drop.shape[1] * 1.0)
118119
out = fluid.layers.fc(
119120
input=drop,

python/paddle/fluid/tests/unittests/dist_transformer.py

+6-12
Original file line numberDiff line numberDiff line change
@@ -1179,11 +1179,9 @@ def scaled_dot_product_attention(q, k, v, attn_bias, d_model, dropout_rate):
11791179
product += attn_bias
11801180
weights = paddle.nn.functional.softmax(product)
11811181
if dropout_rate:
1182-
weights = layers.dropout(
1182+
weights = paddle.nn.functional.dropout(
11831183
weights,
1184-
dropout_prob=dropout_rate,
1185-
seed=ModelHyperParams.dropout_seed,
1186-
is_test=False,
1184+
p=dropout_rate,
11871185
)
11881186
out = paddle.matmul(weights, v)
11891187
return out
@@ -1258,11 +1256,9 @@ def pre_post_process_layer(prev_out, out, process_cmd, dropout_rate=0.0):
12581256
)
12591257
elif cmd == "d": # add dropout
12601258
if dropout_rate:
1261-
out = layers.dropout(
1259+
out = paddle.nn.functional.dropout(
12621260
out,
1263-
dropout_prob=dropout_rate,
1264-
seed=ModelHyperParams.dropout_seed,
1265-
is_test=False,
1261+
p=dropout_rate,
12661262
)
12671263
return out
12681264

@@ -1318,11 +1314,9 @@ def prepare_encoder(
13181314
src_pos_enc.stop_gradient = True
13191315
enc_input = src_word_emb + src_pos_enc
13201316
return (
1321-
layers.dropout(
1317+
paddle.nn.functional.dropout(
13221318
enc_input,
1323-
dropout_prob=dropout_rate,
1324-
seed=ModelHyperParams.dropout_seed,
1325-
is_test=False,
1319+
p=dropout_rate,
13261320
)
13271321
if dropout_rate
13281322
else enc_input

python/paddle/fluid/tests/unittests/dygraph_to_static/bert_dygraph_model.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@ def __init__(
5656
def forward(self, x):
5757
hidden = self._i2h(x)
5858
if self._dropout_rate:
59-
hidden = fluid.layers.dropout(
60-
hidden, dropout_prob=self._dropout_rate, is_test=False
61-
)
59+
hidden = paddle.nn.functional.dropout(hidden, p=self._dropout_rate)
6260
out = self._h2o(hidden)
6361
return out
6462

0 commit comments

Comments
 (0)