Weight Initialization
Weight initialization sets the random starting values of a neural network’s weights and biases before training. Layer takes activations and computes
where is the activation function and is the input. The fan-in counts the inputs of one unit, and the fan-out counts the units of the layer. Suppose the weights are independent with mean and variance , and the biases are . Each pre-activation then has variance
Each layer multiplies the size of the signal by a factor set by . Over many layers these factors multiply. Activations then shrink or grow geometrically with depth, and the gradients of the backward pass do the same. The schemes below assume standardized inputs (mean , variance ) and choose to keep the factor at :
- Zero initialization sets every weight to . With ReLU or tanh, every hidden activation and every weight gradient is then . The weights never move.
- Xavier initialization draws . It keeps the variance of a linear layer, and of tanh near , where tanh is close to the identity.
- He initialization, also called Kaiming initialization, draws for a layer fed by ReLU units. A ReLU outputs half the time, so the variance doubles to compensate.
- Glorot initialization draws . The backward pass needs the fan-out in place of the fan-in, and Glorot averages the two.
A uniform distribution has variance , so each scheme has a uniform version with . Glorot uniform uses , and He uniform uses . Biases have three common choices:
- Zero, the usual default. The random weights already break the symmetry between units.
- A small constant such as .
- Xavier with fan-in , which treats the bias as one more weight on a constant input of .
Inputs are standardized for a related reason. A scalar ReLU unit bends at the elbow . With , the elbow has a standard Cauchy distribution, and half of all elbows fall in .
Names Differ Between Sources
The lecture calls Xavier initialization, and many sources call it LeCun initialization. Glorot and Bengio (2010), the paper behind the name Xavier, proposed the average . PyTorch’s
xavier_normal_follows the paper and uses the average. Itskaiming_normal_is He initialization, with fan-in by default. The default fornn.Linearis neither. It draws , which has variance . For a convolution, PyTorch counts and .
Not Taught in Lecture
The uniform versions come from Glorot and Bengio (2010) and from the PyTorch library, not from lecture.
Intuition
Each unit adds up inputs, each multiplied by a random weight. The variance of a sum of uncorrelated terms is times the variance of one term. To keep the output at the size of the input, the weight variance must shrink like . A deep network repeats this step once per layer. A per-layer factor slightly off then causes geometric growth or decay. With weights drawn from , a ReLU layer of width multiplies the RMS of its activations by . The figure follows a ReLU network through ten layers for three weight variances, starting from a standardized input.
\usepackage{tikz}
\begin{document}
\definecolor{hot}{HTML}{D9483B}
\definecolor{cold}{HTML}{2F80ED}
\definecolor{grn}{HTML}{2F9E44}
\definecolor{mid}{HTML}{8A8A8A}
\begin{tikzpicture}[x=1cm,y=1cm,>=latex]
\foreach \k in {-5,...,3} \draw[mid] (0,\k*0.5) -- (-0.1,\k*0.5);
\foreach \k/\lab in {-4/{1/16},-2/{1/4},0/{1},2/{4}} \node[left] at (-0.1,\k*0.5) {\small $\lab$};
\draw[->,thick] (0,-2.7) -- (0,1.9);
\draw[->,thick] (0,-2.7) -- (7.5,-2.7);
\foreach \l in {0,2,...,10} {\draw (\l*0.7,-2.7) -- (\l*0.7,-2.8); \node[below] at (\l*0.7,-2.8) {\small $\l$};}
\node at (3.5,-3.5) {layer $\ell$};
\node[rotate=90] at (-1.3,-0.4) {RMS of $\mathbf{a}_\ell$ (log scale)};
\draw[hot,very thick] (0.00,0.000) -- (0.70,0.146) -- (1.40,0.292) -- (2.10,0.439) -- (2.80,0.585) -- (3.50,0.731) -- (4.20,0.877) -- (4.90,1.024) -- (5.60,1.170) -- (6.30,1.316) -- (7.00,1.462);
\draw[grn,very thick] (0,0) -- (7,0);
\draw[cold,very thick] (0.00,0.000) -- (0.70,-0.250) -- (1.40,-0.500) -- (2.10,-0.750) -- (2.80,-1.000) -- (3.50,-1.250) -- (4.20,-1.500) -- (4.90,-1.750) -- (5.60,-2.000) -- (6.30,-2.250) -- (7.00,-2.500);
\foreach \l in {0,...,10} {
\fill[hot] (\l*0.7,\l*0.14624) circle (0.05);
\fill[grn] (\l*0.7,0) circle (0.05);
\fill[cold] (\l*0.7,-\l*0.25) circle (0.05);
}
\node[hot,right] at (7.2,1.46) {$\sigma_w^2 = 3/d$: RMS $7.6$};
\node[grn,right] at (7.2,0) {$\sigma_w^2 = 2/d$ (He): RMS $1$};
\node[cold,right] at (7.2,-2.5) {$\sigma_w^2 = 1/d$ (Xavier): RMS $1/32$};
\end{tikzpicture}
\end{document}The backward pass multiplies the gradient by at every layer, so gradients compound in the same way. Gradients that shrink layer by layer are called vanishing gradients. Gradients that grow are called exploding gradients, and they can overflow floating-point range. The initial weights and biases also set where each ReLU bends. A unit whose bend lies outside the data is either or a straight line over the whole data range. A layer of such units passes only a straight-line function of the input to the next layer. Inputs are standardized before training to prevent this.
Properties
(Errors Compound With Depth) In a ReLU network of constant width , each layer multiplies by the gain .
After layers, , and the RMS of the activations scales by . Xavier’s gives on a ReLU network, so the RMS halves every two layers. He’s gives exactly. An error of in changes by about . Over layers, this compounds to a factor of to . The recursion describes the average over random weights. A single network of finite width scatters around it, and the scatter grows with depth.
(Backward Pass Uses Fan-Out) The gradient variance changes by the factor per ReLU layer, so stable gradients need .
Not Taught in Lecture
Lecture stated Glorot’s formula without the backward argument. The argument and the ReLU compromise below follow Prince §7.5.2 and §7.5.3.
Write . Backpropagation gives . Each entry of sums terms, and the ReLU derivative is half the time. Treating as independent of , the variance changes by . The forward gain is , so the ratio of the two gains is for every . A non-square layer cannot keep both at . Glorot averages the two widths, which gives for linear and tanh layers. Prince’s ReLU version is .
(Identical Units Stay Identical) Two hidden units with the same incoming weights, bias and outgoing weights receive the same gradients, so they stay copies.
The two units compute the same activation on every input. Their outgoing weights are equal, so backpropagation gives them the same . Their weight gradients are then equal, and gradient descent keeps them equal. A layer of copies acts like a single unit, whatever its width. Any constant initialization has this problem, and zero is the extreme case. With ReLU or tanh, , so every activation and weight gradient is , and only the output bias can move. With sigmoid, , so the weights do move, but every unit in a layer remains a copy. Random weights give each unit a different starting point, which breaks the symmetry.
(Elbow Placement) A ReLU unit adds a bend only if its elbow lies inside the data, and random elbows cluster near .
The standard Cauchy density is . It has no finite mean or variance, but its median is , and . Half the elbows fall in , about in , about in , and within . Lecture rounds the last bound to . Suppose the data lie in . Then only of elbows land there, about units in . Every other unit is either or affine, , on the data. Without the elbowed units, the feature matrix would be , a sum of two rank-one matrices. Each elbowed unit adds at most one to the rank. A practical check is to inspect the rank or the singular values of a hidden layer’s feature matrix. Zero biases cause a related problem for a scalar input. Every elbow then sits at , and every unit is a multiple of or . The first layer’s features have rank until training moves the biases.
\usepackage{tikz}
\begin{document}
\definecolor{hot}{HTML}{D9483B}
\definecolor{cold}{HTML}{2F80ED}
\definecolor{prp}{HTML}{8E6BD6}
\begin{tikzpicture}[x=1cm,y=1cm,>=latex]
\draw[->,thick] (-4.4,0) -- (4.5,0);
\foreach \t in {-6,-3,0,3,6} {\draw (\t*0.5,0) -- (\t*0.5,-0.1); \node[below] at (\t*0.5,-0.1) {\small $\t$};}
\node at (0,-0.85) {elbow position $-b/w$};
\foreach \x/\y in {-0.50/1.273,-0.40/1.553,-0.30/1.872,-0.20/2.195,-0.10/2.449,0.00/2.546,0.10/2.449,0.20/2.195,0.30/1.872,0.40/1.553,0.50/1.273} \draw[hot,line width=1.5pt] (\x,0) -- (\x,\y);
\draw[very thick] plot coordinates {(-4.250,0.035) (-4.000,0.039) (-3.750,0.044) (-3.500,0.051) (-3.250,0.059) (-3.000,0.069) (-2.750,0.081) (-2.500,0.098) (-2.250,0.120) (-2.000,0.150) (-1.750,0.192) (-1.500,0.255) (-1.375,0.297) (-1.250,0.351) (-1.125,0.420) (-1.000,0.509) (-0.875,0.627) (-0.750,0.784) (-0.625,0.994) (-0.500,1.273) (-0.375,1.630) (-0.250,2.037) (-0.125,2.397) (0.000,2.546) (0.125,2.397) (0.250,2.037) (0.375,1.630) (0.500,1.273) (0.625,0.994) (0.750,0.784) (0.875,0.627) (1.000,0.509) (1.125,0.420) (1.250,0.351) (1.375,0.297) (1.500,0.255) (1.750,0.192) (2.000,0.150) (2.250,0.120) (2.500,0.098) (2.750,0.081) (3.000,0.069) (3.250,0.059) (3.500,0.051) (3.750,0.044) (4.000,0.039) (4.250,0.035)};
\node at (-2.6,1.5) {density $1/(\pi(1+x^2))$};
\node[hot] at (1.9,2.2) {$50\%$ in $[-1, 1]$};
\draw[cold,very thick,dashed] (-3.157,0) -- (-3.157,1.0);
\draw[cold,very thick,dashed] (3.157,0) -- (3.157,1.0);
\node[cold] at (3.157,1.3) {$90\%$ within $\pm 6.31$};
\draw[->,thick,prp] (3.6,0.5) -- (4.4,0.5);
\node[prp,right] at (4.4,0.5) {$[100, 200]$: $0.16\%$};
\end{tikzpicture}
\end{document}(RMS-Norm View) Xavier initialization keeps close to .
An RMS norm of means that a typical entry has size about . For a fixed input, each output entry has mean square over the random weights. Under Xavier this equals . Once the weights are drawn, inputs in a random direction keep their RMS, but special directions do not. A square Xavier matrix has largest singular value about . An input along its top singular vector doubles in RMS. 4.3. Maximal Update Parameterization turns the RMS view into conditions on whole weight matrices and on their updates. Those conditions decide how learning rates should scale with width.
(Width Changes Do Not Compound) With He fan-in at every layer, the gradient’s squared length stays the same from layer to layer.
Not Taught in Lecture
This observation is from He et al. (2015), the paper that introduced He initialization.
With , the backward gain across layer is . Over a stack, these gains telescope to , so the hidden widths cancel. Each unit’s gradient variance is proportional to , which keeps constant. Choosing fan-in therefore fixes the forward pass exactly and leaves the backward pass off by one bounded ratio. Fan-out does the reverse.
(Where Zeros Are Safe) A single layer can start at zero when the units feeding it already differ.
Not Taught in Lecture
Fixup (Zhang et al., 2019), cited in Prince’s chapter notes, and Goyal et al. (2017) use this.
Two common cases are the output layer and the last layer of a residual branch. Suppose the output layer starts at . Its gradient is still nonzero, because the random hidden layers give varied activations. After one step the earlier layers receive gradients that differ from unit to unit. Fixup zeroes the last layer of each residual branch, and Goyal et al. zero the last BatchNorm scale of each block. Each residual block then starts as the identity.
(Normalization and Skips Reduce Sensitivity) Normalization layers and residual connections make a network much less sensitive to the initial weight scale.
A normalization layer rescales activations to unit variance on every forward pass. A wrong then no longer compounds across layers. BatchNorm output does not change when the incoming weights and bias are multiplied by the same positive constant. Residual connections carry the signal and the gradient through identity paths that do not depend on the weights. He-initialized branches still double the variance at every block, so residual networks pair skips with normalization or rescaling.
Derivation
We want the variance of a pre-activation in layer of a ReLU network, in terms of layer . Three assumptions are needed. First, the weights of layer are independent, with mean , variance and a distribution symmetric about . Second, is independent of , which holds because depends only on the input and earlier layers. Third, the biases are . The activations may be correlated with each other.
By linearity and independence, . Expanding the square of the sum,
For , independence gives , so only the diagonal terms survive:
where is the average over the units. Next we need for . Flipping the sign of every weight in layer flips the sign of . The flipped weights have the same distribution, so and have the same distribution. Since when and otherwise,
Lecture reaches the same factor by counting. About half of the inputs are , so the sum has about active terms. Each active input has mean square . A ReLU output has positive mean, so is smaller than . For Gaussian , , and using it would give the wrong factor. Combining the two results,
Setting the factor to gives , which is He initialization. For the identity activation, , and the same step gives Xavier’s .
Examples
(Easy) Standard Deviations for One Layer
A recommendation model has a dense layer that maps ReLU features to units.
What standard deviation does each scheme use, and which one keeps the forward variance? . He uses . Glorot uses . The inputs come from ReLUs, so He keeps the forward variance, with gain . Glorot gives forward gain , and Xavier gives . The He uniform version draws from , since .
Xavier uses
(Medium) A Small Error in a Deep Network
A -layer ReLU MLP has width , so He initialization uses . One engineer uses , and another uses .
By what factor does change after layers in each case? How many layers does it take to halve or double? With , the gain is . After layers, , so the RMS falls to of its start. The mean square halves every layers. With , and , so the RMS grows by . It doubles every layers. A simulation of networks matched both averages. Single networks scattered widely, and the middle spanned a factor of about .
(Medium) Gains at a Bottleneck
An autoencoder narrows from ReLU units to units in one dense layer.
Find the forward and backward variance gains under He fan-in, He fan-out and Prince's compromise . The forward gain is and the backward gain is . He fan-in uses , which gives forward and backward. He fan-out uses , which gives forward and backward. The compromise uses , which gives forward and backward. The forward gain is times the backward gain in every case, because . A simulation reproduces all six gains.
(Hard) Elbows and Floor Area
A model predicts apartment prices from floor area, which is spread evenly between and square meters. Its first layer has ReLU units with .
What fraction of elbows fall inside the data, before and after standardization? What does this do to the rank of the feature matrix? , about units. The other units are or affine on the data, so the feature matrix has rank about . A uniform distribution on has mean and standard deviation . Standardized, the data fill , and the fraction becomes . About units now bend inside the data. In a simulation with apartments, the raw rank was plus the number of elbowed units. The standardized matrix had numerical rank about .
Before standardization, the fraction is
See Also
- 4.3. Maximal Update Parameterization, which extends the RMS view to update sizes and to learning rates across widths.
- 3. Activation Functions, for the ReLU, tanh and sigmoid shapes that set the factor in the variance rule.
- 4.5. Neural Network Normalization, which restores unit variance during training.
- 5.2.5. Residual Networks, where He-initialized blocks double the variance unless normalized.
- 2. Neural Networks, for the layer equations used here.