3.1.12Neural Network Fundamentals

Weight initialization (Xavier, He)

1,529 words7 min readdifficulty · medium6 backlinks

WHAT is the problem?

WHY do we care? A deep net computes, layer by layer: z[l]=W[l]a[l1],a[l]=ϕ(z[l])z^{[l]} = W^{[l]} a^{[l-1]}, \qquad a^{[l]} = \phi(z^{[l]}) If the numbers in WW are on average too big, each layer multiplies the signal up → activations blow up (exploding). Too small → activations shrink to 00 (vanishing). Either way gradients become useless and learning stalls.


HOW to derive the right scale (first principles)

We track the variance of a single pre-activation z=i=1ninwixiz = \sum_{i=1}^{n_{in}} w_i x_i, where ninn_{in} = number of inputs (fan-in).

Assume: weights wiw_i and inputs xix_i are independent, zero-mean, i.i.d.

For independent zero-mean variables, variance of a product and sum gives: Var(z)=i=1ninVar(wixi)=ninVar(w)Var(x)\mathrm{Var}(z) = \sum_{i=1}^{n_{in}} \mathrm{Var}(w_i x_i) = n_{in}\,\mathrm{Var}(w)\,\mathrm{Var}(x)

Why this step? For zero-mean independent w,xw,x: Var(wx)=Var(w)Var(x)\mathrm{Var}(wx)=\mathrm{Var}(w)\mathrm{Var}(x), and variance of a sum of independents adds.

Forward goal: we want Var(z)=Var(x)\mathrm{Var}(z)=\mathrm{Var}(x) so the signal size is preserved. That forces ninVar(w)=1    Var(w)=1nin\boxed{\,n_{in}\,\mathrm{Var}(w) = 1 \;\Rightarrow\; \mathrm{Var}(w) = \frac{1}{n_{in}}\,}

Backward goal: the same argument on gradients flowing back gives noutVar(w)=1n_{out}\,\mathrm{Var}(w)=1, i.e. Var(w)=1/nout\mathrm{Var}(w)=1/n_{out}.

You can't satisfy both exactly, so Xavier compromises with the average:

Why ReLU needs a different constant (He)

ReLU zeroes out half the inputs (all negatives). So on average it kills half the variance: Var(a)=12Var(z)\mathrm{Var}(a) = \tfrac{1}{2}\,\mathrm{Var}(z)

Why this step? For zero-mean symmetric zz, ϕ(z)=max(0,z)\phi(z)=\max(0,z) passes only the positive half → the second moment is halved.

To keep Var(z[l])=Var(z[l1])\mathrm{Var}(z^{[l]})=\mathrm{Var}(z^{[l-1]}) we must double the weight variance to compensate: ninVar(w)12=1    Var(w)=2ninn_{in}\,\mathrm{Var}(w)\cdot\tfrac12 = 1 \;\Rightarrow\; \mathrm{Var}(w) = \frac{2}{n_{in}}

Figure — Weight initialization (Xavier, He)

Worked examples


Recall Feynman: explain to a 12-year-old

Imagine passing a whisper down a line of 50 kids. If each kid speaks a little louder, by the end it's a scream (exploding). If each speaks a little softer, the last kid hears nothing (vanishing). Weight initialization is choosing how loud the first whisper is, and a "volume knob" for each kid, so the message stays the same loudness all the way down the line. ReLU kids only pass the message half the time (they ignore "sad" numbers), so we tell them to speak twice as loud — that's the "2" in He initialization.


Flashcards

