Residual Networks Skip Connections
# Residual Networks & Skip Connections
## Introduction & Motivation
Residual Networks: enable deeper architectures. Skip connections, residual blocks. Applications: very deep networks, improved gradient flow.
Motivation: Solve vanishing gradient in deep networks.
Applications: Image classification, feature extraction.
---
## Core Concepts & Theory
### Residual Block
Skip connection addition.
### Bottleneck Architecture
Dimensionality reduction.
### Dense Connections
All-to-all shortcuts.
### Highway Networks
Gated skip connections.
---
## Mathematical Formulation
Residual Block:
$$y = F(x) + x$$
Bottleneck:
$$y = ext{Conv}(1x1) o ext{Conv}(3x3) o ext{Conv}(1x1) + x$$
Dense Connection:
$$x_{\ell} = H_\ell([x_0, x_1, ..., x_{\ell-1}])$$
---
## Advanced Theory & Extensions
### Pre-Activation ResNet
BatchNorm before convolution.
### ResNeXt
Grouped convolutions.
### Wide ResNet
Increased width.
---
## Computational Considerations
Forward: O(H·W·C·K).
Skip addition: O(H·W·C).
Memory: O(layers·H·W·C).
---
## Practical Implementation Strategies
### Block Design
Optimal bottleneck ratios.
### Shortcut Projection
Dimension matching.
### Initialization
He init for residuals.
---
## Benchmark Datasets & Evaluation
ImageNet: ResNet-50 baseline.
CIFAR-10: Comparison suite.
Object detection: Backbone validation.
---
## Key Challenges & Limitations
### Identity Mapping
Skip connection effectiveness.
### Training Dynamics
Residual path importance.
### Computational Cost
Parameter growth.
---
## Hyperparameter Tuning
Block depth: 18-152 layers.
Width multiplier: 1-2.
Bottleneck ratio: 4-16.
---
## Real-World Applications & Case Studies
ImageNet classification: ResNet-50 standard.
Object detection: Backbone network.
Semantic segmentation: Feature extraction.
---
## Integration with Other Methods
ResNets + normalization for stability; + attention for selective focus.
---
## Summary & Key Takeaways
Residual Networks enable very deep architectures.
Principles:
1. Skip connections: Gradient highways.
2. Residual blocks: Structured shortcuts.
3. Bottleneck: Efficient design.
4. Pre-activation: Improved ordering.
5. Depth scaling: Increased capacity.
---
## Appendix: Practical Labs
### Lab 1: Residual Block Forward
import numpy as np
def residual_block_forward(x, W1, W2, W3):
"""Forward pass through residual block"""
h = x @ W1
h = np.maximum(h, 0)
h = h @ W2
h = np.maximum(h, 0)
h = h @ W3
y = h + x
return y
np.random.seed(42)
x = np.random.randn(32, 256)
W1 = np.random.randn(256, 64) * 0.01
W2 = np.random.randn(64, 64) * 0.01
W3 = np.random.randn(64, 256) * 0.01
output = residual_block_forward(x, W1, W2, W3)
assert output.shape == x.shape, "Correct output shape"
print("✓ Residual block forward working")### Lab 2: Bottleneck Architecture
import numpy as np
def bottleneck_block(x, reduction_ratio=4):
"""Bottleneck block with dimension reduction"""
in_channels = x.shape[1]
bottleneck_channels = in_channels // reduction_ratio
h = x @ np.random.randn(in_channels, bottleneck_channels) * 0.01
h = np.maximum(h, 0)
h = h @ np.random.randn(bottleneck_channels, bottleneck_channels) * 0.01
h = np.maximum(h, 0)
h = h @ np.random.randn(bottleneck_channels, in_channels) * 0.01
y = h + x
return y
np.random.seed(42)
x = np.random.randn(32, 256)
output = bottleneck_block(x, reduction_ratio=4)
assert output.shape == x.shape, "Correct bottleneck shape"
print("✓ Bottleneck architecture working")### Lab 3: Dense Connections
import numpy as np
def dense_block(layer_inputs, new_features):
"""Dense block concatenates all previous activations"""
all_features = list(layer_inputs)
concatenated = np.concatenate(all_features, axis=1)
new_output = concatenated @ new_features
new_output = np.maximum(new_output, 0)
all_features.append(new_output)
return all_features
np.random.seed(42)
x1 = np.random.randn(32, 64)
x2 = np.random.randn(32, 64)
W_new = np.random.randn(128, 64) * 0.01
features = dense_block([x1, x2], W_new)
assert len(features) == 3, "Dense connections concatenated"
print("✓ Dense connections working")### Lab 4: Shortcut Projection
import numpy as np
def shortcut_projection(x, out_channels):
"""Project shortcut to match dimensions"""
if x.shape[1] == out_channels:
return x
else:
W = np.random.randn(x.shape[1], out_channels) * 0.01
return x @ W
np.random.seed(42)
x = np.random.randn(32, 64)
projected = shortcut_projection(x, out_channels=128)
assert projected.shape[1] == 128, "Correct projection shape"
print("✓ Shortcut projection working")---