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

PyTorch · Function

F.dropout

F.dropout randomly zeroes input elements during training with probability p. Use training=False to disable masking, such as during evaluation.

How it works

F.dropout independently masks input elements during training according to probability p, using Bernoulli samples. When training is false, it leaves dropout inactive; inplace=True performs the operation on the input tensor itself.

Syntax

torch.nn.functional.dropout(input, p=0.5, training=True, inplace=False)

Parameters

NameTypeDescription
inputTensorInput tensor to which dropout is applied.
pfloatProbability of an element being zeroed; default 0.5.
trainingboolWhether to apply dropout; default True.
inplaceboolWhether to perform the operation in place; default False.

Returns

Tensor

Key points

  • Dropout independently removes input elements during training according to the configured probability.
  • The mask is generated with Bernoulli sampling, so different forward calls can produce different element masks.
  • The functional operation follows the behavior documented for the Dropout layer.
  • The p argument expresses the chance that each input element is zeroed.

Common mistakes

  • Set training=False during evaluation; otherwise dropout remains active.
  • inplace=True mutates the input tensor, which can interfere with later computations or autograd.

Example: Apply functional dropout

import torch
import torch.nn.functional as F

x = torch.ones(2, 4)
y = F.dropout(x, p=0.5, training=True)
print(y.shape)  # torch.Size([2, 4])

Applies dropout to a tensor while training is enabled.

When to use it

Reduce overfitting by randomly masking activations during training.

Regularization

Apply dropout directly inside a model’s forward computation without creating a layer instance.

Custom forward passes

Use the training flag to switch dropout off during validation or inference.

Evaluation control

Interview questions

QWhat does F.dropout do?

F.dropout randomly sets input elements to zero with probability p during training, using Bernoulli sampling. The function returns a Tensor.

Follow-up: How would you disable it for evaluation? Pass training=False, commonly by forwarding a module’s training state.

QWhat are the main parameters?

p is the probability that an input element is zeroed, and its default is 0.5. training controls whether dropout is applied and defaults to True.

Follow-up: What behavior should a model use at inference time? It should call the function with training=False so elements are not randomly masked.

QWhat does inplace change?

inplace controls whether the operation writes into the input tensor. It defaults to False; setting it to True performs dropout in place.

Follow-up: When is the default preferable? Use the out-of-place form when the original tensor is needed elsewhere or mutation could complicate gradient computation.

  • torch.nn.Dropout
  • torch.nn.functional.alpha_dropout
  • torch.nn.functional.dropout1d
  • torch.nn.functional.dropout2d
  • torch.nn.functional.dropout3d

Source: torch.nn.functional.dropout — PyTorch 2.14 documentation. Checked 2026-10-16.