Hello! Welcome back to our exploration of advanced neural network architectures.
In our last lesson, we successfully built a complete Convolutional Neural Network (CNN) for image classification. We saw that even a simple CNN dramatically outperforms a basic dense network on image data. A natural next question is: if adding a few convolutional layers helps so much, can we get even better performance by adding many more layers to create a much "deeper" network?
Counter-intuitively, the answer isn't so simple. As we stack more and more layers, networks can become incredibly difficult to train, and their performance can actually get worse. This lesson tackles a groundbreaking solution to this problem. Your learning outcome is to explain how residual connections (ResNets) enable the training of very deep networks.
We'll unpack the problem with deep networks and then dive into the elegant architectural solution that changed the landscape of deep learning.
1. The Problem with Very Deep Networks
In the early days of deep learning, there was a race to build deeper models. However, researchers quickly ran into two major obstacles: the vanishing gradient problem and a surprising phenomenon known as the degradation problem.
The Vanishing Gradient Problem
We've touched on this before. During backpropagation, the gradients are calculated using the chain rule, which involves multiplying many small numbers together as the error signal travels backward from the output layer to the input layer. In a very deep network, this can cause the gradient to shrink exponentially, becoming so small that the weights of the early layers barely update. As a result, the initial layers of the network fail to learn effectively.
To understand this more formally, it's helpful to review the mechanics of backpropagation.
Intuitive Explanation of Skip Connections in Deep Learning
The article 'Intuitive Explanation of Skip Connections in Deep Learning' from The AI Summer provides a great refresher on how the chain rule in backpropagation leads to vanishing gradients.
Please read the first three sections, up to and including 'Backpropagation and partial derivatives'. Focus on the core idea: as you go backward through the network, the gradient is the result of many multiplications, and if these terms are less than 1, the gradient shrinks.
The Degradation Problem
Even more puzzling is the degradation problem. One might expect that a deeper model should, at worst, perform as well as a shallower one. After all, the deeper model could theoretically just learn to copy the behavior of the shallower model in its initial layers and treat the extra layers as identity functions (i.e., they just pass the input through unchanged).
In practice, this doesn't happen. Experiments showed that as "plain" networks (simple stacks of layers) got deeper, their accuracy on both the training and testing sets would first saturate and then quickly degrade.
Why is learning an identity function so hard for a stack of non-linear layers? This is the central challenge that ResNet was designed to solve.
The following video provides an excellent visual summary of these problems.
ResNet (actually) explained in under 10 minutes
The video 'ResNet (actually) explained' by rupert ai starts by clearly illustrating why adding more layers can actually make performance worse.
Watch from the beginning to 01:40. Notice how the input signal can get lost or distorted as it passes through many layers, leading to poor performance.
2. The Solution: Learning the Residual
The insight behind Residual Networks (ResNets) is brilliantly simple. Instead of forcing a set of layers to learn a target mapping H(x), let's reframe the problem. We'll have the layers learn a residual function F(x) = H(x) - x.
The original mapping can then be calculated as:
This is implemented using a "shortcut" or "skip connection" that passes the input x directly to the end of the block and adds it to the output of the layers, F(x).

