3.4.9 · D4Convolutional Neural Networks

Exercises — ResNet and skip connections

2,446 words11 min readBack to topic

These problems climb a ladder: first you just recognize the pieces of a residual block, then you apply the forward and backward rules, then you analyze why they work, then you synthesize whole architectures, and finally you reach mastery — the subtle edge cases most people trip over. Every problem has a full solution hidden inside a collapsible callout, so cover the answer, try it yourself, then reveal.

New to the whole idea? Start from the parent note: ResNet and skip connections.

Figure — ResNet and skip connections

Level 1 — Recognition

Goal: name the parts. No arithmetic yet.

Recall Solution 1.1
  • — the input to the block, and also the thing carried untouched along the skip connection (the highway).
  • — the residual mapping: the small correction learned by the stacked conv layers (the detour).
  • — the identity shortcut addition: it adds the untouched input back so the layers only have to learn the difference , not the whole target .
Recall Solution 1.2

The addition happens before the final ReLU: ... → BN → Add → ReLU. Why: we want the nonlinearity to act on the combined signal , not on alone. If ReLU came first it would clip the residual to be , so the block could never produce a negative correction — it could only ever push values up, never down. Adding first, then ReLU, keeps the correction free to go either way.

Recall Solution 1.3
  • Basic Block (two convs): ResNet-18, ResNet-34.
  • Bottleneck Block (): ResNet-50, ResNet-101, ResNet-152. Rule of thumb: 50 and deeper use bottlenecks, because the "squeeze then expand" trick keeps the conv cheap at high depth.

Level 2 — Application

Goal: push numbers and tensors through the rules.

Recall Solution 2.1

A conv with stride 1 and padding 1 preserves spatial size, and "64 filters" fixes 64 output channels. So after each conv the shape stays . Thus is , identical to , so they add element-wise and is .

Recall Solution 2.2

Inner pre-activation: . ReLU: (positive, passes through). Detour: . Output: .

Recall Solution 2.3

Inner: ; ReLU; detour . So . The block collapsed to the identity map just by driving weights to zero — this is exactly why residual learning is easy: "do nothing" costs zero weights, whereas a plain layer must learn the identity from scratch.

Recall Solution 2.4

The addition needs both tensors to match exactly. So is a conv with 128 filters (to reach 128 channels) and stride 2 (to halve spatially). Result of : , matching . ✓


Level 3 — Analysis

Goal: explain the mechanism, especially the backward pass.

Recall Solution 3.1

Chain rule: . Since : . So The bare term (from the "" / ) is the highway: even if the detour's Jacobian , this term survives, so early layers still receive a gradient of size . That is what kills vanishing gradients.

Figure — ResNet and skip connections
Recall Solution 3.2

Plain: — the signal has nearly vanished. Residual: — the signal is amplified, easily flowing back. Ratio residual/plain . The highway makes the deep gradient ~ larger here.

Recall Solution 3.3

Overfitting means the model fits the training data too well and only hurts test accuracy — but here the deeper net is worse on the training set itself, so it isn't even memorizing well. That means the extra layers can't be optimized to at least copy the shallower net (which they could do by learning identity mappings), so the failure is in optimization / trainability, not generalization — precisely the degradation problem residuals fix.


Level 4 — Synthesis

Goal: assemble and cost whole structures.

Recall Solution 4.1

One conv: . Two such convs: weights (plus BN scale/shift, ignored here).

Recall Solution 4.2
  • reduce: .
  • middle: .
  • expand: .
  • Bottleneck total: .
  • Naive : . The bottleneck is cheaper for the same in/out channels. That is why deep ResNets use it.
Recall Solution 4.3

Conv layers inside blocks: . Add the stem conv () and the final FC layer (): . That is where "ResNet-34" gets its number — counted layers with weights. (Poolings and additions carry no weights, so they aren't counted.)


Level 5 — Mastery

Goal: the subtle edge cases and limits.

Recall Solution 5.1

With : exactly — identity, with no dependence on getting weights precisely right, since zero weights already give . A plain block computes with no ; to be the identity it must satisfy for every , which for nonlinear (ReLU) layers is a specific, hard function to learn — not achievable by simply zeroing weights (zeroing gives , not ). Hence extra plain layers can hurt, extra residual layers can at worst do nothing.

Recall Solution 5.2

ReLU, so the detour is dead: . Then . Gradient: . With ReLU input negative, , so . Punchline: even when the entire detour is dead (a scenario that would zero-out a plain block's gradient), the highway still delivers gradient . This is the vanishing-gradient cure in a single number.

Recall Solution 5.3

With every , each block is identity, so the composition of blocks is identity: the network passes the input straight through at init, regardless of . This is ideal because:

  1. A very deep net begins in a well-behaved state (no exploding/vanishing forward signal),
  2. Optimization then only has to carve small nonzero where useful — a gentle departure from identity — instead of taming a wildly transformed signal. So depth becomes "safe": adding blocks starts as a no-op and can only help.
Recall Solution 5.4

(the conv's Jacobian replaces the identity ). So the highway is no longer the pristine ""; it becomes "". The gradient floor is now set by instead of exactly — still a non-nonlinear, single-layer shortcut (no ReLU in the way), so it's far healthier than a plain deep chain, but it is not a perfect identity highway. That's why identity shortcuts (no projection) are preferred whenever shapes already match, and projections are used only where they must be.


Recall Self-test cloze recap

The block computes , where the residual is only the correction ::: correct, because pushing to zero yields the identity for free. The backward highway term that survives even when is ::: the (identity) term, giving . Bottleneck order is ::: reduce → expand. Projection shortcut is used when ::: spatial size or channel count of differs from , via a conv .