liveBook · Manning

Residual connection

As explained in Build a Text-to-Image Generator (from Scratch)

A direct link that transfers representations between corresponding layers or stages of a neural network.

A residual connection lets an input bypass a transformation and adds it to the transformation’s output: output = input + transformation(input). The transformed path can learn new features while the direct path preserves information. In deep networks, this shortcut also helps combat vanishing gradients, making deeper models easier to train. section 13.3.1 section 3.3.4

In a transformer encoder block, the pattern appears twice: once around multi-head self-attention and once around the feed-forward network. The illustrated implementation normalizes the input before each transformation, then adds the original input back. Decoder sublayers use residual connections too, including the cross-attention sublayer. section 3.3.4 section 2.3.2

Listing 3.5 Creating an encoder block
class Block(nn.Module):
    def __init__(self, config):
        super().__init__()
        self.attention = MultiHeadAttention(config)
        self.layernorm_1 = nn.LayerNorm(config.hidden_size)
        self.mlp = MLP(config)
        self.layernorm_2 = nn.LayerNorm(config.hidden_size)
    def forward(self, x, output_attentions=False):
        attention_output, attention_probs = \
        self.attention(self.layernorm_1(x),  #1
                       output_attentions=output_attentions)
        x = x + attention_output  #2
        mlp_output = self.mlp(self.layernorm_2(x))  #3
        x = x + mlp_output  #4
        if not output_attentions:
            return (x, None)
        else:
            return (x, attention_probs)

Follow the two `x = x + ...` lines: each adds a sublayer’s result to the input that reached that sublayer. section 3.3.4

ResNet50 applies the same additive shortcut idea inside convolutional bottleneck blocks. A related use of skip connections appears in a denoising U-Net: links between encoder and decoder stages of the same resolution merge earlier feature maps into the upsampling path. Those links restore fine detail that downsampling could otherwise lose; unlike the transformer block’s explicit addition, the cited U-Net description does not specify the merge operation. section 13.3.1 section 6.2.2 section 6.5

Skip connections are useful, but they do not select relevant features on their own. In the denoising U-Net, redundant feature extraction can make that selection harder; attention is added to emphasize important image regions while disregarding irrelevant ones. section 7.1.1

How Build a Text-to-Image Generator (from Scratch) covers it

How deep it goes
Defines additive residual connections and shows two implemented transformer additions; describes U-Net feature merging without specifying its merge operation.
Examples
Shows a transformer encoder block with two additions, ResNet50 bottleneck blocks, and a denoising U-Net with matching encoder–decoder links.
What it assumes
Assumes familiarity with neural-network layers and tensors while defining the residual addition explicitly.
What you can do after
Build it from scratch in a transformer encoder block by adding each attention or feed-forward output to its sublayer input.
How it connects
Links layer normalization, multi-head attention, feed-forward networks, vanishing gradients, ResNet bottlenecks, U-Nets, and attention mechanisms.

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 Build a Text-to-Image Generator (from Scratch). The section links point to the parts of the book it draws on.