Intuition What this page is
The parent note gave you two formulas: He (Var ( w ) = 2/ n in ) for ReLU and Xavier (Var ( w ) = 2/ ( n in + n o u t ) ) for tanh/sigmoid. Here we drill through every kind of situation a real network throws at you: normal layers, square layers, the first and last layer (where fan-in ≠ fan-out matters a lot), the degenerate one-input case, the "wrong constant" disasters, a real-world sizing problem, and an exam twist. First guess, then verify.
Before anything, two reminders of the vocabulary (built in the parent, repeated so you never guess):
Definition Bias initialization (why every example uses zero biases)
A neuron computes z = ∑ i w i x i + b , where b is the bias — a constant added after the weighted sum. In every example on this page we set b = 0 .
Why zero? The bias has no fan-in to sum over, so it cannot explode or vanish with depth; and a nonzero constant would add an offset to z that shifts the whole variance argument off its zero-mean assumption. Starting at b = 0 keeps pre-activations zero-mean so the He/Xavier variance derivation holds exactly. (Training then moves biases away from zero on its own.)
Every weight-init question is one of the nine cells below. First skim the flowchart — it is the "decision tree" you walk to land on the right cell; then the table names each cell and its example.
use Xavier 2 over sum of fans
Cell
Situation
What makes it tricky
Example
A
ReLU layer, n in = n o u t
He uses only fan-in → output size is a decoy
Ex 1
B
tanh/sigmoid layer, general
Xavier uses both fans
Ex 2
C
Square layer n in = n o u t
He vs Xavier give different answers even here
Ex 3
D
Uniform sampling (need bound a )
must convert variance → half-width via a 2 /3
Ex 4
E
Degenerate: n in = 1
limiting/edge case, formula still holds
Ex 5
F
Wrong constant → exploding
per-layer gain > 1 , raised to depth
Ex 6
G
Wrong constant → vanishing
per-layer gain < 1 , raised to depth
Ex 7
H
Real-world word problem
pick activation → pick init → size it
Ex 8
I
Exam twist: mixed activations
which formula per layer, and why
Ex 9
Intuition One edge-case the variance story hides
The whole He/Xavier argument tracks variance and assumes zero mean. But ReLU, ϕ ( z ) = max ( 0 , z ) , outputs only non-negative numbers, so its activations have a small positive mean even when z is zero-mean. That mean shift can slowly accumulate down a deep net (each layer adds a little positive bias), which is one reason Batch Normalization — which re-centres activations every step — pairs so well with He init. The examples below use the variance-only rule (it is what He/Xavier actually prescribe), but keep this mean-shift caveat in mind for very deep ReLU stacks.
A hidden layer maps a vector of size 784 → 128 with ReLU . Give Var ( w ) and the standard deviation σ to sample w ∼ N ( 0 , σ 2 ) .
Forecast: which number does σ depend on — 784 , 128 , or both? Guess before reading.
Activation is ReLU → use He. He variance is Var ( w ) = 2/ n in .
Why this step? ReLU zeroes the negative half of its inputs, halving variance; He's factor-2 compensates (parent derivation). Xavier's constant would leave the signal too small in a ReLU net.
Identify fan-in. Inputs come in with dimension 784 , so n in = 784 . The 128 is fan-out and He ignores it.
Why this step? He balances the forward pass only, which depends on how many terms are summed into each z = ∑ i = 1 n in w i x i — that count is n in .
Plug in. Var ( w ) = 784 2 ≈ 0.002551 .
Standard deviation. σ = 0.002551 ≈ 0.05051 .
Verify: σ 2 = 0.0505 1 2 ≈ 0.002551 = 2/784 . ✓ The answer used only fan-in, exactly as He prescribes. Answer to the forecast: only 784 .
A layer maps 256 → 64 with tanh . Give Var ( w ) (normal form).
Forecast: will the variance be bigger or smaller than He on the same fan-in?
Activation is tanh → use Xavier. Var ( w ) = n in + n o u t 2 .
Why this step? tanh is symmetric and near-linear around 0 , so it does not kill half the signal — no factor-2 boost needed. Xavier instead compromises between the forward rule 1/ n in and the backward rule 1/ n o u t by averaging the two fans.
Sum the fans. n in + n o u t = 256 + 64 = 320 .
Why this step? Xavier's denominator is the sum, so both the forward and backward variance constraints are approximately met at once.
Plug in. Var ( w ) = 320 2 = 0.00625 .
Verify: compare to He on n in = 256 : He gives 2/256 = 0.0078125 . Xavier here (0.00625 ) is smaller — as forecast, because the denominator 320 > 256 . ✓
Layer maps 100 → 100 . Compute both the He and Xavier variances and see they differ even though n in = n o u t .
Forecast: for a square layer, do the two formulas coincide? Many students say "yes" — check it.
He (if ReLU). Var He = n in 2 = 100 2 = 0.02 .
Xavier (if tanh). Var Xav = n in + n o u t 2 = 200 2 = 0.01 .
Why this step? Even when the two fans are equal, Xavier's denominator is n in + n o u t = 2 n in , so Xavier is exactly half of He. They never coincide.
Ratio. Var He / Var Xav = 0.02/0.01 = 2 .
Why this step? That factor-2 is precisely ReLU's compensation — the mnemonic "He is Two."
Verify: 2/100 = 0.02 , 2/200 = 0.01 , ratio = 2 . ✓ The activation, not the shape, decides.
Use Xavier uniform for a 400 → 200 tanh layer: find the half-width a so that w ∼ U [ − a , a ] .
Forecast: will a be larger or smaller than Var ( w ) ?
Target variance (Xavier). Var ( w ) = 400 + 200 2 = 600 2 = 0.003333 .
Relate uniform variance to a . A uniform on [ − a , a ] has variance a 2 /3 .
Why this step? Sampling uniformly, not normally, changes the spread–variance link. We must invert it so the uniform delivers the same variance the theory demands.
Solve for a algebraically. Set the uniform's variance equal to the target:
3 a 2 = n in + n o u t 2 .
Multiply both sides by 3 to isolate a 2 :
a 2 = 3 ⋅ n in + n o u t 2 = n in + n o u t 6 .
Take the square root:
a = n in + n o u t 6 .
Why this step? This is exactly where the mysterious "6" in Xavier-uniform comes from — it is 3 × 2 , the 3 from the uniform's a 2 /3 and the 2 from Xavier's numerator. Now plug numbers: a = 6/600 = 0.01 = 0.1 .
Verify: a 2 /3 = 0. 1 2 /3 = 0.01/3 = 0.003333 = Var ( w ) . ✓ Note a = 0.1 is larger than Var = 0.003333 ≈ 0.0577 , because uniform spreads mass to the edges.
A neuron has exactly one input (n in = 1 ) and uses ReLU. What He variance does the formula give, and does it make sense?
Forecast: with one input, is variance-preservation even meaningful?
Apply He. Var ( w ) = 1 2 = 2 , so σ = 2 ≈ 1.4142 .
Sanity check the forward variance. For one term, Var ( z ) = n in Var ( w ) Var ( x ) = 1 ⋅ 2 ⋅ Var ( x ) = 2 Var ( x ) .
Why this step? The forward-variance law Var ( z ) = n in Var ( w ) Var ( x ) still holds with n in = 1 . He deliberately doubles it so that ReLU's halving brings the activation variance back to Var ( x ) .
After ReLU. Var ( a ) = 2 1 Var ( z ) = 2 1 ⋅ 2 Var ( x ) = Var ( x ) . Preserved. ✓
Why this step? This shows the formula is not a fluke of large n — it works at the tiny edge n in = 1 too.
Verify: 2/1 = 2 , and 2 1 ( 1 ⋅ 2 ) = 1 , so the activation variance equals the input variance exactly. ✓ Degenerate case is consistent.
Definition Per-layer gain
The per-layer gain g is the single number by which one layer multiplies the activation variance: if the input to a layer has variance V , the output has variance g ⋅ V . For a ReLU layer, g = n in ⋅ Var ( w ) ⋅ 2 1 — fan-in (how many terms are summed) times the weight variance times ReLU's halving. He is designed so that g = 1 ; any other constant makes g = 1 .
A 6-layer ReLU net with n in = 256 per layer is initialized naively with Var ( w ) = 1 (standard normal). By what factor does the activation variance scale from input to output?
Forecast: guess the order of magnitude — tens? millions? billions?
Per-layer gain. Forward variance factor g = n in ⋅ Var ( w ) ⋅ 2 1 = 256 ⋅ 1 ⋅ 0.5 = 128 .
Why this step? Using the gain definition above with the naive Var ( w ) = 1 : each ReLU layer multiplies variance by 128 .
Raise to depth. Over 6 layers the total scaling is g 6 = 12 8 6 .
Why this step? The gain compounds — layer after layer multiplies, so exponents, not sums, govern the outcome.
Evaluate. 12 8 6 = 4 , 398 , 046 , 511 , 104 ≈ 4.4 × 1 0 12 .
Verify: 128 = 2 7 , so 12 8 6 = 2 42 = 4398046511104 . ✓ Activation variance blows up by ~4.4 trillion → explosion (see Vanishing and Exploding Gradients ).
How to read the figure below: the horizontal axis is layer depth 0 → 6 ; the vertical axis is the activation-variance scaling on a log scale (each gridline is × 10 ). The orange curve is this naive gain-128 case — it rockets almost vertically, passing 1 0 12 by layer 6. The flat magenta line is He (g = 1 ): read straight across at height 1 , meaning variance is unchanged. The gap between the two curves at any depth is exactly how badly the naive init has distorted the signal.
Same 6-layer ReLU net, n in = 256 , but now over-cautious: Var ( w ) = 1024 1 . What is the total variance scaling across 6 layers?
Forecast: will the signal survive to layer 6, or die?
Per-layer gain. g = 256 ⋅ 1024 1 ⋅ 2 1 = 2048 256 = 0.125 .
Why this step? Same gain recipe (fan-in × weight-variance × ReLU's ½), but now g < 1 , so each layer shrinks the signal.
Raise to depth. Total = g 6 = 0.12 5 6 .
Evaluate. 0.12 5 6 = ( 1/8 ) 6 = 1/262144 ≈ 3.81 × 1 0 − 6 .
Verify: 0.125 = 2 − 3 , so 0.12 5 6 = 2 − 18 = 1/262144 ≈ 3.815 × 1 0 − 6 . ✓ The signal is ∼ 260,000× smaller at the output → vanishing .
How to read the figure below: same axes as before — depth on the horizontal, variance scaling on a log vertical axis. The violet curve is this gain-0.125 case; it plunges below 1 0 − 5 by layer 6, i.e. the signal has all but disappeared. The magenta flat line at height 1 is He (g = 1 ) — the only choice that neither climbs nor collapses. Reading the two figures together: explosion and vanishing are mirror images across the magenta He line.
You are building an image classifier. The convolutional backbone flattens to 2048 features, then a fully-connected head maps 2048 → 512 → 512 → 10 . Layers 1–2 use ReLU , the final 512 → 10 uses a softmax (linear pre-activation). Initialize every weight matrix.
Forecast: how many different init formulas will you use across these three matrices?
Layer 2048 → 512 (ReLU) → He. Var = 2/2048 = 0.0009766 , σ ≈ 0.03125 .
Why this step? ReLU → He, fan-in = 2048 .
Layer 512 → 512 (ReLU) → He. Var = 2/512 = 0.003906 , σ ≈ 0.0625 .
Why this step? ReLU again, fan-in = 512 .
Layer 512 → 10 (linear/softmax) → Xavier. Var = 512 + 10 2 = 522 2 = 0.003831 .
Why this step? The softmax's pre-activation is a plain linear combination z = ∑ w i x i — softmax normalises the outputs but does not rectify or clip the pre-activation, so unlike ReLU it does not zero out half the signal. Its second moment is therefore not halved, and He's factor-2 boost would over-inflate the variance. On the backward pass, gradients flow through this narrow head symmetrically (both the 512 inputs and the 10 outputs matter), which is exactly the situation Xavier's symmetric 2/ ( n in + n o u t ) was derived for. Note the fan-out (10 ) genuinely matters here because the head is so narrow.
Verify: 2/2048 = 0.0009766 (√=0.03125); 2/512 = 0.003906 (√=0.0625); 2/522 = 0.003831 . Two He, one Xavier → two formulas across three matrices, as forecast. ✓
Two layers, both 300 → 300 . Layer 1 uses sigmoid , layer 2 uses ReLU . A student initializes both with He (2/300 ). Which layer is mis-initialized, by what factor, and what's correct?
Forecast: which of the two is wrong?
Layer 2 (ReLU) with He is correct. Var = 2/300 = 0.006667 . ✓
Why this step? ReLU → He is exactly right.
Layer 1 (sigmoid) should be Xavier. Correct value = 300 + 300 2 = 600 2 = 0.003333 .
Why this step? Sigmoid (like tanh) is not a half-killer, so it needs Xavier, not He.
Quantify the error. Student used 2/300 = 0.006667 ; correct is 0.003333 . Ratio = 0.006667/0.003333 = 2 → the sigmoid layer's weights are twice the correct variance.
Why this step? Over-large variance in a sigmoid layer pushes pre-activations into the flat tails where the gradient ≈ 0 — the vanishing-gradient trap from Activation Functions (ReLU, tanh, sigmoid) .
Verify: ( 2/300 ) / ( 2/600 ) = 600/300 = 2 . ✓ Layer 1 (sigmoid) is over-scaled by exactly 2 × ; fix it with Xavier = 2/600 = 0.003333 variance.
Recall Quick self-test
ReLU 512 → 128 : which fan and which constant? ::: He, 2/ n in = 2/512 ; fan-out 128 is ignored.
tanh 256 → 64 variance? ::: Xavier 2/ ( 256 + 64 ) = 2/320 = 0.00625 .
Xavier uniform half-width for 400 → 200 ? ::: a = 6/600 = 0.1 (from a 2 /3 = 2/600 ⇒ a 2 = 6/600 ).
He variance for n in = 1 ? ::: 2/1 = 2 (still preserves variance after ReLU).
Naive Var ( w ) = 1 on n in = 256 ReLU, 6 layers — total gain? ::: 12 8 6 = 2 42 ≈ 4.4 × 1 0 12 (explodes).
Why is a square layer's He twice its Xavier? ::: Xavier denominator is 2 n in , so it is half of He's 2/ n in — ReLU's compensation factor.
What value do we initialize biases to, and why? ::: Zero — biases have no fan-in to amplify and keeping b = 0 preserves the zero-mean assumption the variance derivation needs.