Why can't we initialize all weights to zero?
It fails to break symmetry — all neurons in a layer compute identical outputs and get identical gradients, so they never differentiate.
What is the goal of good weight initialization?
Keep the variance of activations (forward) and gradients (backward) roughly constant across layers, avoiding vanishing/exploding.
Xavier/Glorot weight variance formula?
Var(w)=2nin+nout\mathrm{Var}(w)=\dfrac{2}{n_{in}+n_{out}}.
He/Kaiming weight variance formula?
Var(w)=2nin\mathrm{Var}(w)=\dfrac{2}{n_{in}}.
Why does He use a factor of 2 vs the 1/nin1/n_{in} variance-preserving rule?
ReLU zeroes negative inputs, halving the variance; doubling the weight variance compensates.
Which init for ReLU vs tanh/sigmoid?
He for ReLU (and variants); Xavier for tanh/sigmoid/linear.
Derive Var(z)\mathrm{Var}(z) for z=wixiz=\sum w_i x_i with zero-mean i.i.d. terms.
Var(z)=ninVar(w)Var(x)\mathrm{Var}(z)=n_{in}\mathrm{Var}(w)\mathrm{Var}(x).
Uniform Xavier bound aa in U[a,a]\mathcal{U}[-a,a]?
a=6/(nin+nout)a=\sqrt{6/(n_{in}+n_{out})}, from a2/3=Var(w)a^2/3=\mathrm{Var}(w).
What is ninn_{in} (fan-in)?
The number of inputs to a neuron/layer = input dimension of WW.
Per-layer variance gain and depth relationship?
Total scaling = (per-layer gain)depth^{depth}, so any gain 1\ne 1 explodes/vanishes exponentially with depth.

Connections

Concept Map

each layer multiplies signal

too big

too small

gradients useless

gradients useless

identical neurons

fix

track Var of z

backward pass

compromise average

compromise average

for tanh sigmoid

ReLU kills half

for ReLU

Deep net stacks layers

Variance drifts

Exploding activations

Vanishing activations

Learning stalls

Zero init

Symmetry not broken

Random weights + controlled variance

Var w = 1 / n_in forward

Var w = 1 / n_out

Xavier / Glorot: 2 / n_in+n_out

Stable variance

He init: 2 / n_in

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Socho aap ek 50-layer ki deep network bana rahe ho. Har layer input ko matrix WW se multiply karti hai. Agar WW ke numbers thode bhi bade hain, to har layer signal ko thoda bada karti hai — aur 50 baar multiply hoke woh signal aasman chhoo leta hai (exploding). Agar chhote hain, to signal 50 layers me ghis-ghis ke zero ho jaata hai (vanishing). Dono case me gradient bekaar ho jaata hai aur training rukh jaati hai. Isliye starting weights ka scale sahi chunna zaroori hai.

Idea simple hai: hum chahte hain ki har layer ke baad activation ki variance same rahe. Maths se: z=wixiz=\sum w_i x_i ka variance =ninVar(w)Var(x)= n_{in}\cdot \mathrm{Var}(w)\cdot \mathrm{Var}(x) hota hai. Isko constant rakhne ke liye Var(w)=1/nin\mathrm{Var}(w)=1/n_{in} chahiye. Yahi Xavier ka logic hai — tanh/sigmoid ke liye, jahan hum fan-in aur fan-out dono ka average lete hain: Var(w)=2/(nin+nout)\mathrm{Var}(w)=2/(n_{in}+n_{out}).

He initialization ReLU ke liye hai. ReLU saare negative numbers ko zero kar deta hai — yaani aadha signal maar deta hai, variance aadhi ho jaati hai. Isko compensate karne ke liye hum weight ki variance double kar dete hain: Var(w)=2/nin\mathrm{Var}(w)=2/n_{in}. Wahi "2" jo He formula me hai — woh ReLU ke halving ka jawab hai.

Yaad rakhne ka tarika: "He is Two, Xavier is Sum." ReLU use kar rahe ho to He (2/n_in), tanh/sigmoid ho to Xavier (fan-in + fan-out ka sum). Aur kabhi bhi saare weights zero mat karna — warna saare neurons clone ban jaate hain aur kuch seekhte hi nahi (symmetry break nahi hota).

Go deeper — visual, from zero

Test yourself — Neural Network Fundamentals

Connections