All courses

Initialization and activation scale

Explain why weights are initialized with variance ∝ 1/fan_in (fan_in = number of inputs to each unit), so activations neither blow up nor shrink layer by layer. Predict what happens with a bad initialization scale.

Builds on: Variance of sums and dot products of random vectors, MLPs and why nonlinearities matter

▶ Watch this lesson Start the whole course

What the animation says

The narrow window

Before training, every weight in a network is a random number. How big should those numbers be? Take twenty linear layers, each one thousand twenty-four wide, and feed in values of size about one. With weights of standard deviation one, the output comes out about ten to the thirty times bigger. With standard deviation one hundredth, it comes out ten to the minus ten times smaller. So the safe window is narrow. Start with a single layer: by what factor does it multiply the scale?

One layer, then depth

Thirty-two. Each output adds up one thousand twenty-four products: an input times a weight. Independent terms add their variances, so the output variance is n, times the weight variance, times the input's. The size, the standard deviation, grows by the square root of n times the weight's std: thirty-two per layer. Depth compounds it. Ten layers: thirty-two to the tenth, about ten to the fifteen. Weights of one hundredth give point three two per layer, about ten to the minus five. Which weight scale makes the per-layer factor exactly one? Tap it.

ReLU halves it

So for a plain linear layer, use weight variance one over n. Now put a ReLU after it. Each output is a sum of many independent terms with mean zero, so it's roughly symmetric around zero. ReLU zeroes the negative half and keeps the positive half. The mean square drops by half, so the size shrinks by root two per layer. To compensate, double the weight variance: two over n. That's He initialization. Width five hundred twelve, ten ReLU layers, weights one over root n. How big is the output?

Run it

About point oh three. Let's check in code: a batch of random inputs, width five hundred twelve, ten layers. This loop tries three weight scales. Each layer draws fresh random weights, multiplies, then applies ReLU. One over root n: the size shrinks to about point oh three, just as predicted. Root of two over n, the He scale: it stays near one. Point one: the size grows about one point six times per layer, ending near a hundred.

Keep the factor at 1

Why does this matter? Too large, and values blow up toward infinity and not-a-number. Too small, and the signal fades. Biases and rounding swamp it, or it underflows to zero. Then the output carries almost no information about the input, and learning struggles to start. The rule: keep each layer's factor at one. Weight variance one over fan-in, or two over fan-in when a ReLU follows.

How big should random starting weights be?

Before learning starts, every weight in a network is a random number. How large should those numbers be? It sounds like a detail. Yet in a stack of 20 linear layers, each 1024 wide, weight std 1 makes outputs about $10^{30}$ times bigger than the input. Weight std 0.01 makes them about $10^{-10}$ times smaller. Why is the safe window so narrow, and where is it?

Each layer multiplies the scale; depth compounds

Each unit sums fan_in products $x_i w_i$. You've seen that a sum of $n$ independent terms has variance growing like $n$. So one layer multiplies the activation variance by about fan_in × Var(w). Stack $L$ layers and that factor is raised to the $L$-th power. Above 1, values explode exponentially. Below 1, they die away exponentially. The fix is to choose Var(w) so the factor is exactly 1.

Variance through one layer

One layer: $y = xW$, with $x\in\mathbb{R}^{1\times n}$, $W\in\mathbb{R}^{n\times m}$, and $n$ = fan_in. The $W_{ij}$ are independent, mean 0, variance $\sigma_w^2$, and independent of $x$. Then $$\mathrm{Var}(y_j)=n\,\sigma_w^2\,\mathbb{E}[x_i^2]$$ To preserve scale, set $\sigma_w^2 = 1/n$. Each $y_j$ is a mean-0 sum of many independent terms, so it is close to symmetric around 0. A ReLU after the layer zeroes the negative half and keeps the positive half, so $\mathbb{E}[\mathrm{ReLU}(y)^2]=\tfrac12\mathrm{Var}(y)$. Compensate with $\sigma_w^2=2/n$ (He initialization).

Width 1024, three weight scales

Linear stack, width $n=1024$, input std 1. Per layer, std is multiplied by $\sqrt{n}\,\sigma_w$. - Weight std 1: factor $\sqrt{1024}=32$. After 10 layers: $32^{10}\approx 10^{15}$. - Weight std 0.01: factor $32\times0.01=0.32$. After 10 layers: $0.32^{10}\approx 10^{-5}$. - Weight std $1/\sqrt{1024}=1/32$: factor $1$. Std stays 1 at every depth.

Watch a 20-layer ReLU stack

A 20-layer ReLU MLP of width 512, printing the root-mean-square (rms) of the output for three weight scales: - $1/\sqrt n$ shrinks it by $\sqrt2$ per layer, ending near $2^{-10}\approx 0.001$. - $\sqrt{2/n}$ keeps it near 1. - 0.1 grows it about $1.6\times$ per layer, ending near $10^4$.
import numpy as np
rng = np.random.default_rng(0)
n, L = 512, 20
x = rng.standard_normal((1000, n))
for s in [1/np.sqrt(n), np.sqrt(2/n), 0.1]:
    h = x
    for _ in range(L):
        h = np.maximum(0, h @ (rng.standard_normal((n, n)) * s))
    print(f"{s:.4f}  rms={np.sqrt((h**2).mean()):.3g}")

Keep the per-layer factor at 1

Each layer scales activation variance by fan_in × Var(w), halved after a ReLU, and depth compounds this exponentially. Initialize with Var(w) = 1/fan_in, or 2/fan_in for a layer followed by a ReLU. Too large, and values blow up toward inf/NaN. Too small, and the signal shrinks toward zero layer by layer. Added constants (biases) and rounding then swamp it, or it underflows to 0, so the output carries almost no information about the input and learning struggles to start.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.