v9.1.1
Menu

Residual Block Gradient Flow Analysis

mediumFREEConvNextNeural Networks, BackpropagationAdvanced Architectures

Residual connections (skip connections) enable training of very deep networks by providing a direct gradient path.

A residual block computes: Y=X+F(X)Y = X + F(X)

For LL sequential residual layers where Yi=Yi−1+Fi(Yi−1)Y_i = Y_{i-1} + F_i(Y_{i-1}) with Y0=XY_0 = X:

Task: Calculate the gradient ∂YL∂X\frac{\partial Y_L}{\partial X} symbolically.

For simplicity, assume Fi(X)=Wi⋅XF_i(X) = W_i \cdot X (linear transformation) and all values are scalars.

Return a string representing the gradient formula, showing why residual connections prevent vanishing gradients.

Output format: write each block's factor as (dFi/dY{i-1} + 1) with the block numbers filled in (for example (dF3/dY2 + 1)), except block 1, whose input is XX itself: (dF1/dX + 1). List the factors from block LL down to block 1, separated by ** * **. For example, L=2L = 2 gives (dF2/dY1 + 1) * (dF1/dX + 1).

Example

Input
L=1
Output
(dF1/dX + 1)
Reasoning
  • L is the number of stacked residual blocks. The input XX enters block 1, and each block adds its own correction to what it receives: Yi=Yi−1+Fi(Yi−1)Y_i = Y_{i-1} + F_i(Y_{i-1}).
  • With L=1 there is a single block, Y1=X+F1(X)Y_1 = X + F_1(X). Differentiate each term with respect to XX: the skip path contributes ∂X∂X=1\frac{\partial X}{\partial X} = 1 and the residual branch contributes ∂F1∂X\frac{\partial F_1}{\partial X}.
  • Adding the two gives ∂Y1∂X=∂F1∂X+1\frac{\partial Y_1}{\partial X} = \frac{\partial F_1}{\partial X} + 1, which is the string (dF1/dX + 1).
  • Under the linear assumption F1(X)=W1XF_1(X) = W_1 X this factor equals W1+1W_1 + 1. Even if W1W_1 is tiny, say 0.010.01, the gradient is 1.011.01 rather than 0.010.01: the identity path keeps it close to 1 instead of letting it shrink.
  • Notice that only block 1 is differentiated with respect to XX directly. A deeper block receives Yi−1Y_{i-1}, so its factor is taken with respect to that, and by the chain rule ∂YL∂X\frac{\partial Y_L}{\partial X} is the product of one (1+⋅)(1 + \cdot) factor per block.

Constraints

  • LL (number of residual blocks): 1≤L≤51 \leq L \leq 5
  • All computations use scalar values for simplification

Learn this in

More from Advanced Architectures

solution.py

Test results

Sign in to run your code against the test cases.