liveBook · Manning

Residual connection

As explained in Deep Learning with PyTorch, Second Edition

A connection that bypasses one or more network layers to provide an alternate path for activations or gradients.

A residual connection, also called a skip connection, gives a network a shortcut around a layer or block of layers. Instead of using only the transformed output, the network adds the earlier activation back in. This gives gradients a more direct path during backpropagation. section 8.5.3 section 9.6.1

Why add the shortcut? In a very deep network, gradients reaching early layers pass through a long chain of operations and can become too small for those layers to train effectively. Residual connections help combat this vanishing-gradient problem, making deeper networks practical. section 8.5.3 section 8.8

A small PyTorch residual block

A block can transform x with a convolution, batch normalization, and ReLU, then return out + x. The addition is the residual connection: x bypasses those operations. The two tensors must have compatible sizes for the addition; padding can preserve image size across convolutions. section 8.5.3 section 8.2.1

# In[56]:
class ResBlock(nn.Module):
    def __init__(self, n_chans):
        super(ResBlock, self).__init__()
        self.conv = nn.Conv2d(n_chans, n_chans, kernel_size=3,
                              padding=1, bias=False)      #1
        self.batch_norm = nn.BatchNorm2d(num_features=n_chans)
        torch.nn.init.kaiming_normal_(self.conv.weight,
                                      nonlinearity='relu')     #2
        torch.nn.init.constant_(self.batch_norm.weight, 0.5)
        torch.nn.init.zeros_(self.batch_norm.bias)

    def forward(self, x):
        out = self.conv(x)
        out = self.batch_norm(out)
        out = torch.relu(out)
        return out + x

The return statement shows the shortcut in code: the transformed output is added to the block’s input. section 8.5.3

The same idea appears in a GPT-style transformer decoder. Its residual connections sum outputs at points in the decoder block, giving gradients paths that bypass intervening layers. section 9.6.1

Figure 9.10 GPT-style decoder. The pluses represent residual connections where outputs are summed.
figure

The plus signs mark where the decoder sums outputs through residual connections. section 9.6.1

How Deep Learning with PyTorch, Second Edition covers it

How deep it goes
Explains the gradient problem and demonstrates addition in PyTorch residual blocks without deriving the backpropagation equations.
Examples
Compares convolutional networks with and without a skip connection, then shows a PyTorch residual block and a decoder diagram.
What it assumes
Assumes basic familiarity with network layers and backpropagation while explaining why deep networks need a shortcut.
What you can do after
Build it from scratch in PyTorch by adding a block’s input to its transformed output.
How it connects
Connects residual connections to backpropagation, vanishing gradients, ResNets, convolutional padding, and transformer decoders.

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 Deep Learning with PyTorch, Second Edition. The section links point to the parts of the book it draws on.