Practice / Initialisation and optimisers

Xavier and He initialisation: the variance by hand

Ten problems on weight initialisation: the variance of a product and of a dense layer's output, why a ReLU halves the second moment, He's 2/n_in and Xavier's 2/(n_in + n_out) derived forwards and backwards, the signal scale through depth, fan-in for convolutions, the leaky-ReLU gain and uniform bounds, and residual stacks, with worked solutions and the mistakes that lose a factor of 2, 3 or 9.

Before you start

Before the first gradient step, a network's weights are random numbers, and their variance decides whether a signal that enters the first layer arrives at the last one at a usable size. Too small and every layer shrinks it, so the output and the gradients vanish; too large and they explode. The two standard answers, Xavier (Glorot and Bengio, 2010) and He (He et al., 2015), come from the same short calculation: the variance of a sum of products of independent random numbers. These ten problems do that calculation forwards and backwards, through a ReLU and a leaky ReLU, for convolutions, for uniform distributions and through residual stacks. The five mistakes at the end each change the variance by a constant factor per layer, which depth turns into an exponential: a variance used where a second moment belongs, Xavier trusted on a ReLU network, a uniform's half-width read as its standard deviation, a convolution's fan-in counted as its channels, and a residual block assumed to preserve what its branch preserves.

  • E\mathbb{E} is expectation and Var⁡(u)=E[u2]−(E[u])2\operatorname{Var}(u) = \mathbb{E}[u^2] - (\mathbb{E}[u])^2. The second moment E[u2]\mathbb{E}[u^2] equals the variance exactly when E[u]=0\mathbb{E}[u] = 0. If uu and vv are independent, E[uv]=E[u] E[v]\mathbb{E}[uv] = \mathbb{E}[u]\,\mathbb{E}[v], and the same holds for any functions of them, such as u2u^2 and v2v^2.
  • A dense layer is y=Wxy = Wx with W∈Rnout×ninW \in \mathbb{R}^{n_{\text{out}} \times n_{\text{in}}}, so yi=∑jWij xjy_i = \sum_j W_{ij}\,x_j. The fan-in ninn_{\text{in}} is the number of inputs each output sums; the fan-out noutn_{\text{out}} is the number of outputs each input feeds. Biases are initialised to 00 and left out.
  • At initialisation the WijW_{ij} are independent and identically distributed, independent of the layer's input, with mean 00 and variance σw2\sigma_w^2, and symmetric: −Wij-W_{ij} has the same distribution as WijW_{ij}. N(0,σ2)\mathcal{N}(0, \sigma^2) (normal) and U(−b,b)U(-b, b) (uniform on [−b,b][-b, b]) are both symmetric.
  • A random variable zz is symmetric about 00 if −z-z has the same distribution as zz; then E[z]=0\mathbb{E}[z] = 0. Every distribution on this page is continuous, so P(z=0)=0P(z = 0) = 0.
  • relu⁡(t)=max⁡(0,t)\operatorname{relu}(t) = \max(0, t), and relu⁡′(t)\operatorname{relu}'(t) is 11 for t>0t > 0 and 00 for t<0t < 0. The leaky ReLU with slope α∈[0,1]\alpha \in [0, 1] is fα(t)=tf_\alpha(t) = t for t≥0t \ge 0 and αt\alpha t for t<0t < 0. Both act entry by entry.
  • A deep network: y(l)=W(l)h(l−1)y^{(l)} = W^{(l)}h^{(l-1)} and h(l)=relu⁡(y(l))h^{(l)} = \operatorname{relu}(y^{(l)}) for l=1,…,Ll = 1, \dots, L, with h(0)=xh^{(0)} = x the input, W(l)∈Rnl×nl−1W^{(l)} \in \mathbb{R}^{n_l \times n_{l-1}} and weight variance σl2\sigma_l^2. Entries of one layer are treated as identically distributed, so Var⁡(y(l))\operatorname{Var}(y^{(l)}) means the variance of any one entry.
  • The backward pass uses the code-style names of the minibatch page: dydy is ∇yL\nabla_y L for a scalar loss LL. Through y=Wxy = Wx, dx=W⊤dydx = W^\top dy, so dxj=∑iWij dyidx_j = \sum_i W_{ij}\,dy_i; through h=relu⁡(y)h = \operatorname{relu}(y), dy=dh⊙relu⁡′(y)dy = dh \odot \operatorname{relu}'(y), with ⊙\odot the elementwise product.
  • All of this describes the network at initialisation, averaged over the random weights. The backward calculations also treat a layer's WW as independent of the dydy arriving from above, the simplification Glorot and Bengio made; it is not exact, but it predicts the scale well.

Builds on: One-hidden-layer backprop, the whole backward pass

