Skip to content

Residual Connections in Transformers

Normalization and Residual Connections in Transformers

Section titled “Normalization and Residual Connections in Transformers”

The Transformer architecture, a cornerstone of modern Natural Language Processing (NLP) and beyond, relies on several key components for its remarkable performance. Among these, the combination of Residual Connections and Layer Normalization, often appearing as an “Add & Norm” block, is crucial. This mechanism plays a vital role in enabling the training of very deep Transformer models by stabilizing the learning process and mitigating issues like vanishing gradients.

This chapter will explore the significance of Layer Normalization and Residual Connections, explain their mechanics within the Transformer, discuss their benefits, and touch upon practical implementation considerations.

Recall that a Transformer encoder or decoder is composed of a stack of identical layers (or blocks). Each of these layers typically contains two main sub-layers:

  1. A Multi-Head Self-Attention mechanism.
  2. A position-wise Fully Connected Feed-Forward Network (FFN).

The “Add & Norm” operation is applied around each of these two sub-layers. That is, the output of each sub-layer is LayerNorm(x + Sublayer(x)), where Sublayer(x) is the function implemented by the sub-layer itself (e.g., multi-head attention or FFN) and x is the input to that sub-layer.

Residual Connections, also known as skip connections, were popularized by ResNet and are designed to address the vanishing gradient problem in deep neural networks. As networks get deeper, gradients can become increasingly small as they are propagated backward, making it difficult for earlier layers to learn effectively.

A residual connection bypasses one or more layers by adding the input of a block x directly to its output F(x). The output of the block thus becomes F(x) + x. This allows gradients to flow more directly through the network during backpropagation, making it easier to train deeper architectures.

In simple terms, the network learns a ‘residual mapping’ F(x). If the identity mapping is optimal, it’s easier for the network to learn to make F(x) close to zero than to learn an identity mapping from scratch with a stack of non-linear layers. The mathematical representation is: Output_of_block = Sublayer_Output + Input_to_block.

Layer Normalization (LN) is a technique used to normalize the inputs across the features for each data sample independently. Unlike Batch Normalization, which normalizes across the batch dimension, Layer Normalization computes the mean and variance used for normalization from all of the summed inputs to the neurons within a single layer for a single training sample.

This makes LN independent of batch size and particularly suitable for recurrent neural networks and Transformers where sequence lengths can vary. It helps stabilize the hidden state dynamics, speeds up training, and often improves generalization.

Mathematically, for a given input vector x (representing the activations in a layer for a single sample), Layer Normalization is typically computed as: First, calculate the mean (mu) and standard deviation (sigma) of the elements in x for that sample. Then, normalize x to get x_hat: x_hat = (x - mu) / sqrt(sigma^2 + epsilon), where epsilon is a small constant for numerical stability. Finally, the normalized output y is scaled and shifted using learnable parameters gamma (scale) and beta (shift), specific to the layer: y = gamma * x_hat + beta.

This scaling and shifting allows the network to learn if and how much normalization is optimal for that layer, preserving its representational power.

The “Add & Norm” Block in Transformers

Section titled “The “Add & Norm” Block in Transformers”

In the Transformer architecture, Layer Normalization is typically applied after the residual connection. The sequence of operations for each sub-layer (Multi-Head Attention or FFN) is as follows:

  1. The input x is passed through the sub-layer (e.g., Multi-Head Attention) to get Sublayer_Output.
  2. The residual connection is applied: Residual_Output = x + Sublayer_Output.
  3. Layer Normalization is applied to this sum: Final_Output = LayerNorm(Residual_Output).

Let x_0 be the input to a Transformer layer. The first sub-layer (Multi-Head Attention) process would be: attn_out = MultiHeadAttention(x_0) add_norm_1_out = LayerNorm(x_0 + attn_out) This add_norm_1_out then becomes the input to the second sub-layer (Feed-Forward Network): ffn_out = FeedForwardNetwork(add_norm_1_out) add_norm_2_out = LayerNorm(add_norm_1_out + ffn_out) This add_norm_2_out is the final output of the Transformer layer.

This specific ordering (add then norm, also known as “post-LN”) was used in the original Transformer paper. Variants like “pre-LN” (Sublayer(LayerNorm(x)) + x) also exist and are sometimes found to offer more stable training in very deep networks, though post-LN remains common.

The combination of residual connections and layer normalization provides several key benefits:

  • Stabilizes Training: Layer normalization helps keep activation values within a reasonable range, preventing exploding or vanishing gradients and making the optimization landscape smoother. This stabilizes the training process, especially for deep networks.
  • Enables Deeper Networks: Residual connections are crucial for training very deep networks by ensuring gradients can propagate effectively to earlier layers. Transformers can have dozens of layers.
  • Improves Learning Speed and Convergence: By facilitating gradient flow and stabilizing activations, “Add & Norm” often leads to faster convergence and allows for higher learning rates.
  • Enhances Model Performance and Generalization: A more stable training process and the ability to train deeper models contribute to better overall performance and generalization on unseen data.

When implementing or working with Transformers, consider these points related to normalization and residual connections:

  • Initialization: Proper initialization of network weights (e.g., Xavier/Glorot or He initialization) and the gamma and beta parameters in Layer Normalization (often gamma to 1s, beta to 0s) is important for stable training.
  • Layer Normalization Placement: While post-LN is standard, pre-LN is an alternative that some research suggests can improve stability and performance for extremely deep Transformers. The choice might depend on the specific architecture and task.
  • Hyperparameter Tuning: Parameters like the model depth (number of layers), hidden dimensions, number of attention heads, and dropout rates interact with the effects of normalization and residuals. Careful tuning is often required.
  • Computational Overhead: Layer Normalization adds some computational cost per layer. However, its benefits in terms of training stability and model depth usually outweigh this cost.

The “Add & Norm” component, combining residual connections with layer normalization, is a fundamental design element of the Transformer architecture. It is not merely an add-on but an integral part of what makes Transformers trainable and effective at great depths.

By facilitating gradient flow, stabilizing activations, and enabling deeper models, these techniques have been pivotal in the success of Transformers across a wide range of NLP and other sequence modeling tasks. Understanding their role is essential for anyone working with or developing Transformer-based models. As research progresses, new normalization techniques or variations may emerge, but the core principles of managing signal propagation and stabilizing learning will remain critical.

For further study, refer to the original Transformer paper (“Attention Is All You Need”) and research on Layer Normalization variants and their impact on deep learning model training.