Residual and dense connections
Why adding a block's input back to its output let networks reach hundreds of layers, how DenseNet concatenates instead of adding, and what each costs in memory.
What runs in CodeSplash
- DNN007Residual network (ResNet) Partly runs in CodeSplash (Experimental)
- DNN008DenseNet Explained here
- Image classifier (pretrained ResNet-50 or DINOv2) Experimental
- Image embeddings, map and neighbors (DINOv2 or ResNet-50) Experimental
- FT-Transformer Experimental
You will be able to
- Write a residual block and explain why it can start out as the identity
- Explain how the shortcut carries gradients to early layers
- Contrast DenseNet's concatenation with ResNet's addition, including memory
- Say where CodeSplash uses residual networks
Read first
Deeper got worse, until shortcuts
ReadStacking more plain layers eventually made networks worse even on their training data: not overfitting, but an optimization failure. A deeper network could in principle copy a shallower one and pass the rest through unchanged, yet training couldn't find that.
A residual connectionResidual connectionAdds a block's input to its output, y = x + F(x), so the block learns a correction and gradients reach early layers. builds the copy in. The block outputs its input plus a correction:
If the best thing a block can do is nothing, F only has to output zero, which small weights already nearly do. Residual networks (ResNetResNet-50A 50-layer convolutional network whose blocks add their input back to their output, which lets very deep networks train.) stack dozens or hundreds of such blocks.
A block that does nothing
ReadIn the program below, the first row's branch adds a small correction to its second value, giving 1 and 2.40. For the second row the ReLU branch outputs zeros, so the block returns its input, −1 and 0.50, unchanged.
The shortcut also helps gradients: the derivative of y with respect to x is the identity plus F's derivative, so the loss reaches the earliest layers even when F's slope is small. That sidesteps the vanishing gradientVanishing gradientRepeated multiplication by the same weights shrinks a signal's effect on the loss toward zero, so far-back steps stop teaching the network..
import numpy as np
x = np.array([[1., 2.], [-1., .5]])
W = np.array([[.1, .2], [-.1, .1]])
y = x + np.maximum(0, x @ W) # residual block: input + ReLU branch
print(y)
h1 = np.maximum(0, x @ np.array([[.2], [.3]]))
h2 = np.maximum(0, np.concatenate([x, h1], 1) @ np.array([[.1], [.2], [.4]]))
print(np.concatenate([x, h1, h2], 1)) # dense block: every output keptx <- matrix(c(1, 2, -1, .5), 2, byrow = TRUE)
W <- matrix(c(.1, .2, -.1, .1), 2, byrow = TRUE)
print(x + pmax(x %*% W, 0)) # residual block
h1 <- pmax(x %*% matrix(c(.2, .3)), 0)
h2 <- pmax(cbind(x, h1) %*% matrix(c(.1, .2, .4)), 0)
print(cbind(x, h1, h2)) # dense blockConcatenate instead of add
ReadDense connectivityDense connectivityEach layer in a block receives the concatenated outputs of every earlier layer, so features are reused instead of re-learned. (DenseNet) gives each layer in a block every earlier layer's output, concatenated as channels. Features are reused rather than recomputed, so each layer can be thin.
In the second half of the program, two inputs and two one-unit layers end as a row of four values kept side by side: x, then h1 (0.80 for the first row), then h2 (0.82). The price is memory: the channel count grows with every layer, and every concatenated map stays in memory for the backward pass. Transition layers with 1 × 1 convolutions and pooling shrink it between blocks.
Which grows faster?
PredictPredict first
A block starts with 64 channels and has 6 layers. Each residual layer outputs 64 channels and adds them; each DenseNet layer outputs 32 new channels and concatenates them. Whose output has more channels?
Reveal
The dense block's: 64 + 6 × 32 = 256 channels. Addition keeps the residual block at 64 however deep it gets.
Residual networks in CodeSplash
ReadThe image classifier and the image embedder can use a pretrained ResNet-50, which turns each photo into 2,048 features; the classifier can also fine-tune its last blocks. The FT-Transformer learner adds a residual connection around every attention and feed-forward sublayer.
Method guide · learn.image_classifierImage classifier (pretrained ResNet-50 or DINOv2)Sort images into classes by fine-tuning a pretrained vision network on this computer.What runs here
Read| Entry | In CodeSplash |
|---|---|
| Residual network (DNN007) | Partly runs in CodeSplash: a pretrained ResNet-50 in the image classifier and image embedder; residual connections in the FT-Transformer. The CNN and torch MLP have no residual blocks |
| DenseNet (DNN008) | Explained here |
Check: residual and dense connections
CheckpointCheck yourself
A residual block's branch F outputs all zeros. What does the block output?
Show the answer
Answer: a
y = x + F(x) = x. That makes "do nothing" easy to learn, which is why deep residual stacks train.
Check yourself
Why do gradients reach early layers in a deep residual network?
Show the answer
Answer: a
∂y/∂x = I + ∂F/∂x. Even if every ∂F/∂x is small, the identity term carries the gradient through.
Check yourself
A residual block's branch halves the image size and doubles the channels. What must the shortcut do?
Show the answer
Answer: a
Addition needs matching shapes. ResNet projects the shortcut whenever a block changes resolution or width.
Check yourself
How does a DenseNet layer combine earlier layers' outputs?
Show the answer
Answer: a
Concatenation keeps every earlier feature map available. Addition, as in ResNet, merges them into one.
Check yourself
Why does a DenseNet often need more training memory than a ResNet with fewer parameters?
Show the answer
Answer: a
Parameters can be few because layers are thin, but stored activations grow with the concatenated channels.
Recap
Recap- Residual blocks output x + F(x): the identity is easy, gradients flow back, and depth stops hurting.
- Shapes must match for the add; a projection fixes it when a block changes size.
- DenseNet concatenates every earlier output: thin layers, reused features, growing memory.
- In CodeSplash, ResNet-50 is a pretrained backbone of the image classifier and embedder, and the FT-Transformer uses residual connections; DenseNet is explained here.
Concepts in this lesson