#229Gradient paths through L residual blocksEasy

Gradient paths through L residual blocks

Background

Expanding the stacked residual Jacobian ∏_{ℓ=1}^{L} (I + ∂f_ℓ) gives a sum over paths: at each block the gradient either goes through the sublayer or skips it. With L blocks that's:

2L paths2^L \text{ paths}

The single "skip every block" path is the pure identity I — the highway that never vanishes.

Problem statement

Implement gradient_path_count(L) returning the number of gradient paths through L residual blocks.

Input

  • Lint, number of residual blocks (L ≥ 0).

Output

Returns an int: 2**L.

Examples

Example 1

Input:  L = 1
Output: 2

(Through the sublayer, or skip it.)

Example 2 — GPT-2 small depth

Input:  L = 12
Output: 4096

Constraints

  • Return 2**L.
  • L = 01 (just the identity path).

Notes

  • The short paths (few layers traversed) dominate early training and all carry healthy gradient — which is why deep residual nets train from step one.
Python
Loading...

▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.

  • One block example
  • GPT-2 small depth reference
  • Zero blocks sample