Residual networks and normalization
Trace identity paths, normalization axes, and activation scales through a deep computation graph.
On this page
The question for today
What you’ll understand
- Derive the gradient through an additive residual block.
- Specify exactly which axes a normalization operation reduces.
- Separate optimization benefits from memory and inference costs.
Making a network deeper changes both its representational capacity and the numerical path by which information and gradients travel. A residual block learns a correction to a state that is already available. Normalization controls particular statistics of that state. These mechanisms often appear together, but they solve different problems and their placement matters. Start with a tensor and an explicit computation graph rather than a diagram whose arrows hide incompatible shapes.
Identity paths carry an explicit derivative
Let a block receive a row vector of width d and return the same width. Adding its learned branch to its input gives a direct identity contribution to the Jacobian. If the branch initially produces a small correction, the block initially behaves close to identity. This can make adding depth easier to optimize. It does not force the full Jacobian to have well behaved singular values: the learned derivative can still amplify, cancel, or rotate incoming gradients.
Addition requires matching shapes. A stage that changes channels or downsamples spatial dimensions needs a projection or another specified skip transformation. That projection has its own parameters and derivative; it is no longer a literal identity. Also distinguish an unmodified identity path from a post-addition nonlinearity. A ReLU applied after addition can zero a component's derivative. Trace the actual ordering when comparing residual formulations rather than treating every skip connection as mathematically identical.
Normalization is an axis contract
For tokens shaped batch by sequence by features, a conventional LayerNorm computes a mean and variance over the feature axis separately for each token. It does not mix examples or average over sequence positions. Learned feature-wise gain and bias then restore flexibility. The small positive epsilon prevents division by zero and influences behavior when variance is tiny. Specify the axes because a numerically valid reduction over the wrong dimension can silently make tokens depend on their neighbors.
BatchNorm commonly uses batch and spatial axes to estimate a separate statistic per channel and usually uses running estimates for evaluation. LayerNorm uses each example's current features in both modes. This difference matters for small batches, distributed synchronization, variable sequence lengths, and deployment. Neither operation makes all activations independent, nor does zero mean imply good conditioning everywhere. Pre-normalization, where a residual branch consumes normalized input, preserves a direct identity path around that branch; post-normalization places another derivative on that path.
Measure the whole block
Residual addition is inexpensive relative to large matrix multiplications, but keeping activations for backward propagation costs memory. Normalization also reads and reduces tensors and can become visible in small-batch inference. Kernel fusion can reduce memory traffic without changing the architectural equation, subject to numerical tolerances. Checkpointing trades recomputation for activation storage; it does not remove parameters or optimizer state. Separate these memory categories before attributing a training limit to depth alone.
Compare alternatives using a fixed task and declared training budget. Record initialization, residual scaling, precision, gradient clipping, and normalization placement. A model that diverges under one learning rate has not automatically lost an architectural comparison if another model received more tuning. Inspect activation and gradient statistics across depth, then test whether an intervention repairs the diagnosed problem. A claim about optimization should be supported by training behavior as well as final validation accuracy.
Work it through
Differentiate one scalar residual block
Let F(x)=0.2x² and y=x+F(x). At x=2, compute the forward output and the gradient when the loss derivative with respect to y is 3.
The branch produces F(2)=0.2×4=0.8, so the output is y=2.8.
The branch derivative is F′(x)=0.4x, giving 0.8 at x=2. The complete derivative is 1+0.8=1.8.
The chain rule gives the input loss derivative 3×1.8=5.4. The identity contribution is 3 and the learned-branch contribution is 2.4.
The identity path adds a known derivative contribution, but the total derivative is 1.8 rather than exactly one.
The living notebook
Explore the model
Interactive lab · network
Inspect a nonlinear branch
The lab computes exactly one sigmoid σ(wx−0.5). Adjust input x and weight w to inspect saturation as a possible branch behavior. It does not include a residual addition, LayerNorm, or a trainable deep network; add the identity term analytically in the worked example.
- Product · wx
- 1.2
- Logit · wx − 0.5
- 0.7
- Sigmoid output
- 0.6682
One inspectable sigmoid unit, σ(wx − 0.5). Playback traces the computation graph. There is no training, attention mechanism or hidden deep network. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.
Look forA saturated nonlinear branch can have a small derivative while a separately specified identity path still contributes to the total derivative.
Pause & reason
Check your understanding
Your turn
Try it yourself
A token has features [1,3]. Compute LayerNorm with gain one, bias zero, and epsilon zero for this nonzero-variance example. What changes if epsilon is positive?
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
Compute the feature mean and the variance using divisor two before normalizing.
Compare with the solution
The mean is 2 and variance is [(−1)²+1²]/2=1, so the result is [−1,1]. Positive epsilon changes the denominator to √(1+epsilon), reducing both magnitudes slightly. Epsilon zero is safe for this example but would be undefined for a constant token, so actual implementations retain a positive value.
Keep the idea
Take it with you
- An identity skip adds I to a branch Jacobian without guaranteeing stable deep products.
- Normalization axes determine which values influence one another.
- Report normalization placement, training choices, and activation memory together.
Read further
Original sources and deeper explanations to follow at your own pace.
Optional progress, saved in this browser when available. Revisit any lesson at any time.
Lesson not marked complete.