Problems

  1. ·

    Let ww and xx be independent with E[w]=0\mathbb{E}[w] = 0 and Var⁡(w)=σw2\operatorname{Var}(w) = \sigma_w^2. Show that Var⁡(wx)=σw2 E[x2]\operatorname{Var}(wx) = \sigma_w^2\,\mathbb{E}[x^2]. Evaluate it for σw2=0.25\sigma_w^2 = 0.25 and an xx with mean 11 and variance 44, and compare it with Var⁡(w)Var⁡(x)\operatorname{Var}(w)\operatorname{Var}(x).

  2. ··

    A dense layer y=Wxy = Wx has weights as in Before you start, and inputs xjx_j with a common second moment q=E[xj2]q = \mathbb{E}[x_j^2]; the xjx_j need not be independent of each other. Show that Var⁡(yi)=nin σw2 q\operatorname{Var}(y_i) = n_{\text{in}}\,\sigma_w^2\,q, and find the σw2\sigma_w^2 that makes E[yi2]=q\mathbb{E}[y_i^2] = q.

  3. ··

    Let zz be symmetric about 00 with variance σ2\sigma^2. Show that E[relu⁡(z)2]=σ2/2\mathbb{E}[\operatorname{relu}(z)^2] = \sigma^2/2. For z∼N(0,σ2)z \sim \mathcal{N}(0, \sigma^2), also find E[relu⁡(z)]\mathbb{E}[\operatorname{relu}(z)] and Var⁡(relu⁡(z))\operatorname{Var}(\operatorname{relu}(z)).

  4. ···

    In the deep network of Before you start, suppose the entries of y(l−1)y^{(l-1)} are symmetric about 00. Show that Var⁡(y(l))=12 nl−1 σl2 Var⁡(y(l−1))\operatorname{Var}(y^{(l)}) = \tfrac12\,n_{l-1}\,\sigma_l^2\,\operatorname{Var}(y^{(l-1)}), that the entries of y(l)y^{(l)} are again symmetric, and find the σl2\sigma_l^2 that keeps the variance fixed.

  5. ··

    A ReLU network of constant width nn uses σl2=c/n\sigma_l^2 = c/n in every layer. Find Var⁡(y(L))/Var⁡(y(1))\operatorname{Var}(y^{(L)})/\operatorname{Var}(y^{(1)}). Evaluate it for c=1c = 1 and L=21L = 21, which is Xavier's choice when nin=nout=nn_{\text{in}} = n_{\text{out}} = n, and say what c=2.2c = 2.2 does over the same depth.

  6. ··

    In the backward pass through y=Wxy = Wx, dx=W⊤dydx = W^\top dy. Suppose the dyidy_i share a second moment E[dyi2]\mathbb{E}[dy_i^2] and are independent of WW. Show that Var⁡(dxj)=nout σw2 E[dyi2]\operatorname{Var}(dx_j) = n_{\text{out}}\,\sigma_w^2\,\mathbb{E}[dy_i^2]. Explain Xavier's σw2=2/(nin+nout)\sigma_w^2 = 2/(n_{\text{in}} + n_{\text{out}}), and evaluate it and both scale factors for a layer from 784784 inputs to 256256 outputs.

  7. ···

    In a ReLU network with He's fan-in initialisation σl2=2/nl−1\sigma_l^2 = 2/n_{l-1}, the gradient goes back through dy(l)=dh(l)⊙relu⁡′(y(l))dy^{(l)} = dh^{(l)} \odot \operatorname{relu}'(y^{(l)}) and dh(l−1)=W(l)⊤dy(l)dh^{(l-1)} = W^{(l)\top}dy^{(l)}. Treat relu⁡′(y(l))\operatorname{relu}'(y^{(l)}) as independent of dh(l)dh^{(l)} and W(l)W^{(l)} as independent of dy(l)dy^{(l)}. Show that E[(dh(l−1))2]=nlnl−1 E[(dh(l))2]\mathbb{E}[(dh^{(l-1)})^2] = \dfrac{n_l}{n_{l-1}}\,\mathbb{E}[(dh^{(l)})^2], and find the factor from dh(4)dh^{(4)} back to dh(1)dh^{(1)} for widths n1=1024n_1 = 1024, n2=512n_2 = 512, n3=256n_3 = 256, n4=128n_4 = 128. Which variance would make each factor 11?

  8. ·

    A 2-D convolution has Cin=64C_{\text{in}} = 64 input channels, Cout=128C_{\text{out}} = 128 output channels, a 3×33 \times 3 kernel and stride 11: yo,r,c=∑i,a,bKo,i,a,b xi,r+a,c+by_{o,r,c} = \sum_{i,a,b} K_{o,i,a,b}\,x_{i,r+a,c+b}. Find its fan-in and fan-out, and the He and Xavier weight variances.

  9. ···

    For a leaky ReLU, show that E[fα(z)2]=12(1+α2) σz2\mathbb{E}[f_\alpha(z)^2] = \tfrac12(1 + \alpha^2)\,\sigma_z^2 for zz symmetric about 00, and find the weight standard deviation g/ning/\sqrt{n_{\text{in}}} that preserves the variance, where gg is the gain. Show that Var⁡(U(−b,b))=b2/3\operatorname{Var}\big(U(-b, b)\big) = b^2/3 and find the uniform bound bb with the same variance. PyTorch's nn.Linear initialises its weight with kaiming_uniform_(weight, a=math.sqrt(5)), where a is the slope that enters the gain: what are its bound and variance?

  10. ···

    A residual stack computes h(l)=h(l−1)+βFl(h(l−1))h^{(l)} = h^{(l-1)} + \beta F_l(h^{(l-1)}) for l=1,…,Ll = 1, \dots, L, with a scalar β≥0\beta \ge 0. Each branch is initialised so that E[Fl(h)i hi]=0\mathbb{E}[F_l(h)_i\,h_i] = 0 and E[Fl(h)i2]=E[hi2]\mathbb{E}[F_l(h)_i^2] = \mathbb{E}[h_i^2] (for example Fl(h)=VhF_l(h) = Vh with Var⁡(Vij)=1/n\operatorname{Var}(V_{ij}) = 1/n). With ql=E[(hi(l))2]q_l = \mathbb{E}[(h_i^{(l)})^2], find qL/q0q_L/q_0. Evaluate it for β=1\beta = 1, L=10L = 10, and for β=1/L\beta = 1/\sqrt L with L=10L = 10; show that the second is below ee for every LL.

Worked solutions

Problem 1

