Starting weights and activations
Why a network's starting weights are scaled to the layer width, how sigmoid, tanh, ReLU and GELU shape the gradient, and how signals vanish or explode in deep stacks.
What runs in CodeSplash
- Deep neural network (torch MLP) Verified in Python and R
You will be able to
- Explain why initial weights are scaled by the layer's fan-in (and fan-out)
- Match Glorot and He initialization to the activations they suit
- Recognize vanishing and exploding signals in a deep stack
- Say what dead ReLU units are and how they arise
Each layer multiplies the spread
ReadA unit adds up n_in inputs, each times a random weight. If inputs have variance 1 and weights variance σ², the sum has variance about n_in·σ². Stack ten layers and the spread is multiplied ten times: it shrinks toward zero when n_in·σ² < 1 and grows without bound when it's above 1, forward and backward alike.
Weight initializationWeight initializationHow a network's starting weights are drawn. Their spread is scaled to the layer width so signals neither shrink nor blow up. picks σ² to keep that product near one. Glorot's rule uses 2/(n_in + n_out) for tanh; He's rule uses 2/n_in for ReLU, which zeroes half its inputs. Biases start at zero.
Watch the spread through ten layers
Exploreimport numpy as np
rng = np.random.default_rng(0)
x = rng.normal(size=(500, 100))
for name, sd in [("too small", 0.05), ("He", np.sqrt(2 / 100)), ("too large", 0.3)]:
h, sds = x, []
for layer in range(10):
h = np.maximum(0, h @ rng.normal(0, sd, size=(100, 100))) # ReLU layer, width 100
sds.append(h.std())
print(f"{name:10s}", " ".join(f"{s:.2g}" for s in sds))set.seed(0)
x <- matrix(rnorm(500 * 100), 500, 100)
for (cfg in list(c("too small", 0.05), c("He", sqrt(2 / 100)), c("too large", 0.3))) {
h <- x; sds <- numeric(10)
for (layer in 1:10) {
h <- pmax(h %*% matrix(rnorm(100 * 100, 0, as.numeric(cfg[2])), 100, 100), 0)
sds[layer] <- sd(as.vector(h))
}
cat(sprintf("%-10s", cfg[1]), signif(sds, 2), "\n")
}Run either program. With He's scale the spread stays roughly level for all ten layers. The small scale fades layer by layer: a vanishingVanishing gradientRepeated multiplication by the same weights shrinks a signal's effect on the loss toward zero, so far-back steps stop teaching the network. signal. The large one grows by orders of magnitude, the forward face of an exploding gradientExploding gradientGradients that grow layer after layer, or step after step in a recurrent network, until updates overshoot and the loss turns to NaN..
Activations and their slopes
ReadThe backward pass multiplies by each activation's slope, so the slope matters as much as the shape.
| Activation | Slope | Watch for |
|---|---|---|
| Sigmoid | at most a quarter | gradients shrink in deep stacks |
| tanh | at most 1, near 0 when saturated | large inputs stop learning |
| ReLU | 0 or 1 | units stuck at 0 for every row ("dead") |
| GELUGELUA smooth activation, x times the normal CDF of x: close to ReLU for large inputs but curved near zero. Common in Transformers. | smooth, ReLU-like | slightly more compute |
A ReLU unit whose input is negative for every training row gets no gradient and never recovers. Too large a learning rate is the usual cause.
Try it on a ring
ExploreDExplorable · Neural network playground
How do hidden units combine straight lines into curved boundaries?
Each hidden unit can draw one straight line. How accurate will this network be on new data?
Data
Activation
Commit your prediction above to see the result
Make your prediction to see the result.
With three hidden layers, switch Activation between tanh and ReLU and compare the loss curves. Then raise Learning rate until training stops improving. Which activation breaks first?
Check: starting weights and activations
CheckpointCheck yourself
A ReLU layer has 256 inputs and 128 outputs. Which initial weight variance does He's rule give?
Show the answer
Answer: a
He's rule uses 2/n_in, the 2 making up for ReLU zeroing half its inputs. Glorot's 2/(n_in + n_out) is the tanh choice.
Check yourself
Every weight in a hidden layer starts at the same value 0.1. What happens in training?
Show the answer
Answer: a
Identical units receive identical updates, so the layer behaves like one unit however wide it is. Random starting weights break that symmetry.
Check yourself
After a few epochs with a high learning rate, a third of a ReLU network's hidden units output 0 for every training row. What's going on?
Show the answer
Answer: b
ReLU's slope is 0 for negative inputs. A big update can push a unit's bias so far down that no row turns it on again. Lower the learning rate or use a leaky or smooth activation.
Recap
Recap- Each layer multiplies the signal's spread by about n_in·σ²; initialization keeps that near one.
- Glorot suits tanh, He suits ReLU; biases start at zero; weights must not all be equal.
- CodeSplash's torch MLP draws its starting weights uniformly within the Glorot bound, or He's bound before ReLU units, from one shared random stream, so Python and R start identical.
Concepts in this lesson