advanced32 min studyLesson 51 in Engineering

Residual networks and normalization

Trace identity paths, normalization axes, and activation scales through a deep computation graph.

On this page

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.

y=x+F(x),Jy=I+JF(x)y=x+F(x),\qquad J_y=I+J_F(x)
This Jacobian formula assumes equal input and output dimensions and a differentiable branch at the point being considered. It describes one block, not a guarantee for a product of many blocks.↔ Scroll wide equations horizontally.

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.

LN⁡(x)i=γixi−μv+ϵ+βi,μ=1d∑ixi,v=1d∑i(xi−μ)2\operatorname{LN}(x)_i=\gamma_i\frac{x_i-\mu}{\sqrt{v+\epsilon}}+\beta_i,\quad \mu=\frac1d\sum_i x_i,\quad v=\frac1d\sum_i(x_i-\mu)^2
These statistics use the population-style divisor d across one token's features. Gain and bias are learned parameters shared across tokens, not fresh parameters for each position.↔ Scroll wide equations horizontally.

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.

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.

  1. The branch produces F(2)=0.2×4=0.8, so the output is y=2.8.

  2. The branch derivative is F′(x)=0.4x, giving 0.8 at x=2. The complete derivative is 1+0.8=1.8.

  3. 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 result

The identity path adds a known derivative contribution, but the total derivative is 1.8 rather than exactly one.

Explore the model

Interactive lab

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.

Inspect a nonlinear branchOne inspectable sigmoid unit, σ(wx − 0.5). Playback traces the computation graph. There is no training, attention mechanism or hidden deep network. Current readings: Product · wx: 1.2; Logit · wx − 0.5: 0.7; Sigmoid output: 0.6682. All parameters are adjustable with the labeled controls below.One input. One weight. One sigmoid.x1.20× w1.20+ b0.70σ0.668output0.668w = 1.00b = −0.50σ(z) = 1 / (1 + exp(−z))
Computation graphSignal trace
Product · wx
1.2
Logit · wx − 0.5
0.7
Sigmoid output
0.6682
t = 0 s

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.

Check your understanding

For a tensor shaped batch × tokens × features, which operation is conventional token-wise LayerNorm?

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.

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.