Let ww and xx be independent with E[w]=0\mathbb{E}[w] = 0 and Var⁡(w)=σw2\operatorname{Var}(w) = \sigma_w^2. Show that Var⁡(wx)=σw2 E[x2]\operatorname{Var}(wx) = \sigma_w^2\,\mathbb{E}[x^2]. Evaluate it for σw2=0.25\sigma_w^2 = 0.25 and an xx with mean 11 and variance 44, and compare it with Var⁡(w)Var⁡(x)\operatorname{Var}(w)\operatorname{Var}(x).

  1. E[wx]=E[w] E[x]=0\mathbb{E}[wx] = \mathbb{E}[w]\,\mathbb{E}[x] = 0.Independence factorises the expectation of the product, and E[w]=0\mathbb{E}[w] = 0 kills it whatever xx's mean is.
  2. Var⁡(wx)=E[w2x2]−02=E[w2] E[x2]\operatorname{Var}(wx) = \mathbb{E}[w^2x^2] - 0^2 = \mathbb{E}[w^2]\,\mathbb{E}[x^2].The definition of variance with step 1's mean; w2w^2 and x2x^2 are functions of independent variables, so they are independent too.
  3. E[w2]=Var⁡(w)+(E[w])2=σw2\mathbb{E}[w^2] = \operatorname{Var}(w) + (\mathbb{E}[w])^2 = \sigma_w^2.The definition of variance rearranged, with a zero mean.
  4. E[x2]=Var⁡(x)+(E[x])2=4+1=5\mathbb{E}[x^2] = \operatorname{Var}(x) + (\mathbb{E}[x])^2 = 4 + 1 = 5.The same identity for xx, whose mean is not zero, so its square stays.
  5. Var⁡(wx)=σw2 E[x2]=0.25×5=1.25\operatorname{Var}(wx) = \sigma_w^2\,\mathbb{E}[x^2] = 0.25 \times 5 = 1.25, while Var⁡(w)Var⁡(x)=1\operatorname{Var}(w)\operatorname{Var}(x) = 1The product of the variances misses σw2(E[x])2\sigma_w^2(\mathbb{E}[x])^2: because ww's sign is random, even a constant input contributes spread to wxwx. The two agree only when E[x]=0\mathbb{E}[x] = 0, which a ReLU output never satisfies (Problem 3).

Problem 2

A dense layer y=Wxy = Wx has weights as in Before you start, and inputs xjx_j with a common second moment q=E[xj2]q = \mathbb{E}[x_j^2]; the xjx_j need not be independent of each other. Show that Var⁡(yi)=nin σw2 q\operatorname{Var}(y_i) = n_{\text{in}}\,\sigma_w^2\,q, and find the σw2\sigma_w^2 that makes E[yi2]=q\mathbb{E}[y_i^2] = q.

  1. E[yi]=∑jE[Wij] E[xj]=0\mathbb{E}[y_i] = \sum_j \mathbb{E}[W_{ij}]\,\mathbb{E}[x_j] = 0.Linearity of expectation, then independence of WW from xx, then the zero mean of the weights.
  2. E[yi2]=∑j∑kE[WijWik xjxk]\mathbb{E}[y_i^2] = \sum_j\sum_k \mathbb{E}[W_{ij}W_{ik}\,x_jx_k].Square the sum and use linearity; there are nin2n_{\text{in}}^2 terms.
  3. For j≠kj \neq k: E[WijWik xjxk]=E[Wij] E[Wik] E[xjxk]=0\mathbb{E}[W_{ij}W_{ik}\,x_jx_k] = \mathbb{E}[W_{ij}]\,\mathbb{E}[W_{ik}]\,\mathbb{E}[x_jx_k] = 0.WijW_{ij}, WikW_{ik} and the input are mutually independent. The cross terms vanish because of the weights' zero means, so correlated inputs do no harm.
  4. For j=kj = k: E[Wij2 xj2]=σw2 q\mathbb{E}[W_{ij}^2\,x_j^2] = \sigma_w^2\,q.Problem 1, step 2, with E[Wij2]=σw2\mathbb{E}[W_{ij}^2] = \sigma_w^2.
  5. Var⁡(yi)=nin σw2 q\operatorname{Var}(y_i) = n_{\text{in}}\,\sigma_w^2\,q, so σw2=1/nin\sigma_w^2 = 1/n_{\text{in}} keeps E[yi2]=q\mathbb{E}[y_i^2] = qOnly the ninn_{\text{in}} diagonal terms survive, and with a zero mean the second moment is the variance. This is LeCun's initialisation. It preserves the scale of a linear network, and of a tanh network near 00, where tanh⁡t≈t\tanh t \approx t. For nin=256n_{\text{in}} = 256 it is a standard deviation of 1/161/16.

Problem 3

