Reduce overfitting
A dense network memorizes training examples and performs poorly on unseen data.
TensorFlow · Class
Applies dropout regularization by randomly zeroing input units during training and rescaling the units that remain. During inference, it leaves the input unchanged.
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.
tf.keras.layers.Dropout(rate, noise_shape=None, seed=None, **kwargs)| Name | Type | Description |
|---|---|---|
rate | float | Fraction of input units to drop; a float from 0 through, but not including, 1. |
noise_shape | 1D integer tensor | Shape of the binary dropout mask multiplied by the input; a one-dimensional integer tensor. |
seed | Python integer | Optional Python integer random seed. |
inputs | Tensor | Input tensor of any rank. |
training | Python bool | Python boolean selecting training behavior or inference behavior. |
A tensor with the same shape as the input, with dropout applied only in training mode.
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.
A dense network memorizes training examples and performs poorly on unseen data.
A recurrent or attention-style input has batch, time, and feature dimensions.
A custom training loop needs explicit control over stochastic regularization.
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.
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.
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.dropouttf.keras.layers.Attentiontf.keras.layers.LSTMCell