Stacked RNNs › Dropout Between RNN Layers

Michael BrenndoerferMay 13, 20254 min read
Language AI HandbookAll chapters
Part 11 of 67Recurrent Neural Networks
Chapter 82Stacked RNNs › Dropout Between RNN Layers
Reading section 6 of 16 · Dropout Between RNN Layers · 4 minSection 6 of 16 · 4 min

Book contents

Section 6 of 16 · 4 min read

Stacked RNNs › Dropout Between RNN LayersOne section · Sections in this chapter6 / 16 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Deeper models are more expressive but also more prone to overfitting. The standard solution in feedforward networks is dropout, which randomly zeroes out a fraction pp of activations during training. Applying dropout naively to recurrent networks requires more care.

Standard Dropout on Non-Recurrent Connections

The original approach, used in early stacked RNNs, applies standard dropout only between layers (on the vertical connections), not on the recurrent connections within a layer. Concretely, the input to layer ℓ\ell at timestep tt is:

h~t(ℓ−1)=Dropout ⁣(ht(ℓ−1), p)\tilde{h}_t^{(\ell-1)} = \text{Dropout}\!\left(h_t^{(\ell-1)},\, p\right)

where Dropout(x,p)\text{Dropout}(x, p) zeroes each element of xx independently with probability pp and scales by 11−p\frac{1}{1-p} to preserve expected values.

The key constraint is that dropout is applied independently at each timestep. This means the dropout mask changes at every timestep. Applying a different mask at each timestep destroys the ability of the network to retain information over time, because any bit of memory that gets dropped at one step is lost permanently for the recurrent path through that step.

Variational Dropout (Fixed Mask Across Time)

Gal and Ghahramani (2016) showed that the correct Bayesian interpretation of dropout for recurrent networks requires using the same dropout mask at every timestep within a single forward pass. This is called variational dropout.

Instead of sampling a new mask at every tt, you sample once per forward pass:

ϵh∼Bernoulli(1−p),ϵx∼Bernoulli(1−p)\epsilon_h \sim \text{Bernoulli}(1-p), \quad \epsilon_x \sim \text{Bernoulli}(1-p)

and apply these fixed masks consistently:

h~t−1(ℓ)=ht−1(ℓ)⊙ϵh,h~t(ℓ−1)=ht(ℓ−1)⊙ϵx\tilde{h}_{t-1}^{(\ell)} = h_{t-1}^{(\ell)} \odot \epsilon_h, \quad \tilde{h}_t^{(\ell-1)} = h_t^{(\ell-1)} \odot \epsilon_x

where ⊙\odot is element-wise multiplication. Using a fixed mask across timesteps means the same units are always dropped within a sequence, which preserves gradient flow through time while still regularizing.

Variational Dropout

Variational dropout applies the same random dropout mask to all timesteps within a single training sequence. This preserves the recurrent gradient flow while regularizing the network, unlike standard dropout which applies a different mask at each step and disrupts memory.

The practical effect is significant. With standard timestep-varying dropout, a model trained with high dropout rates struggles to retain information across many steps because the dropout noise accumulates over the recurrence. With variational dropout, a consistent mask prevents specific units from participating, but the units that do participate are always the same, so gradients flow cleanly through those units across all timesteps.

In PyTorch's built-in nn.LSTM with dropout=p, the dropout is applied between layers (inter-layer) using standard dropout. For variational dropout, you need a custom implementation or a library like torchnlp or WeightDrop from Merity et al.

Dropout Rate Selection

Typical dropout rates for stacked RNNs:

  • 0.1–0.2: Light regularization, suitable for large datasets
  • 0.3–0.5: Standard regularization, good for medium datasets
  • 0.5: Strong regularization, often used for small datasets or highly expressive models

The optimal dropout rate usually increases with depth and decreases with dataset size. For a 3-layer stacked LSTM on a medium-sized language modeling task, rates of 0.3–0.5 between layers are common.

Out[5]:
Visualization
Line chart showing training and validation loss over epochs for three dropout configurations.
Training and validation loss curves for a 3-layer stacked LSTM with no dropout, standard dropout (p=0.4), and variational dropout (p=0.4). Standard dropout reduces overfitting compared to no dropout, but variational dropout achieves lower validation loss because the consistent mask preserves gradient flow through time.

The divergence between training and validation loss without dropout illustrates classic overfitting. Standard dropout closes the gap somewhat, while variational dropout achieves the lowest validation loss because it preserves coherent gradient flow while still regularizing.