Skip to content
mlmentorship

Residual connections

Trace addition through a residual block, including both gradient contributions, projection shortcuts, normalization and cancellation.

Published · 6 min read ·Core ·Foundation
Reading depth

Summary

A residual block adds a learned change to a shortcut: . The identity branch supplies a direct gradient contribution, not a guarantee about the combined gradient.

Take , and . Find the output and the two contributions to .

The mechanism

A residual block:

y = x + f(x)

The input goes to both branches. Addition joins their values in the forward pass and splits the upstream gradient in backward.

One input, two contributions

Addition copies the upstream gradient; each branch then applies its own derivative

Addition copies the upstream gradient into both branchesWith upstream 2.2, the identity branch returns 2.2. A residual branch with derivative 0.1 returns 0.22, so the input receives 2.42.Addition copies the upstreamsensitivity2.22.2x 12.22.2x 0.10.222.2 + 0.22 = 2.42
Derivation
Forward and backward paths through one residual addition The specified function is f(x)=0.1x, so its derivative is 0.1. Input two splits into f(2)=0.2 and unchanged x=2. Addition gives y=2.2 and loss y squared over two gives upstream gradient 2.2. Backward, the residual branch contributes 0.1 times 2.2, or 0.22. The identity contributes 2.2. Their sum is 2.42. FORWARD · SPLIT, TRANSFORM ONE BRANCH, THEN ADD input x = 2 f(x) = 0.1x f(2) = 0.2 identity branch x = 2 unchanged + y = 2 + 0.2 = 2.2 BACKWARD · THE ADD NODE SENDS g DOWN BOTH BRANCHES Loss L = y² / 2 g = ∂L/∂y = 2.2 through f g · Jf = 0.22 f'(x) = 0.1 direct route g · I = 2.2 no residual operation + ∂L/∂x = 2.2 + 0.22 = 2.42
Read it this way: the forward branches add to 2.2. Backward copies the upstream 2.2 to both branches. The residual branch scales it by 0.1; the shortcut leaves it unchanged. Their contributions add to 2.42.
Check: Why does the add node copy 2.2 into both branches? Why does the residual branch return only 0.22?
Read it this way: With upstream 2.2, the identity branch returns 2.2. A residual branch with derivative 0.1 returns 0.22, so the input receives 2.42.
Does the identity path receive only half of the upstream gradient?

References: Deep Residual Learning and Identity Mappings in Deep Residual Networks.

The loss is , giving upstream gradient . The shortcut contributes ; the residual branch contributes . Directly differentiating gives at too.

For vector inputs, the diagram’s row-gradient convention gives

One contribution crosses no residual operation. The sum still depends on .

Change the shortcut

1. Change output dimension

New task: map into three channels with .

Picture and change: replace the identity wire by , giving . The branches must produce matching shapes before addition.

Check: . Row upstream gradient returns , not the unchanged upstream vector.

Use when: channels or spatial resolution change across a block.

2. Move normalization outside the addition

New task: compare pre-norm and post-norm , with . LayerNorm has unit gain and zero offset.

Picture and change: add a common shift to . Pre-norm retains it through the shortcut; post-norm removes it by centering.

Check: the directional derivative along is for pre-norm and zero for post-norm.

Use when: reasoning about gradient paths across normalization boundaries.

The two contributions can cancel

Set . Then and . The total input gradient is zero even though the identity contribution exists. An identity branch does not prove a nonvanishing total gradient.

Try repeating the block

Does repeatedly applying prevent gradient explosion?

Check your answer

No. Each block has local gain . Across identical scalar blocks, the derivative is .

Why it works (intuitions)

The ResNet paper studied degradation in deeper plain networks, including worse training error. An additive block can represent identity by setting its residual function to zero. Veit et al. analyze paths of differing depths; this is not an ensemble of independently trained models.

Pre-norm vs. post-norm in transformers

Their Jacobians are for pre-norm and for post-norm. Matrix order matters. Neither expression guarantees bounded gradients through a whole network. See Xiong et al. for normalization placement under stated training and initialization assumptions.

Bottleneck blocks

A bottleneck reduces channels before the expensive spatial convolution, then expands them:

y = x + Conv1x1 ↓ → Conv3x3 → Conv1x1 ↑ (x)

Ignoring biases, its convolution parameters total . Savings depend on widths, spatial sizes and the comparison block, not a fixed factor or accuracy guarantee.

Where residuals show up

ArchitectureWhere
ResNet, ResNeXt, Wide ResNetEvery block
U-NetLong skips may concatenate; distinguish those from additive residual blocks
Transformer (encoder & decoder)Around attention and FFN sub-blocks
Diffusion U-NetsBoth within blocks and across encoder-decoder
MLP-Mixer, ConvNeXtBlock residuals

Common pitfalls

Some deep-network recipes scale residual contributions with depth; the scale must match their initialization and training assumptions. Addition, saved activations and projection shortcuts have memory or compute costs.