What Reaches the Last Layer

Training Deep NetworksMedium+20 XP+20

What Reaches the Last Layer

The chapter's rule for initialization is scale the spread by the number of inputs: more inputs means smaller weights, so the signal keeps roughly its size through the whole network. In this problem you check that claim one layer at a time, for two different scaling rules.

Take a stack of fully connected layers with ReLU after every layer. layer_sizes =[n0,n1,…,nL]= [n_0, n_1, \dots, n_L]=[n0​,n1​,…,nL​]: there are n0n_0n0​ input features, and layer ℓ\ellℓ (for ℓ=1,…,L\ell = 1, \dots, Lℓ=1,…,L) has nℓn_\ellnℓ​ neurons, each receiving all nℓ−1n_{\ell-1}nℓ−1​ values from the layer below. Biases start at zero.

Every weight in layer ℓ\ellℓ is drawn independently at random with mean 000 and spread (standard deviation) σℓ\sigma_\ellσℓ​, set by scheme:

schemespread of layer ℓ\ellℓ's weights
"he"σℓ=2nℓ−1\sigma_\ell = \sqrt{\dfrac{2}{n_{\ell-1}}}σℓ​=nℓ−1​2​​
"xavier"σℓ=2nℓ−1+nℓ\sigma_\ell = \sqrt{\dfrac{2}{n_{\ell-1} + n_\ell}}σℓ​=nℓ−1​+nℓ​2​​

How the signal's size changes. A neuron in layer ℓ\ellℓ computes z=w1a1+w2a2+…z = w_1 a_1 + w_2 a_2 + \dotsz=w1​a1​+w2​a2​+… over its nℓ−1n_{\ell-1}nℓ−1​ inputs. Treat the terms as independent. Each weight has mean 000, so

Var(z)=nℓ−1 σℓ2 mℓ−1\text{Var}(z) = n_{\ell-1}\, \sigma_\ell^{2}\, m_{\ell-1}Var(z)=nℓ−1​σℓ2​mℓ−1​

where mℓ−1m_{\ell-1}mℓ−1​ is the mean square of the values arriving at layer ℓ\ellℓ, meaning the average of a2a^2a2:

  • The raw input features have mean 000 and spread sss (input_spread), so m0=s2m_0 = s^2m0​=s2.
  • A layer's zzz is symmetric around 000, and its ReLU zeroes the negative half. The values it passes up therefore have mean square mℓ=12 Var(z)m_\ell = \tfrac{1}{2}\,\text{Var}(z)mℓ​=21​Var(z).

Task: write signal_spread(layer_sizes, scheme, input_spread), returning [weight_spreads, signal_spreads]. Each list has one entry per layer, ℓ=1\ell = 1ℓ=1 to LLL:

  • weight_spreads: σℓ\sigma_\ellσℓ​,
  • signal_spreads: the spread Var(z)\sqrt{\text{Var}(z)}Var(z)​ of layer ℓ\ellℓ's values before its ReLU.

Round every number to 4 decimal places. layer_sizes holds at least two positive whole numbers, scheme is "he" or "xavier", and input_spread is positive.

Worked through, signal_spread([6, 3, 3], "xavier", 1.0):

  • Layer 1: σ1=2/9=0.4714\sigma_1 = \sqrt{2/9} = 0.4714σ1​=2/9​=0.4714. Var(z)=6×29×1=1.3333\text{Var}(z) = 6 \times \tfrac{2}{9} \times 1 = 1.3333Var(z)=6×92​×1=1.3333, so the spread is 1.15471.15471.1547. The ReLU passes up m1=0.6667m_1 = 0.6667m1​=0.6667.
  • Layer 2: σ2=2/6=0.5774\sigma_2 = \sqrt{2/6} = 0.5774σ2​=2/6​=0.5774. Var(z)=3×13×0.6667=0.6667\text{Var}(z) = 3 \times \tfrac{1}{3} \times 0.6667 = 0.6667Var(z)=3×31​×0.6667=0.6667, so the spread is 0.81650.81650.8165.
  • The answer is [[0.4714, 0.5774], [1.1547, 0.8165]].

Every layer multiplies the signal's variance by some factor, and a deep network applies its factor again at every layer. This is the compounding from Chapter 3, and it happens before a single gradient has been computed. A factor of 0.90.90.9 looks harmless for one layer. Over fifty layers it leaves 0.950≈0.0050.9^{50} \approx 0.0050.950≈0.005 of the variance. Run a deep stack under both schemes and compare the second lists.

1
2
3
4
5
6
7
weights × 4layer 7: 1.6e+4

Weights too large: the signal grows at every layer until it overflows. The same compounding you saw with exploding gradients.

Examples

layer_sizes = [4, 4, 4, 4]
scheme = "he"
input_spread = 1.0
returns [[0.7071, 0.7071, 0.7071], [1.4142, 1.4142, 1.4142]]
layer_sizes = [100, 50, 50, 10]
scheme = "xavier"
input_spread = 1.0
returns [[0.1155, 0.1414, 0.1826], [1.1547, 0.8165, 0.7454]]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run