liveBook · Manning

Residual connection

As explained in Transformers in Action

A residual connection adds a layer's input to its transformed output to facilitate optimization of deep neural networks.

A residual connection adds a sublayer’s input to its transformed output, preserving input context while allowing information to flow directly through a deep network. In a transformer encoder, attention and feed-forward sublayers use this pattern before layer normalization. section 2.2.1

For an input x and sublayer function Sublayer, the output is LayerNorm(x + Sublayer(x)). The addition provides a path that can bypass part of the transformation, while dropout is applied before the residual result is finalized. section 2.2.1

A transformer encoder layer applies residual connections around its main components:

  • Compute multi-head self-attention, apply dropout, add the original input, and normalize the result. section 2.2.1
  • Process the result with a position-wise feed-forward network, apply dropout, add the sublayer input, and normalize again. section 2.2.1
  • Stack the resulting layers so essential input details and relationships remain available throughout encoding. section 2.2.1
Listing 2.1 Simplified encoder layer example
class EncoderLayer(nn.Module):
    def __init (self, d_model, nhead, dim_feedforward, dropout=0.1): 
        super(). init ()

        self.self_attn = nn.MultiheadAttention
(d_model, nhead, dropout=dropout)   #1
        self.feed_forward = nn.Sequential(   #2
            nn.Linear(d_model, 2 * dim_feedforward),
            RELU(input_size=dim_feedforward, output_size=d_model), 
            nn.Dropout(dropout)
            )
        self.norm1 = LayerNorm(d_model)   #3
        self.norm2 = LayerNorm(d_model)
        self.dropout1 = nn.Dropout(dropout)   #4
        self.dropout2 = nn.Dropout(dropout)

def forward(self, x, mask=None
    # self-attention layer   #5
    attn_output, _ = self.self_attn(x, x, x, attn_mask=mask) 
    x = x + self.dropout1(attn_output)
    x = self.norm1(x)

    # feed-forward layer  #6
    ff_output = self.feed_forward(x) 
    x = x + self.dropout2(ff_output) 
    x = self.norm2(x)
    return x

Inspect a simplified encoder-layer implementation showing attention, feed-forward processing, dropout, addition, and layer normalization. section 2.2.1

How Transformers in Action covers it

How deep it goes
Explains the addition, normalization order, dropout placement, and encoder-layer use, but stops short of optimization mathematics.
Examples
Demonstrates residual additions around attention and feed-forward outputs in a simplified encoder-layer listing.
What it assumes
Assumes transformer encoder terminology while identifying self-attention and feed-forward networks as the layer’s principal sublayers.
What you can do after
Build it from scratch by adding dropout-regularized sublayer outputs to their inputs before applying layer normalization.
How it connects
Connects residual connections with transformer encoders, self-attention, feed-forward networks, dropout, and layer normalization.

Also explained in

Compare how 12 books explain residual connection →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Transformers in Action. The section links point to the parts of the book it draws on.