Residual Networks
Visual explanation :
ResNet (actually) explained in under 10 minutes
We have
What residuals solve
Residual connections, also known as skip connections, address the problem of vanishing gradients and degradation in very deep neural networks.
The vanishing gradient problem, in simple terms, is when the neural network struggles to learn and update its parameters, especially in the earlier layers, due to extremely small gradients.
- Basic concept:
- Neural networks learn by adjusting their weights based on the error gradient.
- This gradient is calculated through backpropagation, flowing backwards from the output layer to the input layer.
- The problem:
- In deep networks (those with many layers), as the gradient flows backward, it can become extremely small.
- By the time it reaches the earlier layers, it's often too tiny to cause any significant updates.
As networks become deeper, they can struggle to learn identity mappings and may paradoxically perform worse than shallower networks. Residuals allow the network to learn residual functions with reference to the layer inputs, rather than learning unreferenced functions.
The term "unreferenced functions" refers to the traditional approach where the network tries to learn the entire mapping H(x) without explicitly preserving the input information.
When we say "residual functions with reference to the layer inputs", we're describing the core idea behind residual networks. Here's a more detailed explanation:
- In a traditional neural network layer, we try to learn a function H(x) that maps input x to some desired output.
- In a residual network, instead of trying to learn H(x) directly, we reformulate the problem. We try to learn a function F(x) such that: H(x) = F(x) + x Here, F(x) is called the residual function.
- The "reference" in this case is the input x. We're learning F(x) with respect to (or in reference to) the input x, rather than trying to learn the entire transformation H(x) from scratch.
- This means that the network only needs to learn the difference (or residual) between the input and the desired output, rather than the full transformation.
Why they work
Residuals work because they provide a direct path for gradients to flow backward through the network during backpropagation. This helps mitigate the vanishing gradient problem. Additionally, they allow the network to easily learn identity mappings by "skipping" layers if needed. This means that adding more layers doesn't hurt performance, as the network can choose to use or ignore these extra layers.
To use residual connections correctly:
- Add them between layers or blocks of layers in your network.
- Ensure that the dimensions of the input and output match. If they don't, use a 1x1 convolution to adjust the dimensions.
- Use them in combination with batch normalization and ReLU activations for best results.
- Be careful not to overuse them, as this can lead to diminishing returns.
ResNet Block
Residuals are used in what is called a ResNet Block, which architecture is as follows :
To match dimensions, 1x1 Convolutions are used to increase or decrease channels.
Let's dive deeper into how 1x1 convolutions are used to adjust the number of channels in residual blocks.
- Basic concept of 1x1 convolutions:
- A 1x1 convolution is essentially a linear transformation applied to each spatial location independently.
- It operates on a single pixel at a time across all input channels.
- Channel adjustment:
- The number of filters in the 1x1 convolution determines the number of output channels.
- If you want to increase channels: use more filters than input channels.
- If you want to decrease channels: use fewer filters than input channels.
How it works in residual blocks:
- Let's say we have an input with shape
channels.( 𝐻 𝑒 𝑖 𝑔 ℎ 𝑡 , 𝑊 𝑖 𝑑 𝑡 ℎ , 𝐶 𝑖 𝑛 ) - Our residual function outputs
channels.( 𝐻 𝑒 𝑖 𝑔 ℎ 𝑡 , 𝑊 𝑖 𝑑 𝑡 ℎ , 𝐶 𝑜 𝑢 𝑡 ) - If
, we use a 1x1 convolution with𝐶 𝑖 𝑛 ≠ 𝐶 𝑜 𝑢 𝑡 filters on the shortcut path.𝐶 𝑜 𝑢 𝑡 - If spatial dimensions also need to change, you can use a stride > 1 in the 1x1 convolution.
Practical example : U-net architecture