Why is this easier?
Imagine the ideal mapping is simply the identity function (H(x) = x).
- In a plain network, the stack of layers would have to learn to approximate the identity function, which is difficult.
- In a residual network, the layers just need to learn a residual
F(x)that is equal to zero. It's much easier for a network to push its weights towards zero than to fine-tune them to approximate an identity mapping.
This reframing makes the network's job significantly simpler, especially when the optimal transformation is close to the identity.
Residual Networks (ResNet) [Physics Informed Machine Learning]
Let's watch a clip from Steve Brunton's 'Residual Networks' video. He uses a super-resolution task as a fantastic, intuitive example for why learning the residual is a good idea.
Watch from 00:44 to 01:49. Think about why it's more efficient for the network to learn only the difference between the low-res and high-res images, rather than generating the high-res image from scratch.
3. The Residual Block in Detail
A residual block is the fundamental building block of a ResNet. A common variant consists of:
- A 3x3 Convolutional Layer
- A Batch Normalization Layer
- A ReLU Activation Function
- Another 3x3 Convolutional Layer
- Another Batch Normalization Layer
- An addition operation, where the input to the block (
x) is added to the output of the second batch norm layer. - A final ReLU Activation Function.
The key is the "skip connection" in step 6. Let's look at this structure more closely.
Residual Networks (ResNet) and ResNeXt
The 'Dive into Deep Learning' book provides a thorough explanation of the residual block, complete with diagrams and code examples. This will give us a more formal understanding.
Read Section 8.6.2, 'Residual Blocks'. Pay attention to the diagram comparing a regular block to a residual block and how the code implements the forward pass with the addition Y += X.
4. How Residual Connections Enable Deeper Networks
Now we can connect the pieces and understand exactly how this architecture solves our two main problems.
Solving the Vanishing Gradient Problem
The skip connection creates an alternative path for the gradient to flow during backpropagation. The gradient can now flow backward through the identity connection uninterrupted. Since the derivative of the identity function (x) with respect to x is 1, the gradient is passed back directly, without being diminished by multiplications in the weight layers.
This acts like an express lane for the gradient, ensuring that even the earliest layers in a very deep network receive a strong error signal and can continue to learn effectively.
Intuitive Explanation of Skip Connections in Deep Learning
Let's return to the 'Intuitive Explanation of Skip Connections' article to see how the addition in a residual block helps the gradient.
Read the sections 'Skip connections for the win' and 'ResNet: skip connections via addition'. Notice the mathematical representation: the gradient of the loss with respect to x involves a + 1 term, which prevents the gradient from vanishing.
Solving the Degradation Problem
By making it easy to learn the identity function, residual blocks ensure that adding more layers can't hurt performance. In the worst-case scenario, the network can simply learn to set the weights of the extra layers to zero, making them identity mappings. The deeper model then becomes equivalent to the shallower model. This property, where deeper function classes contain the shallower ones, is called nested function classes. This guarantees that a deeper model will have a training error that is at least as low as its shallower counterpart.
ResNet (actually) explained in under 10 minutes
Let's hear the summary from the 'ResNet (actually) explained' video on how these connections allow for extremely deep networks.
Watch from 03:41 to 04:38. The key takeaway is that because each block can easily learn to be an identity function, the network can effectively choose to 'use' only as many blocks as it needs.
Test your understanding!
A colleague is training a very deep "plain" CNN (without skip connections) and finds that its training accuracy is lower than a much shallower version of the same network. They believe the deep network must be overfitting. Are they correct? Why or why not?
Show answer
They are likely incorrect. Overfitting is when a model performs well on training data but poorly on test data. In this case, the training accuracy itself is getting worse, which is a classic symptom of the degradation problem, not overfitting. The deep, plain network is struggling to optimize its parameters effectively, possibly due to vanishing gradients, and cannot even learn a function as good as its shallower counterpart.
5. A Practical Detail: Handling Dimension Mismatches
There's one final detail we need to address. What happens if the convolutional layers inside the residual block change the dimensions of the data? For example, a convolution with stride=2 will halve the height and width of the feature maps. In this case, we can't directly add the input x to the output F(x) because their shapes don't match.
There are two common solutions:
- Zero-Padding: Pad the input
xwith zeros to match the new channel dimension. This is simple but adds no new learnable information. - 1x1 Convolution: Use a
1x1convolutional layer on the skip connection itself. This is a very powerful technique. A1x1convolution can be configured with a specific stride and number of filters to intelligently downsamplexand change its channel depth to precisely match the dimensions ofF(x). This is the approach used in the original ResNet paper.
ResNet (actually) explained in under 10 minutes
The 'ResNet (actually) explained' video provides a clear and detailed explanation of how to handle these dimension mismatches.
Watch from 06:27 to 09:21. This is a crucial practical concept. Focus on how a stride=2 convolution in the main path requires a corresponding downsampling operation on the skip connection, and how a 1x1 convolution is used to solve this.
Conclusion
In this lesson, we explored a fundamental breakthrough in deep learning. We saw how simply stacking layers in "plain" networks leads to performance degradation and training difficulties due to vanishing gradients.
Key Takeaways:
- The Problem: Very deep networks are hard to train due to the vanishing gradient and degradation problems.
- The Core Idea: ResNets reframe the learning problem. Instead of learning a direct mapping
H(x), the network learns a residual mappingF(x) = H(x) - x. - The Mechanism: This is achieved via skip connections, which add the block's input
xto its outputF(x). - The Benefit (Gradient Flow): Skip connections create an express lane for gradients to flow backward through the network, mitigating the vanishing gradient problem.
- The Benefit (Identity Mapping): Residual blocks can easily learn identity functions, solving the degradation problem and enabling the effective training of networks with hundreds or even thousands of layers.
This simple but powerful idea of the residual connection has been incredibly influential and is now a standard component not only in modern CNNs but also in other advanced architectures like Transformers, which we will see later in the course.
Preview of the Next Lesson:
So far, we have focused on architectures for processing static, spatial data like images. But what about sequential data, where order matters, such as time series or text? In our next lesson, we will pivot to a new class of models designed for this purpose, starting with the foundational architecture: the Recurrent Neural Network (RNN).