oneflow.nn.functional.dropout

oneflow.nn.functional.dropout(x: Tensor, p: float = 0.5, training: bool = True, generator: Generator = None, *, addend: Tensor)Tensor

During training, randomly zeroes some of the elements of the input tensor with probability p using samples from a Bernoulli distribution.

The documentation is referenced from: https://pytorch.org/docs/1.10/generated/torch.nn.functional.dropout.html.

Parameters
  • x (Tensor) – A Tensor which will be applyed dropout.

  • p (float) – probability of an element to be zeroed. Default: 0.5

  • training (bool) – If is True it will apply dropout. Default: True

  • generator (Generator, optional) – A pseudorandom number generator for sampling

  • addend (Tensor, optional) – A Tensor add in result after dropout, it can be used in model’s residual connection structure. Default: None

Shape:
  • Input: \((*)\). Input can be of any shape

  • Output: \((*)\). Output is of the same shape as input

For example:

Example 1:

>>> import numpy as np
>>> import oneflow as flow


>>> arr = np.array(
...    [
...        [-0.7797, 0.2264, 0.2458, 0.4163],
...        [0.4299, 0.3626, -0.4892, 0.4141],
...        [-1.4115, 1.2183, -0.5503, 0.6520],
...    ]
... )
>>> x = flow.tensor(arr, dtype=flow.float32)
>>> y = flow.nn.functional.dropout(x, p=0)

>>> arr = np.array(
...    [
...        [-0.7797, 0.2264, 0.2458, 0.4163],
...        [0.4299, 0.3626, -0.4892, 0.4141],
...        [-1.4115, 1.2183, -0.5503, 0.6520],
...    ]
... )
>>> x = flow.tensor(arr, dtype=flow.float32)
>>> generator = flow.Generator()
>>> y = flow.nn.functional.dropout(x, p=0.5, generator=generator)

Example 2:

>>> import numpy as np
>>> import oneflow as flow


>>> arr = np.array(
...    [
...        [-0.7797, 0.2264, 0.2458, 0.4163],
...        [0.4299, 0.3626, -0.4892, 0.4141],
...        [-1.4115, 1.2183, -0.5503, 0.6520],
...    ]
... )
>>> x = flow.tensor(arr, dtype=flow.float32)
>>> addend = flow.ones((3, 4), dtype=flow.float32)
>>> y = flow.nn.functional.dropout(x, p=0, addend=addend)
>>> y 
tensor([[ 0.2203,  1.2264,  1.2458,  1.4163],
        [ 1.4299,  1.3626,  0.5108,  1.4141],
        [-0.4115,  2.2183,  0.4497,  1.6520]], dtype=oneflow.float32)

See Dropout for details.