r"""Randomly sets some elements of inputs to zeros with the probability :math:`drop\_prob` during training. Commonly used in large networks for regularization and prevent overfitting, see `Improving Neural Networks by Preventing Co-Adaptation of Feature Detectors
| 4 | |
| 5 | |
| 6 | class Dropout(Module): |
| 7 | r"""Randomly sets some elements of inputs to zeros with the probability :math:`drop\_prob` during training. |
| 8 | Commonly used in large networks for regularization and prevent overfitting, see `Improving Neural Networks by Preventing Co-Adaptation of Feature Detectors<https://arxiv.org/abs/1207.0580>`. |
| 9 | Note that we perform dropout only during training, we also rescale(multiply) the output tensor |
| 10 | by :math:`\frac{1}{1 - drop\_prob}`. During inference :class:`~.Dropout` is equal to :class:`~.module.identity.Identity`. |
| 11 | |
| 12 | Args: |
| 13 | drop_prob(:class:`float`): The probability to drop (set to zero) each single element. Default: 0.0 |
| 14 | |
| 15 | Shape: |
| 16 | - input: `(*)`. Input can be of any shape. |
| 17 | - output: `(*)`. Output is of the same shape as input. |
| 18 | |
| 19 | Examples: |
| 20 | >>> import numpy as np |
| 21 | >>> data = Tensor(np.ones(10000000, dtype=np.float32)) |
| 22 | >>> out = F.nn.dropout(data, 1.0 / 3.0, training=True) |
| 23 | >>> assert not out.numpy().all() |
| 24 | >>> out = F.nn.dropout(data, 1.0 / 3.0, training=False) |
| 25 | >>> assert out.numpy().all() |
| 26 | >>> out.numpy() |
| 27 | array([1., 1., 1., ..., 1., 1., 1.], dtype=float32) |
| 28 | |
| 29 | """ |
| 30 | |
| 31 | def __init__(self, drop_prob=0.0, **kwargs): |
| 32 | super().__init__(**kwargs) |
| 33 | self.drop_prob = drop_prob |
| 34 | |
| 35 | def forward(self, inputs): |
| 36 | if self.training: |
| 37 | return dropout(inputs, self.drop_prob, training=True) |
| 38 | else: |
| 39 | return inputs |
| 40 | |
| 41 | def _module_info_string(self) -> str: |
| 42 | return "drop_prob={drop_prob}".format(drop_prob=self.drop_prob) |