LLM + RoboticsUnit 2 · Deep learning explosion · Day 10

Identity Mappings in Deep Residual Networks

Keep the shortcut path completely clean, with nothing on it but addition, and signals travel between any two layers in both directions. That lets a 1001-layer ResNet train easily.

AuthorsKaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun (Microsoft Research)
Year / venue2016 · ECCV
Core ideaPre-activation residual units
Study time2–4 h

Original: arXiv:1603.05027 · Code, notebook and exercises: 30u30 Day 10

Start here (no maths needed)

ResNet works even better when nothing at all gets in the way of its shortcuts, and this lets networks with 1,000 layers learn.

Think of it like this

Think of a water slide with a straight express lane next to the twisty part. If the express lane is completely clear, you zoom from the top to the bottom. Put a gate or a speed bump on it, even a small one, and over a very long slide those slow-downs add up until hardly anyone gets through.

How it works

  1. Each block has two paths: a shortcut, and a path with the layers that do the work.
  2. The authors tried putting things on the shortcut: shrinking the signal, gates, extra layers, randomly switching it off. Every one made learning worse.
  3. They also moved the "clean-up" steps from after the two paths join to inside the work path.
  4. Now the shortcut is a clear express lane from the top of the network to the bottom.
  5. Learning signals travel easily through all the layers, so even 1,000-layer networks train well.

Why robots care

Engineers often add new sensors to a robot that already works, such as a touch sensor on its gripper. A safe way is to add the new part like a residual block that starts out adding nothing. On day one the robot behaves exactly as before. Then it slowly learns to use the new sensor.

Try it

Words to know

Shortcut
The path in a ResNet block that skips the layers.
Gradient
The learning signal that tells each layer how to change.
Activation
A simple step after a layer that decides which signals pass on.
Pre-activation
Doing that clean-up step before a layer instead of after it.

Going deeper: everything below is the full version, with some maths. On a first read, skim the headings and the "Why it matters for robotics" box, and come back for the rest later.

1. Summary

TL;DR

The follow-up to ResNet asks why residual nets train so well, and answers with a few lines of algebra. If the shortcut is a pure identity and nothing is applied after the addition, any deep layer's features equal any shallow layer's features plus a sum of residuals. The gradient then has a term that skips every weight layer. The paper tests this by spoiling the shortcut with scaling, gating, 1×1 convs and dropout. Every variant trains worse. It then moves BN and ReLU before the weight layers ("pre-activation"), so the path after the addition is clean too. A 1001-layer pre-activation ResNet gets 4.92% error on CIFAR-10, against 7.61% for the original design at the same depth.

2. The core idea

Write a residual unit in general form, with a shortcut function h and an after-addition function f:

y_l     = h(x_l) + F(x_l, W_l)
x_{l+1} = f(y_l)

# if h and f are both identity:
x_L = x_l + Σ_{i=l}^{L-1} F(x_i, W_i)                          (forward)
∂E/∂x_l = ∂E/∂x_L · ( 1 + ∂/∂x_l Σ_{i=l}^{L-1} F(x_i, W_i) )   (backward)

Now break the shortcut with a scale, h(x) = λ·x. The "1" becomes a product:

∂E/∂x_l = ∂E/∂x_L · ( Π_{i=l}^{L-1} λ_i  +  ∂/∂x_l Σ F̂(x_i, W_i) )

Over many units, λ > 1 makes that product explode and λ < 1 makes it vanish. Either way the direct path is lost, and the signal has to squeeze through the weight layers.

3. Deep dive

Watching the direct path die

Interactive: the shortcut term Πλ across units

ResNet-110 has 54 residual units. The plot shows log₁₀ of the direct gradient factor that reaches unit l from the top of the network when every shortcut is scaled by λ. λ = 1 is the identity.

Factor at unit 1
–
Units until < 1%
–

At λ = 0.5, as in the paper's constant-scaling test, the direct term is about 10⁻¹⁶ by the bottom of the net. Even λ = 0.9 shrinks it to below 1%. Only λ = 1 keeps it exactly 1 at every depth. Dropout on the shortcut acts like a random λ with mean 0.5, which explains why it failed too.

Moving the activation: post versus pre

The original unit ends with ReLU after the addition, so f is not the identity. Negative values get clipped on the shortcut path, again and again. The paper tested five orderings of the same parts:

The trick is to see the after-addition activation as the pre-activation of the next unit's branch. Apply it only to the branch, and the shortcut stays untouched from start to end. The paper credits two effects: easier optimisation (very clear at 1001 layers) and better regularisation, because every weight layer now receives normalised input.

Limitations, and what came after

4. Why it matters for robotics

5. How to study it

  1. Read Section 2 carefully and derive Eqns 4 and 5 yourself (about 30 min). Then read Table 1 (shortcut variants) and Table 2 (activation orderings).
  2. In the 30u30 Day 10 folder, read implementation.py and compare its pre-activation block with the post-activation block from Day 9. Note the special handling of the first and last units.
  3. Run train_minimal.py, then work through notebook.ipynb.
  4. Exercises: pre versus post activation, ablating where BN and ReLU go, ultra-deep networks, a 1001-layer ResNet and transfer learning. Log per-layer gradient norms in each.
  5. Stretch: put a 0.5 scale on the shortcut of a 50-unit toy ResNet and confirm the training slowdown.

6. Self-check

What two conditions give a clean information path?

The shortcut h must be the identity, and the function after the addition f must be the identity. Then x_L = x_l + ΣF.

Why does the "1" in the backward equation matter?

It sends the gradient from any deep unit straight to any shallow one without passing through weight layers, so it can't vanish however small the weights are.

A 1×1 conv shortcut can learn the identity. Why did it train worse?

Being able to represent the identity doesn't mean the optimiser will find it. Over 54 units, products of shortcut Jacobians still hamper propagation, and training error rose. It's an optimisation problem, not a capacity one.

What is "full pre-activation"?

Each branch runs BN → ReLU → conv → BN → ReLU → conv, then adds to the untouched shortcut. Nothing is applied after the addition.

Why does ReLU before the addition hurt?

The residual becomes non-negative, so each unit can only increase the features. A residual should be able to push either way.

7. Further reading