Let zz be symmetric about 00 with variance σ2\sigma^2. Show that E[relu⁡(z)2]=σ2/2\mathbb{E}[\operatorname{relu}(z)^2] = \sigma^2/2. For z∼N(0,σ2)z \sim \mathcal{N}(0, \sigma^2), also find E[relu⁡(z)]\mathbb{E}[\operatorname{relu}(z)] and Var⁡(relu⁡(z))\operatorname{Var}(\operatorname{relu}(z)).

  1. relu⁡(z)2=z2\operatorname{relu}(z)^2 = z^2 when z>0z > 0 and 00 when z≤0z \le 0.relu⁡\operatorname{relu} passes positive values and zeroes the rest.
  2. E[z2; z>0]=E[z2; z<0]\mathbb{E}[z^2;\, z > 0] = \mathbb{E}[z^2;\, z < 0], where E[u; A]\mathbb{E}[u;\, A] is the expectation of uu times the indicator of the event AA.−z-z has the same distribution as zz, and z↦−zz \mapsto -z swaps the two events while leaving z2z^2 unchanged.
  3. E[z2; z>0]+E[z2; z<0]=E[z2]=σ2\mathbb{E}[z^2;\, z > 0] + \mathbb{E}[z^2;\, z < 0] = \mathbb{E}[z^2] = \sigma^2.The two events cover everything except z=0z = 0, where z2=0z^2 = 0; and E[z]=0\mathbb{E}[z] = 0, so the second moment is the variance.
  4. E[relu⁡(z)2]=σ2/2\mathbb{E}[\operatorname{relu}(z)^2] = \sigma^2/2.Steps 1 to 3: the left side is one of two equal halves of σ2\sigma^2.
  5. For z∼N(0,σ2)z \sim \mathcal{N}(0, \sigma^2): E[relu⁡(z)]=1σ2π∫0∞t e−t2/(2σ2) dt=σ2σ2π=σ2π\mathbb{E}[\operatorname{relu}(z)] = \dfrac{1}{\sigma\sqrt{2\pi}}\displaystyle\int_0^\infty t\,e^{-t^2/(2\sigma^2)}\,dt = \dfrac{\sigma^2}{\sigma\sqrt{2\pi}} = \dfrac{\sigma}{\sqrt{2\pi}}.The integrand is the normal density times tt on t>0t > 0, and −σ2e−t2/(2σ2)-\sigma^2 e^{-t^2/(2\sigma^2)} is an antiderivative of t e−t2/(2σ2)t\,e^{-t^2/(2\sigma^2)}.
  6. E[relu⁡(z)2]=σ2/2\mathbb{E}[\operatorname{relu}(z)^2] = \sigma^2/2; for normal zz, E[relu⁡(z)]=σ/2π\mathbb{E}[\operatorname{relu}(z)] = \sigma/\sqrt{2\pi} and Var⁡(relu⁡(z))=σ2(12−12π)≈0.341 σ2\operatorname{Var}(\operatorname{relu}(z)) = \sigma^2\big(\tfrac12 - \tfrac1{2\pi}\big) \approx 0.341\,\sigma^2The variance is step 4 minus step 5 squared. The next layer needs the second moment, σ2/2\sigma^2/2, not this variance (Problem 2), and the 12\tfrac12 is what He's factor 22 undoes (Problem 4).

Problem 4

In the deep network of Before you start, suppose the entries of y(l−1)y^{(l-1)} are symmetric about 00. Show that Var⁡(y(l))=12 nl−1 σl2 Var⁡(y(l−1))\operatorname{Var}(y^{(l)}) = \tfrac12\,n_{l-1}\,\sigma_l^2\,\operatorname{Var}(y^{(l-1)}), that the entries of y(l)y^{(l)} are again symmetric, and find the σl2\sigma_l^2 that keeps the variance fixed.

  1. Var⁡(yi(l))=nl−1 σl2 E[(hj(l−1))2]\operatorname{Var}(y_i^{(l)}) = n_{l-1}\,\sigma_l^2\,\mathbb{E}\big[(h_j^{(l-1)})^2\big].Problem 2 with x=h(l−1)x = h^{(l-1)}, which is computed from earlier layers only and so is independent of W(l)W^{(l)}.
  2. E[(hj(l−1))2]=12Var⁡(yj(l−1))\mathbb{E}\big[(h_j^{(l-1)})^2\big] = \tfrac12\operatorname{Var}(y_j^{(l-1)}).Problem 3 applied to z=yj(l−1)z = y_j^{(l-1)}, which is symmetric by hypothesis.
  3. Var⁡(yi(l))=12 nl−1 σl2 Var⁡(yj(l−1))\operatorname{Var}(y_i^{(l)}) = \tfrac12\,n_{l-1}\,\sigma_l^2\,\operatorname{Var}(y_j^{(l-1)}).Substitute step 2 into step 1.
  4. Negating row ii of W(l)W^{(l)} negates yi(l)y_i^{(l)} and leaves the joint distribution of the weights and h(l−1)h^{(l-1)} unchanged, so yi(l)y_i^{(l)} is symmetric.The weights are symmetric and independent of everything below them. The first layer's output is symmetric for the same reason, whatever the input, so the hypothesis holds at every layer from l=2l = 2 on.
  5. Var⁡(y(l))=12 nl−1σl2Var⁡(y(l−1))\operatorname{Var}(y^{(l)}) = \tfrac12\,n_{l-1}\sigma_l^2\operatorname{Var}(y^{(l-1)}); He initialisation σl2=2/nl−1\sigma_l^2 = 2/n_{l-1} makes the factor exactly 11The standard deviation is 2/nin\sqrt{2/n_{\text{in}}}, which is 1/161/16 for nin=512n_{\text{in}} = 512. PyTorch's kaiming_normal_ with nonlinearity='relu' uses this, as gain/fan_in\text{gain}/\sqrt{\text{fan\_in}} with gain 2\sqrt2; its default mode is 'fan_in'.

Problem 5

A ReLU network of constant width nn uses σl2=c/n\sigma_l^2 = c/n in every layer. Find Var⁡(y(L))/Var⁡(y(1))\operatorname{Var}(y^{(L)})/\operatorname{Var}(y^{(1)}). Evaluate it for c=1c = 1 and L=21L = 21, which is Xavier's choice when nin=nout=nn_{\text{in}} = n_{\text{out}} = n, and say what c=2.2c = 2.2 does over the same depth.

  1. Var⁡(y(l))=c2Var⁡(y(l−1))\operatorname{Var}(y^{(l)}) = \tfrac c2\operatorname{Var}(y^{(l-1)}) for l≥2l \ge 2.Problem 4, step 3, with nl−1σl2=cn_{l-1}\sigma_l^2 = c.
  2. Var⁡(y(L))=(c2)L−1Var⁡(y(1))\operatorname{Var}(y^{(L)}) = \big(\tfrac c2\big)^{L-1}\operatorname{Var}(y^{(1)}).Apply step 1 for l=2,…,Ll = 2, \dots, L: that is L−1L - 1 factors. The first layer reads the raw input, not a ReLU output, so it is not one of them.
  3. For c=1c = 1, L=21L = 21: 2−20=1/1048576≈9.5×10−72^{-20} = 1/1048576 \approx 9.5 \times 10^{-7}.210=10242^{10} = 1024, so 220=102422^{20} = 1024^2.
  4. For c=2.2c = 2.2: 1.120≈6.71.1^{20} \approx 6.7.The same formula with c/2=1.1c/2 = 1.1.
  5. Var⁡(y(L))/Var⁡(y(1))=(c/2)L−1\operatorname{Var}(y^{(L)})/\operatorname{Var}(y^{(1)}) = (c/2)^{L-1}; for c=1c = 1, L=21L = 21 it is 2−20≈9.5×10−72^{-20} \approx 9.5\times10^{-7}, a standard deviation 10241024 times smallerThe standard deviation scales by the square root, 2−102^{-10}. A constant factor per layer becomes exponential in depth, so only c=2c = 2 neither vanishes nor explodes; 10%10\% too much variance per layer already multiplies it by 6.76.7 over twenty layers.

