He Initialization: Doubling the Variance for ReLU

Neural NetworksEasy+10 XP+10

He Initialization: Doubling the Variance for ReLU

Xavier initialization was derived assuming the activation is roughly symmetric around zero — true for tanh, false for ReLU. ReLU throws away every negative value, so about half the signal disappears at each layer, and weights sized by Xavier leave the activations shrinking as the network deepens.

He initialization compensates by doubling the variance. For a normal distribution, draw each weight with mean 0 and:

σ=2fan_in\sigma = \sqrt{\frac{2}{\text{fan\_in}}}σ=fan_in2​​

Task: write he_std(fan_in) returning that standard deviation, rounded to 4 decimal places.

  • fan_in is how many inputs the layer takes, and is a positive integer.
  • Return the standard deviation itself, not random weights.
  • Only fan_in appears — unlike Xavier, this formula doesn't balance the forward and backward passes against each other; it just keeps the forward signal alive.

The 2 is doing all the work, and it's there for a precise reason: ReLU zeroes out half the inputs, which halves the variance, so you start with twice as much to end up with the right amount. This is the default initializer for ReLU networks in every major framework, and it's a large part of why networks deeper than a few layers became trainable at all.

Examples

fan_in = 2
returns 1.0
fan_in = 784
returns 0.0505

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run