Skip to content
class

Dropout2d

extendsModule
Dropout2d(p: float = 0.5, inplace: bool = False)

Implementing kernel

C++ engine symbols that back this Python API.

Randomly zero entire feature-map channels during training for 4-D inputs.

For image-like inputs of shape (N, C, H, W) a single Bernoulli draw per channel per sample determines whether that entire spatial map is zeroed:

yn,c,:,:={xn,c,:,:1−pwith probability 1−p0with probability p(training)y_{n,c,:,:} = \begin{cases} \dfrac{x_{n,c,:,:}}{1 - p} & \text{with probability } 1 - p \\[6pt] \mathbf{0} & \text{with probability } p \end{cases} \quad \text{(training)}

Because adjacent pixels in a convolutional feature map are strongly correlated, zeroing individual pixels (as standard Dropout does) has little regularisation effect. Zeroing the entire channel forces the network to not rely on any single feature map.

Parameters

pfloat= 0.5
Probability of zeroing an entire channel. Must be in [0, 1]. Default: 0.5.
inplacebool= False
If True, modify the input in place. Default: False.

Notes

  • Input: (N, C, H, W) — batch of 2-D feature maps.
  • Output: (N, C, H, W) — same shape; zeroed channels are zero across the full H × W spatial extent.

The Bernoulli mask is sampled on the (N, C) axes and broadcast over (H, W). Spatial structure within a channel is therefore fully preserved — only the decision of which channels survive varies.

Examples

Typical use after a convolutional layer:
>>> import lucid, lucid.nn as nn
>>> drop2d = nn.Dropout2d(p=0.25)
>>> _ = drop2d.train()
>>> x = lucid.ones(2, 16, 8, 8)    # (N=2, C=16, H=8, W=8)
>>> y = drop2d(x)
>>> y.shape
(2, 16, 8, 8)
>>> # Roughly 25 % of the 16 channels are entirely zero per sample
No-op in eval mode:
>>> _ = drop2d.eval()
>>> y_eval = drop2d(lucid.ones(2, 4, 4, 4))
>>> float(y_eval.sum()) == 128.0
True

See Also

  • Dropout—Element-wise scalar dropout.
  • Dropout1d—Channel-wise dropout for 3-D (sequence) inputs.
  • Dropout3d—Channel-wise dropout for 5-D (volumetric) inputs.
  • FeatureAlphaDropout—Channel-wise variant that preserves SELU statistics.

Used by 1

Constructors

1
dunder

__init__

→None
__init__(p: float = 0.5, inplace: bool = False)

Initialise the Dropout2d module. See the class docstring for parameter semantics.

Instance methods

2
fn

extra_repr

→str
extra_repr()

Return a string representation of the layer's configuration.

fn

forward

→Tensor
forward(input: Tensor)

Apply dropout to the input tensor.

Parameters

inputTensor
Input tensor of arbitrary shape.

Returns

Tensor

Output tensor of the same shape as input; in eval mode this is the identity.