Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

PyTorch · Class

nn.Dropout

torch.nn.Dropout randomly zeros input elements during training with probability p. It samples independently on each forward call and preserves the input shape.

How it works

Dropout is a module that independently samples input elements for removal on each forward pass while training. Removed values become zero, and the output keeps the input shape; evaluation mode disables the stochastic operation.

Syntax

class torch.nn.Dropout(p=0.5, inplace=False)

Parameters

NameTypeDescription
pfloatProbability that an input element is zeroed; defaults to 0.5.
inplaceboolWhether to perform the operation in-place; defaults to False.

Returns

A Tensor with the same shape as the input.

Key points

  • It regularizes a model by randomly removing individual input elements during training with probability p.
  • Each forward invocation samples a fresh independent zeroing pattern.
  • The zeroing decision for each element follows Bernoulli sampling.
  • Channel-level behavior is also independently applied on every forward call.
  • The module accepts tensors without a documented rank restriction.
  • Dropout preserves the tensor dimensions while transforming its values.
  • The p parameter expresses the per-element zeroing probability.

Common mistakes

  • Dropout's random zeroing applies during training; switch the containing module to evaluation mode for inference.
  • A new mask is sampled on every forward call, so repeated training-mode calls can produce different outputs.
  • Setting inplace=True mutates the input tensor, which can conflict with reuse or autograd expectations.

Example: Training and evaluation behavior

import torch
from torch import nn

layer = nn.Dropout(p=0.2)
x = torch.randn(20, 16)
layer.train()
y_train = layer(x)
layer.eval()
y_eval = layer(x)
print(y_train.shape, y_eval.shape)

The same layer applies stochastic dropout in training mode and disables it in evaluation mode while preserving the input shape.

When to use it

Element-wise regularization

Training dense or general neural-network layers

Robust feature learning

Reducing reliance on individual activations

Shape-preserving noise

Applying stochastic masking to tensors of arbitrary shape

Interview questions

QWhat does p control in nn.Dropout?

p is the probability that an individual input element is zeroed during training. The default is 0.5, and the selection is sampled independently for each forward call.

Follow-up: How does the module behave for arbitrary tensor ranks? It accepts any input shape and returns a tensor with that same shape.

QWhat is the purpose of inplace?

The inplace argument controls whether dropout writes directly into its input. It defaults to False; setting it to True performs the operation in-place.

Follow-up: What shape constraints does this class impose? None are documented: the input may have any shape.

QHow does nn.Dropout differ between training and evaluation?

During training, selected elements are set to zero according to independent Bernoulli samples, while evaluation does not apply the training-time dropout operation. The returned tensor has the same shape as the input.

Follow-up: What is the documented return type? The forward pass returns a Tensor.

  • torch.nn.Dropout1d
  • torch.nn.Dropout2d
  • torch.nn.Dropout3d
  • torch.nn.AlphaDropout
  • torch.nn.FeatureAlphaDropout

Source: Dropout — PyTorch main documentation. Checked 2026-10-16.