Dropout

class paddle.nn. Dropout ( p=0.5, axis=None, mode='upscale_in_train', name=None ) [source]

Dropout is a regularization technique for reducing overfitting by preventing neuron co-adaption during training as described in the paper: Improving neural networks by preventing co-adaptation of feature detectors The dropout operator randomly sets the outputs of some units to zero, while upscale others according to the given dropout probability.

See dropout for more details.

In dygraph mode, please use eval() to switch to evaluation mode, where dropout is disabled.

Warning

The corresponding functional methods please reference dropout.

Parameters
  • p (float|int, optional) – Probability of setting units to zero. Default: 0.5

  • axis (int|list|tuple, optional) – The axis along which the dropout is performed. Default: None.

  • mode (str, optional) –

    [‘upscale_in_train’(default) | ‘downscale_in_infer’]

    1. upscale_in_train (default), upscale the output at training time

      • train: \(out = input \times \frac{mask}{(1.0 - p)}\)

      • inference: \(out = input\)

    2. downscale_in_infer, downscale the output at inference

      • train: \(out = input \times mask\)

      • inference: \(out = input \times (1.0 - p)\)

  • name (str, optional) – Name for the operation, Default: None. For more information, please refer to Name.

Shape:
  • input: N-D tensor.

  • output: N-D tensor, the same shape as input.

Examples

>>> import paddle
>>> paddle.seed(2023)

>>> x = paddle.to_tensor([[1, 2, 3], [4, 5, 6]], dtype="float32")
>>> m = paddle.nn.Dropout(p=0.5)

>>> y_train = m(x)
>>> print(y_train)
Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
[[2., 4., 0.],
 [8., 0., 0.]])

>>> m.eval()  # switch the model to test phase
>>> y_test = m(x)
>>> print(y_test)
Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
[[1., 2., 3.],
 [4., 5., 6.]])
forward ( input )

forward

Defines the computation performed at every call. Should be overridden by all subclasses.

Parameters
  • *inputs (tuple) – unpacked tuple arguments

  • **kwargs (dict) – unpacked dict arguments

extra_repr ( )

extra_repr

Extra representation of this layer, you can have custom implementation of your own layer.

Used in the guide/tutorials