Problem 6

In the backward pass through y=Wxy = Wx, dx=W⊤dydx = W^\top dy. Suppose the dyidy_i share a second moment E[dyi2]\mathbb{E}[dy_i^2] and are independent of WW. Show that Var⁡(dxj)=nout σw2 E[dyi2]\operatorname{Var}(dx_j) = n_{\text{out}}\,\sigma_w^2\,\mathbb{E}[dy_i^2]. Explain Xavier's σw2=2/(nin+nout)\sigma_w^2 = 2/(n_{\text{in}} + n_{\text{out}}), and evaluate it and both scale factors for a layer from 784784 inputs to 256256 outputs.

  1. dxj=∑i=1noutWij dyidx_j = \sum_{i=1}^{n_{\text{out}}} W_{ij}\,dy_i.Entry jj of W⊤dyW^\top dy combines column jj of WW with dydy: one term per output.
  2. E[dxj]=0\mathbb{E}[dx_j] = 0 and E[dxj2]=nout σw2 E[dyi2]\mathbb{E}[dx_j^2] = n_{\text{out}}\,\sigma_w^2\,\mathbb{E}[dy_i^2].Problem 2's argument with the roles swapped: the cross terms E[WijWkj dyi dyk]\mathbb{E}[W_{ij}W_{kj}\,dy_i\,dy_k] vanish for i≠ki \ne k because WijW_{ij} and WkjW_{kj} are independent with mean 00, and the sum now runs over noutn_{\text{out}} terms.
  3. The forward pass keeps its scale when ninσw2=1n_{\text{in}}\sigma_w^2 = 1 (Problem 2), the backward pass when noutσw2=1n_{\text{out}}\sigma_w^2 = 1.Both hold only if nin=noutn_{\text{in}} = n_{\text{out}}.
  4. σw2=2nin+nout\sigma_w^2 = \dfrac{2}{n_{\text{in}} + n_{\text{out}}} gives 12(ninσw2+noutσw2)=1\tfrac12\big(n_{\text{in}}\sigma_w^2 + n_{\text{out}}\sigma_w^2\big) = 1.Glorot and Bengio's compromise: the variance is the reciprocal of the average fan, so the two factors average to 11.
  5. Var⁡(dxj)=nout σw2 E[dyi2]\operatorname{Var}(dx_j) = n_{\text{out}}\,\sigma_w^2\,\mathbb{E}[dy_i^2]; for 784→256784 \to 256, Xavier gives σw2=2/1040=1/520\sigma_w^2 = 2/1040 = 1/520, a forward factor 784/520≈1.51784/520 \approx 1.51 and a backward factor 256/520≈0.49256/520 \approx 0.49Neither direction is preserved exactly; each is off by the ratio of one fan to the average. The derivation assumes linear units, so on a ReLU network both factors are halved again (Problems 5 and 7).

Problem 7

