Residual connections: learn the correction
残差连接
Fit a residual correction exactly, compare matched initial functions, check projection shapes and state a conditional sensitivity bound.
residual-connectionsLearn a correction to an existing signal
Suppose a desired mapping is almost the identity: an input x should mostly pass through, with a small correction. A residual block makes that baseline explicit. One path carries x; another computes a correction F(x). The output adds them. A zero correction then preserves the input without requiring the learned branch to reconstruct it.
He and colleagues’ CVPR 2016 paper, Sections 3.1–3.2 formulates the target correction as H(x)−x and discusses projection shortcuts when dimensions change. Their later Identity Mappings paper, Section 2 distinguishes the skip mapping from the activation applied after addition. That distinction matters: ReLU(x+0) is not the identity for negative x.
Addition needs compatible representations
Let x have d coordinates and F(x) have k. A linear skip map S must map d coordinates to k. Where derivatives exist:
The identity case requires matching dimensions. A learned projection or a specified downsampling transform can align shapes, but it is no longer an identity path. The source paper’s standard block includes a nonlinearity after addition; the formula above denotes the additive merge itself, or a unit whose post-addition map is identity.
The residual branch is not necessarily the prediction error against a dataset label. Its target is the difference between a desired intermediate mapping and the chosen skip mapping. Both are learned representations inside a network. This differs from fitting a new model to label residuals in boosting, even though both use the word residual.
Fit a correction that can be checked exactly
Take four inputs −2,−1,1,2 and targets x+x²/4. Use Hₐ(x)=x+ax², with a fixed identity skip and one learned quadratic coefficient. This is a transparent basis-function model, not a convolutional ResNet. The mean half squared loss is:
To derive the coefficient, average x⁴ over the four inputs: the result is 17/2. Each step halves the coefficient error. Initial loss is 17/64; after five updates a=31/128 and loss is 17/65536. The program calculates sample losses and gradients directly rather than using the closed form as its update.
For a fairer comparison, allow the skip coefficient to be learned too: Hₐ,ᵦ(x)=bx+ax². If b starts at one, its gradient remains zero on this symmetric dataset, so the entire trajectory matches the fixed-skip case. Starting b at zero changes the initial function and produces a different trajectory. The latter difference alone cannot prove a universal optimization advantage of residual connections.
A local stability statement needs an assumption
For one same-width additive block, suppose the spectral norm of JF at the point is bounded by ρ, with ρ strictly below one. Then for every perturbation u:
The upper bound is the triangle inequality; the lower bound follows from the reverse triangle inequality. This controls local sensitivity only under the stated derivative bound. A branch can have zero output at a point and still have a large derivative there. A long chain of blocks also requires a separate analysis; multiplying many factors can still shrink or enlarge signals.
Trace the fitted correction and check a projection
The complete Python 3.12 program uses exact fractions. fit computes a gradient from the four observations at every step. merge separately checks identity or explicit projection dimensions and returns a new vector. Input sizes and rational magnitudes are bounded for a small executable example.
from fractions import Fraction as F
def rational(x):
if type(x) not in (int,F) or abs(x)>1000:raise ValueError('bounded rational required')
return F(x)
def merge(x, branch, projection=None):
x=tuple(rational(v) for v in x);branch=tuple(rational(v) for v in branch)
if not 1<=len(x)<=8 or not 1<=len(branch)<=8:raise ValueError('short nonempty vectors required')
if projection is None:
if len(x)!=len(branch):raise ValueError('identity skip requires matching dimensions')
skip=x
else:
rows=[tuple(rational(v) for v in row) for row in projection]
if len(rows)!=len(branch) or any(len(row)!=len(x) for row in rows):raise ValueError('projection must map input to branch shape')
skip=tuple(sum((a*b for a,b in zip(row,x)),F(0)) for row in rows)
return tuple(a+b for a,b in zip(skip,branch))
def loss_grad(a,b):
a,b=rational(a),rational(b);loss=da=db=F(0)
for x in (-2,-1,1,2):
residual=b*x+a*x*x-(x+F(1,4)*x*x)
loss+=residual**2/8;da+=residual*x*x/4;db+=residual*x/4
return loss,da,db
def fit(steps=5,b0=1,learn_skip=False):
if type(steps) is not int or not 0<=steps<=32 or type(learn_skip) is not bool:raise ValueError('bounded step count and Boolean policy required')
a,b=F(0),rational(b0);trace=[(0,a,b,loss_grad(a,b)[0])]
for step in range(1,steps+1):
_,da,db=loss_grad(a,b);a-=da/17
if learn_skip:b-=db/17
trace.append((step,a,b,loss_grad(a,b)[0]))
return trace
if __name__=='__main__':
for row in fit():print('step,a,b,loss:',tuple(str(v) for v in row))
matched=fit(b0=1,learn_skip=True);zero=fit(b0=0,learn_skip=True)
print('matched learned-skip trajectory equals fixed identity:',matched==fit())
print('zero-start learned-skip final loss:',f'{float(zero[-1][3]):.9f}')
print('projected merge:',tuple(str(v) for v in merge((2,-1),(F(1,2),1,F(-1,2)),((1,0),(0,1),(1,1)))))
Actual output:
step,a,b,loss: ('0', '0', '1', '17/64')
step,a,b,loss: ('1', '1/8', '1', '17/256')
step,a,b,loss: ('2', '3/16', '1', '17/1024')
step,a,b,loss: ('3', '7/32', '1', '17/4096')
step,a,b,loss: ('4', '15/64', '1', '17/16384')
step,a,b,loss: ('5', '31/128', '1', '17/65536')
matched learned-skip trajectory equals fixed identity: True
zero-start learned-skip final loss: 0.255001556
projected merge: ('5/2', '0', '1/2')
The projection example maps (2,−1) to (2,−1,1) through S, then adds (1/2,1,−1/2) to get (5/2,0,1/2). A two-coordinate identity skip could not be added to that three-coordinate branch without a mapping.
Tests compare all six training states with the analytic trajectory, check both gradients by finite differences, and verify the matched-initial-function comparison. They also check the projected result, zero-branch identity and shape failures. With n scalar observations, T fitting steps cost O(Tn); this example stores O(T) trace entries. A dense d-to-k projection costs O(kd), whereas an identity skip has no learned projection parameters and only the merge’s O(d) addition cost.
A shortcut does not certify the whole model
Zeroing a branch output is not always equivalent to safely zero-initializing every parameter of a nonlinear branch. Such an initialization can block useful gradients inside it. The branch’s activation and parameterization must be checked separately.
A post-addition activation, normalization, projection or gate changes the simple identity argument. Even an ungated additive identity path does not prevent a learned branch derivative from canceling it. The local bound above assumes a small derivative; it does not follow from the diagram alone.
This toy fit has one quadratic coefficient, exact arithmetic and no stochastic batches. It demonstrates a correction parameterization and an honest comparison, not ResNet training speed, image-recognition performance or an optimal depth. Architecture experiments need matched data, initial functions where relevant, parameter budgets and training settings.
What exactly passes through?
- For an additive identity block with F identically zero, what is H(x)?
Answer 1: x, before any post-addition operation.
- What coefficient and loss follow five toy fitting updates?
Answer 2: a=31/128 and loss=17/65536.
- Why does the learned-skip model initialized at b=1 match the fixed-skip trajectory here?
Answer 3: Symmetry makes its b gradient zero while both models start with the same function and a value.
- Does F(x)=0 at one point imply JF(x)=0 there?
Answer 4: No. For example, F(x)=10x is zero at zero but has derivative ten.
A parameterization and a propagation analysis answer different questions
The original ResNet paper supplies the correction formulation and projection option. The later identity-mapping paper makes propagation assumptions explicit. Our basis-function fit, matched comparison, projection arithmetic and local norm bound are original teaching calculations. We did not reproduce either paper’s image experiments or copy its architecture code.
Continue with skip connections and gradient flow for sums of path derivatives and cancellation, and initialization before deciding how to start a residual branch.
Make a prediction, then check
In an additive identity block, what happens if the residual branch is identically zero?
Learning objectives
- Separate the skip mapping from a learned residual branch.
- Evaluate fair comparisons and shape/derivative assumptions.
Prerequisites & connections
Explore related knowledge →References
- Deep Residual Learning for Image Recognition
Kaiming He; Xiangyu Zhang; Shaoqing Ren; Jian Sun · 2016 - Identity Mappings in Deep Residual Networks — arXiv1603.05027v3
Kaiming He; Xiangyu Zhang; Shaoqing Ren; Jian Sun · 2016
Version1 · Editorial status: Recorded technical checks completed
View technical check record (AI)
Codex · 2026-09-06 · Codex single-agent authored technical review: cited primary-source scope, explicit assumptions, original calculations and counterexamples, runnable tests and bilingual review; not independent human or second-agent review.
- Single-agent authored technical review: bounded primary-source locations, assumptions, original derivations, exact/literal references and matching bilingual equations/examples/exercises. Not independent human or second-agent review.
- Four focused CPython3.12.4 tests passed per chapter. Normalization axes and groups, matched residual fits and reverse graph paths are checked against literal references, analytic identities and finite differences. Two examples use NumPy1.26.4; two use the standard library.
- Promoted the full prose, equations, code and exercises. Tests read formal MDX only; removed temporary fallbacks/draft skips and updated review assertion to checked. Algorithm and independent references are unchanged; final record binds both locale hashes.
- Publication navigation points published topics to complete articles and removes corresponding pending wording, retaining directory entries for unwritten targets. Principles, equations, programs and exercises are unchanged; final article SHA values rebound.
Source locations checked:
- Deep Residual Learning for Image Recognition · CVPR2016 Sections3.1–3.2, correction H-x, addition Eq1 and projection Eq2; zero branch vs post-addition activation distinguished.
- Identity Mappings in Deep Residual Networks — arXiv1603.05027v3 · Fixedv3 Section2 Eqs1–5: skip map h and post-addition f separated, identity assumptions required; no empirical reproduction.
Related topics