liveBook · Manning

Residual connection

As explained in Deep Learning with Python, Third Edition

A residual connection is a neural-network pathway that carries an earlier representation around one or more transformations to a later computation, where it can be combined with the transformed representation.

A deep ConvNet can build useful feature hierarchies, but each additional block can make it harder for error information to reach earlier layers. When that signal becomes too weak or noisy, training can fail. A residual connection gives information a shortcut around a block: save its input, process the input, then add the saved value to the result. section 9.1 section 9.2

The basic move

In pseudocode: `residual = x; x = block(x); x = add([x, residual])`. The block can transform `x`, while the saved input provides a direct path past it. This is especially useful around blocks containing potentially destructive operations such as ReLU or dropout. section 9.2

Before adding the two paths, make their shapes match:

  • If a block changes the number of filters, project the saved input with a 1 × 1 `Conv2D` layer without an activation. section 9.2
  • If the block downsamples with max pooling, use strides in that projection to match the smaller spatial dimensions. `padding="same"` can keep the block’s convolutions from shrinking them unintentionally. section 9.2
Listing 9.3 The target block including a max pooling layer
inputs = keras.Input(shape=(32, 32, 3))
x = layers.Conv2D(32, 3, activation="relu")(inputs)
residual = x                                                         #1
x = layers.Conv2D(64, 3, activation="relu", padding="same")(x)       #2
x = layers.MaxPooling2D(2, padding="same")(x)                         #2
residual = layers.Conv2D(64, 1, strides=2)(residual)                 #3
x = layers.add([x, residual])                                        #4

The Keras listing shows both adjustments together: a convolution-and-pooling path is added to a 1 × 1, stride-2 projection of its saved input. section 9.2

A practical pattern is to repeat blocks of convolutions, optionally pool between them, and place a residual connection around each block. A mini Xception-like dogs-versus-cats ConvNet combines these shortcuts with batch normalization and separable convolutions. It reaches 90.8% test accuracy versus 83.9% for an earlier model, but that comparison does not isolate the effect of residual connections. section 9.2 section 9.5

Addition is not the only way to reconnect paths. A U-Net denoising autoencoder uses concatenative connections from downsampling blocks to corresponding upsampling blocks to help preserve image detail. Keep that use distinct from the additive shortcut used around an individual processing block. section 17.2.2 section 9.2

How Deep Learning with Python, Third Edition covers it

How deep it goes
Works through additive Keras blocks and shape-matching projections, including downsampling, without deriving gradient equations.
Examples
Uses additive pseudocode, Keras ConvNet blocks, a dogs-versus-cats classifier, and a U-Net denoising autoencoder.
What it assumes
Assumes basic familiarity with layers and model training while explaining the gradient problem and shortcut from a simple function chain.
What you can do after
Apply it via the Keras Functional API by adding shortcuts around blocks and projecting them when filters or spatial dimensions change.
How it connects
Links vanishing gradients, backpropagation, convolutional blocks, max pooling, batch normalization, and U-Net skip connections.

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 Python, Third Edition. The section links point to the parts of the book it draws on.