In a ReLU network with He's fan-in initialisation σl2=2/nl−1\sigma_l^2 = 2/n_{l-1}, the gradient goes back through dy(l)=dh(l)⊙relu⁡′(y(l))dy^{(l)} = dh^{(l)} \odot \operatorname{relu}'(y^{(l)}) and dh(l−1)=W(l)⊤dy(l)dh^{(l-1)} = W^{(l)\top}dy^{(l)}. Treat relu⁡′(y(l))\operatorname{relu}'(y^{(l)}) as independent of dh(l)dh^{(l)} and W(l)W^{(l)} as independent of dy(l)dy^{(l)}. Show that E[(dh(l−1))2]=nlnl−1 E[(dh(l))2]\mathbb{E}[(dh^{(l-1)})^2] = \dfrac{n_l}{n_{l-1}}\,\mathbb{E}[(dh^{(l)})^2], and find the factor from dh(4)dh^{(4)} back to dh(1)dh^{(1)} for widths n1=1024n_1 = 1024, n2=512n_2 = 512, n3=256n_3 = 256, n4=128n_4 = 128. Which variance would make each factor 11?

  1. relu⁡′(yi(l))\operatorname{relu}'(y_i^{(l)}) is 11 with probability 12\tfrac12 and 00 otherwise.yi(l)y_i^{(l)} is symmetric about 00 (Problem 4, step 4) and continuous, so it is positive half the time.
  2. E[(dyi(l))2]=E[relu⁡′(yi(l))2] E[(dhi(l))2]=12 E[(dhi(l))2]\mathbb{E}[(dy_i^{(l)})^2] = \mathbb{E}[\operatorname{relu}'(y_i^{(l)})^2]\,\mathbb{E}[(dh_i^{(l)})^2] = \tfrac12\,\mathbb{E}[(dh_i^{(l)})^2].The two factors are treated as independent, and 02=00^2 = 0, 12=11^2 = 1, so the square of the mask is the mask.
  3. E[(dhj(l−1))2]=nl σl2 E[(dyi(l))2]\mathbb{E}[(dh_j^{(l-1)})^2] = n_l\,\sigma_l^2\,\mathbb{E}[(dy_i^{(l)})^2].Problem 6, step 2: W(l)W^{(l)} has nln_l rows, so the backward sum has nln_l terms.
  4. E[(dhj(l−1))2]=nl⋅2nl−1⋅12 E[(dhi(l))2]=nlnl−1 E[(dhi(l))2]\mathbb{E}[(dh_j^{(l-1)})^2] = n_l\cdot\dfrac{2}{n_{l-1}}\cdot\tfrac12\,\mathbb{E}[(dh_i^{(l)})^2] = \dfrac{n_l}{n_{l-1}}\,\mathbb{E}[(dh_i^{(l)})^2].Steps 2 and 3 with σl2=2/nl−1\sigma_l^2 = 2/n_{l-1}: the ReLU's 12\tfrac12 cancels the 22.
  5. From dh(4)dh^{(4)} to dh(1)dh^{(1)}: n4n3⋅n3n2⋅n2n1=n4n1=1281024=18\dfrac{n_4}{n_3}\cdot\dfrac{n_3}{n_2}\cdot\dfrac{n_2}{n_1} = \dfrac{n_4}{n_1} = \dfrac{128}{1024} = \dfrac18.Step 4 for l=4,3,2l = 4, 3, 2; the product telescopes.
  6. E[(dh(l−1))2]=(nl/nl−1) E[(dh(l))2]\mathbb{E}[(dh^{(l-1)})^2] = (n_l/n_{l-1})\,\mathbb{E}[(dh^{(l)})^2]; from layer 44 back to layer 11 the factor is n4/n1=1/8n_4/n_1 = 1/8; σl2=2/nl\sigma_l^2 = 2/n_l (fan-out) makes each factor 11With fan-in initialisation the backward pass is off only by the ratio of two widths, not by an exponential in depth, which is why the fan-in mode is a safe default. PyTorch exposes the other choice as mode='fan_out'.

Problem 8

A 2-D convolution has Cin=64C_{\text{in}} = 64 input channels, Cout=128C_{\text{out}} = 128 output channels, a 3×33 \times 3 kernel and stride 11: yo,r,c=∑i,a,bKo,i,a,b xi,r+a,c+by_{o,r,c} = \sum_{i,a,b} K_{o,i,a,b}\,x_{i,r+a,c+b}. Find its fan-in and fan-out, and the He and Xavier weight variances.

  1. Each output yo,r,cy_{o,r,c} is a sum over i=1,…,64i = 1, \dots, 64 and a,b=0,1,2a, b = 0, 1, 2: 64×3×3=57664 \times 3 \times 3 = 576 products.Fan-in counts the products summed into one output, and a convolution sums over every input channel and every kernel position.
  2. Var⁡(yo,r,c)=576 σw2 E[x2]\operatorname{Var}(y_{o,r,c}) = 576\,\sigma_w^2\,\mathbb{E}[x^2].Problem 2 with nin=576n_{\text{in}} = 576: the 576576 weights in one output's sum are distinct and independent. The same weights are reused at other positions, which correlates different outputs but does not change the variance of one.
  3. An input xi,r,cx_{i,r,c} away from the border appears in 128×3×3=1152128 \times 3 \times 3 = 1152 outputs, each through a different weight.It sits at each of the 99 kernel offsets of some window, for each of the 128128 output channels; the backward sum of Problem 6 has that many terms.
  4. fan-in =576= 576, fan-out =1152= 1152; He σw2=2/576=1/288\sigma_w^2 = 2/576 = 1/288 (standard deviation ≈0.0589\approx 0.0589); Xavier σw2=2/(576+1152)=1/864\sigma_w^2 = 2/(576 + 1152) = 1/864Both are channels times the receptive-field size khkwk_hk_w, which is how PyTorch computes the fans of a convolution weight.

Problem 9

For a leaky ReLU, show that E[fα(z)2]=12(1+α2) σz2\mathbb{E}[f_\alpha(z)^2] = \tfrac12(1 + \alpha^2)\,\sigma_z^2 for zz symmetric about 00, and find the weight standard deviation g/ning/\sqrt{n_{\text{in}}} that preserves the variance, where gg is the gain. Show that Var⁡(U(−b,b))=b2/3\operatorname{Var}\big(U(-b, b)\big) = b^2/3 and find the uniform bound bb with the same variance. PyTorch's nn.Linear initialises its weight with kaiming_uniform_(weight, a=math.sqrt(5)), where a is the slope that enters the gain: what are its bound and variance?

  1. fα(z)2=z2f_\alpha(z)^2 = z^2 for z>0z > 0 and α2z2\alpha^2z^2 for z<0z < 0.The leaky ReLU multiplies negative inputs by α\alpha, so their squares by α2\alpha^2.
  2. E[fα(z)2]=12σz2+α2⋅12σz2=12(1+α2) σz2\mathbb{E}[f_\alpha(z)^2] = \tfrac12\sigma_z^2 + \alpha^2\cdot\tfrac12\sigma_z^2 = \tfrac12(1 + \alpha^2)\,\sigma_z^2.Problem 3, steps 2 and 3: each sign carries half of σz2\sigma_z^2.
  3. Var⁡(y(l))=12(1+α2) nin σw2Var⁡(y(l−1))\operatorname{Var}(y^{(l)}) = \tfrac12(1 + \alpha^2)\,n_{\text{in}}\,\sigma_w^2\operatorname{Var}(y^{(l-1)}), so σw2=2(1+α2) nin\sigma_w^2 = \dfrac{2}{(1 + \alpha^2)\,n_{\text{in}}} and g=2/(1+α2)g = \sqrt{2/(1 + \alpha^2)}.Problem 4, step 3, with step 2 in place of the ReLU's 12\tfrac12. α=0\alpha = 0 gives He's 2\sqrt2 and α=1\alpha = 1, a linear unit, gives LeCun's 11.
  4. Var⁡(U(−b,b))=∫−bbt22b dt=b23\operatorname{Var}\big(U(-b, b)\big) = \displaystyle\int_{-b}^{b}\frac{t^2}{2b}\,dt = \frac{b^2}{3}.The density is 1/(2b)1/(2b) on the interval and the mean is 00; ∫−bbt2 dt=2b3/3\int_{-b}^b t^2\,dt = 2b^3/3.
  5. b2/3=g2/ninb^2/3 = g^2/n_{\text{in}} gives b=g3/ninb = g\sqrt{3/n_{\text{in}}}.Match the uniform's variance to the normal one's.
  6. Slope 5\sqrt5 in step 3's gain: g2=2/(1+5)=1/3g^2 = 2/(1 + 5) = 1/3, so b=1/3 3/nin=1/ninb = \sqrt{1/3}\,\sqrt{3/n_{\text{in}}} = 1/\sqrt{n_{\text{in}}}.kaiming_uniform_ computes its bound as gain times 3/fan_in\sqrt{3/\text{fan\_in}}, with the leaky-ReLU gain of step 3 evaluated at its a; a slope above 11 is not a sensible activation, only a way to set the scale.
  7. E[fα(z)2]=12(1+α2)σz2\mathbb{E}[f_\alpha(z)^2] = \tfrac12(1+\alpha^2)\sigma_z^2, g=2/(1+α2)g = \sqrt{2/(1+\alpha^2)}, uniform bound b=g3/ninb = g\sqrt{3/n_{\text{in}}}; with slope 5\sqrt5 the bound is 1/nin1/\sqrt{n_{\text{in}}} and the variance 1/(3nin)1/(3n_{\text{in}})That variance is a sixth of He's 2/nin2/n_{\text{in}}, so a plain stack of default nn.Linear layers with ReLUs shrinks the variance by 1/61/6 per layer (Problem 5 with c=1/3c = 1/3). The same step 5 gives He uniform 6/nin\sqrt{6/n_{\text{in}}} and Xavier uniform 6/(nin+nout)\sqrt{6/(n_{\text{in}} + n_{\text{out}})}.

Problem 10

A residual stack computes h(l)=h(l−1)+βFl(h(l−1))h^{(l)} = h^{(l-1)} + \beta F_l(h^{(l-1)}) for l=1,…,Ll = 1, \dots, L, with a scalar β≥0\beta \ge 0. Each branch is initialised so that E[Fl(h)i hi]=0\mathbb{E}[F_l(h)_i\,h_i] = 0 and E[Fl(h)i2]=E[hi2]\mathbb{E}[F_l(h)_i^2] = \mathbb{E}[h_i^2] (for example Fl(h)=VhF_l(h) = Vh with Var⁡(Vij)=1/n\operatorname{Var}(V_{ij}) = 1/n). With ql=E[(hi(l))2]q_l = \mathbb{E}[(h_i^{(l)})^2], find qL/q0q_L/q_0. Evaluate it for β=1\beta = 1, L=10L = 10, and for β=1/L\beta = 1/\sqrt L with L=10L = 10; show that the second is below ee for every LL.

  1. ql=E[(hi+βFi)2]=ql−1+2β E[hiFi]+β2 E[Fi2]q_l = \mathbb{E}[(h_i + \beta F_i)^2] = q_{l-1} + 2\beta\,\mathbb{E}[h_iF_i] + \beta^2\,\mathbb{E}[F_i^2], with h=h(l−1)h = h^{(l-1)} and F=Fl(h)F = F_l(h).Expand the square and use linearity of expectation.
  2. ql=(1+β2) ql−1q_l = (1 + \beta^2)\,q_{l-1}.The cross term is 00 and E[Fi2]=ql−1\mathbb{E}[F_i^2] = q_{l-1}, by the hypotheses on the branch.
  3. qL=(1+β2)L q0q_L = (1 + \beta^2)^L\,q_0.Apply step 2 for each of the LL blocks.
  4. β=1\beta = 1, L=10L = 10: 210=10242^{10} = 1024.The skip and the branch each carry the full second moment, and they add.
  5. β=1/L\beta = 1/\sqrt L: (1+1/L)L(1 + 1/L)^L, which for L=10L = 10 is 1.110≈2.5941.1^{10} \approx 2.594.Step 3 with β2=1/L\beta^2 = 1/L.
  6. Llog⁡(1+1/L)<L⋅1L=1L\log(1 + 1/L) < L\cdot\tfrac1L = 1, so (1+1/L)L<e(1 + 1/L)^L < e.log⁡(1+t)<t\log(1 + t) < t for t>0t > 0, because log⁡\log is concave and tt is its tangent line at t=0t = 0.
  7. qL/q0=(1+β2)Lq_L/q_0 = (1 + \beta^2)^L: 10241024 for β=1\beta = 1, L=10L = 10, and (1+1/L)L≈2.594<e(1 + 1/L)^L \approx 2.594 < e for β=1/L\beta = 1/\sqrt L, L=10L = 10Uncorrelated signals add their second moments, so a stack of variance-preserving branches still grows exponentially with depth. Scaling the branches by 1/L1/\sqrt L caps the growth at ee however deep the stack; some ResNet recipes instead start each branch's last layer at zero, which is β=0\beta = 0 at initialisation.

Where this goes wrong

1. He variance computed from Var(h) instead of E[h²]

Initialisation is described as keeping "the variance" fixed, so it is natural to put the previous layer's variance into the formula.

  1. Var⁡(yi(l))=n σ2 E[(hj(l−1))2]\operatorname{Var}(y_i^{(l)}) = n\,\sigma^2\,\mathbb{E}\big[(h_j^{(l-1)})^2\big]Right so far: Problem 4, step 1, for width nn.
  2. “For independent factors, Var⁡(wx)=Var⁡(w)Var⁡(x)\operatorname{Var}(wx) = \operatorname{Var}(w)\operatorname{Var}(x), so the input enters through its variance.”The shortcut that causes the mistake: that product rule needs both means to be 00, and a ReLU output has mean σ/2π\sigma/\sqrt{2\pi} (Problem 3).
  3. Var⁡(y(l))=nσ2 (12−12π)Var⁡(y(l−1))\operatorname{Var}(y^{(l)}) = n\sigma^2\,\big(\tfrac12 - \tfrac1{2\pi}\big)\operatorname{Var}(y^{(l-1)}), so σ2=1/((12−12π) n)≈2.93/n\sigma^2 = 1/\big((\tfrac12 - \tfrac1{2\pi})\,n\big) \approx 2.93/nThe second moment, 12Var⁡(y(l−1))\tfrac12\operatorname{Var}(y^{(l-1)}), is what reaches the next layer (Problem 1), giving 2/n2/n. With 2.93/n2.93/n each layer multiplies the variance by about 1.471.47, and twenty layers by about 21002100.

2. Xavier assumed to keep a ReLU network's variance constant

Xavier initialisation is a common library default (Keras's Dense layer uses glorot_uniform), and it was derived to keep the forward variance fixed.

  1. σ2=2/(nin+nout)=1/n\sigma^2 = 2/(n_{\text{in}} + n_{\text{out}}) = 1/n when nin=nout=nn_{\text{in}} = n_{\text{out}} = nRight so far: Problem 6, step 4, at equal widths.
  2. “With nσ2=1n\sigma^2 = 1, Problem 2 says each layer passes its variance on unchanged.”The shortcut that causes the mistake: Problem 2 preserves the second moment of the layer's input, and Xavier's derivation assumes that input is the previous layer's output unchanged, as in a linear network.
  3. Var⁡(y(l))=nσ2Var⁡(y(l−1))=Var⁡(y(l−1))\operatorname{Var}(y^{(l)}) = n\sigma^2\operatorname{Var}(y^{(l-1)}) = \operatorname{Var}(y^{(l-1)})A ReLU sits between the layers and halves the second moment (Problem 3), so the factor is 12\tfrac12 per layer: after 2121 layers the variance is 2−202^{-20} of the first layer's (Problem 5), and the gradients shrink the same way.

3. He uniform bound set to the standard deviation √(2/n_in)

The normal and the uniform versions of He initialisation are both described by the scale 2/nin\sqrt{2/n_{\text{in}}}, and that number gets used as the uniform's limit.

  1. He initialisation wants Var⁡(Wij)=2/nin\operatorname{Var}(W_{ij}) = 2/n_{\text{in}}Right so far: Problem 4.
  2. “Sample Wij∼U(−2/nin, 2/nin)W_{ij} \sim U\big(-\sqrt{2/n_{\text{in}}},\ \sqrt{2/n_{\text{in}}}\big).”The shortcut that causes the mistake: treating a uniform's half-width as its standard deviation.
  3. Var⁡(Wij)=2/nin\operatorname{Var}(W_{ij}) = 2/n_{\text{in}}Var⁡(U(−b,b))=b2/3\operatorname{Var}\big(U(-b, b)\big) = b^2/3 (Problem 9), so this gives 2/(3nin)2/(3n_{\text{in}}), a third of the target, and the variance falls by a factor of 33 per ReLU layer. The bound must be 3\sqrt3 times larger: 6/nin\sqrt{6/n_{\text{in}}}.

4. Convolution fan-in counted as the input channels

For a dense layer the fan-in is the number of input features, and a convolution's input features look like its channels.

  1. Var⁡(y)=fan-in×σ2 E[x2]\operatorname{Var}(y) = \text{fan-in}\times\sigma^2\,\mathbb{E}[x^2], where the fan-in is the number of products summed into one outputRight so far: Problem 2.
  2. “The layer has Cin=64C_{\text{in}} = 64 input channels, so its fan-in is 6464.”The analogy that causes the mistake: a convolution sums over the kernel's positions as well as the channels.
  3. σ2=2/Cin=2/64=1/32\sigma^2 = 2/C_{\text{in}} = 2/64 = 1/32Each output sums Cinkhkw=576C_{\text{in}}k_hk_w = 576 products (Problem 8), so with ReLUs each layer multiplies the variance by 12×576/32=9\tfrac12 \times 576/32 = 9, the kernel area: five such layers multiply it by 95=590499^5 = 59049. The correct variance is 2/5762/576.

5. Residual block assumed to preserve variance because its branch does

Each branch of a residual network is initialised to preserve the scale of its input, so the whole block looks as if it does too.

  1. E[Fl(h)i2]=E[hi2]\mathbb{E}[F_l(h)_i^2] = \mathbb{E}[h_i^2] and E[Fl(h)i hi]=0\mathbb{E}[F_l(h)_i\,h_i] = 0Right so far: the branch hypotheses of Problem 10.
  2. “The branch preserves the variance and the skip connection passes hh through untouched, so their sum keeps it too.”The analogy that causes the mistake: treating the sum of two signals of the same size as if it had that size.
  3. E[(hi(l))2]=E[(hi(l−1))2]\mathbb{E}[(h_i^{(l)})^2] = \mathbb{E}[(h_i^{(l-1)})^2]Uncorrelated terms add their second moments, so each block doubles it (Problem 10): 2L2^L over LL blocks, about 1.1×10151.1 \times 10^{15} for L=50L = 50. Scale the branches by 1/L1/\sqrt L, or start them at zero.

Print this set: xavier-and-he-initialisation.pdf (problems, answers, and worked solutions on separate pages).