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

TensorFlow · Class

layers.Dropout

Applies dropout regularization by randomly zeroing input units during training and rescaling the units that remain. During inference, it leaves the input unchanged.

How it works

During training, the layer randomly replaces input units with zero according to rate and scales the surviving values by 1 / (1 - rate). It does nothing when training is false, so inference retains all input values. The training flag is supplied to call(), and model.fit sets it automatically.

Syntax

tf.keras.layers.Dropout(rate, noise_shape=None, seed=None, **kwargs)

Parameters

NameTypeDescription
ratefloatFraction of input units to drop; a float from 0 through, but not including, 1.
noise_shape1D integer tensorShape of the binary dropout mask multiplied by the input; a one-dimensional integer tensor.
seedPython integerOptional Python integer random seed.
inputsTensorInput tensor of any rank.
trainingPython boolPython boolean selecting training behavior or inference behavior.

Returns

A tensor with the same shape as the input, with dropout applied only in training mode.

Key points

  • At each training step, rate determines the probability that an input unit is set to zero.
  • Surviving values are rescaled so dropout preserves the expected total input magnitude.
  • The layer accepts tensors with any rank, provided the mask shape is compatible when noise_shape is used.
  • noise_shape controls the mask dimensions and therefore which positions share dropout decisions.
  • seed optionally initializes the random-mask generator for the layer.
  • Standard model.fit training invokes the layer in training mode without requiring manual propagation of the flag.

Common mistakes

  • Dropout is inactive unless the layer is called with training=True; forgetting this in a custom loop disables regularization.
  • Inference does not randomly remove units, so evaluation and prediction are deterministic with respect to dropout.
  • Choose rate within the documented interval; it represents the fraction of input units dropped.

Example: Training versus inference

import tensorflow as tf

layer = tf.keras.layers.Dropout(rate=0.5, seed=7)
x = tf.ones((2, 4))

training_output = layer(x, training=True)
inference_output = layer(x, training=False)
print(training_output)
print(inference_output)

Creates a dropout layer, applies it in training mode to randomly mask and rescale inputs, then applies it in inference mode where inputs pass unchanged.

When to use it

Reduce overfitting

A dense network memorizes training examples and performs poorly on unseen data.

Share a mask across timesteps

A recurrent or attention-style input has batch, time, and feature dimensions.

Toggle training behavior

A custom training loop needs explicit control over stochastic regularization.

Interview questions

QHow does Dropout regularize a model?

The layer drops each input unit with probability rate during training. Values that remain are multiplied by 1 / (1 - rate), preserving the expected sum of the inputs.

Follow-up: What changes at inference time? The layer bypasses dropout and returns inputs without dropping values.

QHow can the same dropout mask be used across sequence timesteps?

The noise_shape parameter defines the shape of the binary mask multiplied with the input. For inputs shaped (batch_size, timesteps, features), (batch_size, 1, features) shares one mask across timesteps.

Follow-up: What requirement applies to noise_shape? It must be compatible with the input shape so the mask can be broadcast across it.

QHow is training mode controlled?

The training call argument is a Python boolean that selects training or inference behavior. model.fit supplies training=True automatically, while custom calls should pass the value explicitly when needed.

Follow-up: What should a custom evaluation loop pass? It should call the layer with training=False to disable dropout.

  • tf.nn.dropout
  • tf.keras.layers.Attention
  • tf.keras.layers.LSTMCell

Source: tf.keras.layers.Dropout. Checked 2026-10-15.