5.3  Initialization

Every neural network we have trained so far started from random weights, usually drawn by the framework’s default initializer. Yet Section 4.4 showed just how unforgiving that starting point can be: if each layer slightly amplifies or attenuates activations, a deep stack compounds the factor exponentially, stalling training before it even begins.

That section also derived the remedy for a single layer: draw weights with standard deviation \(g/\sqrt{n_\textrm{in}}\), where \(n_\textrm{in}\) is the layer’s input width and the gain \(g\) compensates for the activation function that follows: \(g = \sqrt{2}\) before a ReLU (He initialization, Section 4.4.2.2) and \(g = 1\) for a linear output. Under that rule, each layer preserves the mean square of its input.

You might expect that applying this rule to every layer settles initialization once and for all. Surprisingly, it does not. In a residual network, activations still explode with depth even when every individual layer obeys He initialization. Each residual block adds the output of its body \(f\) (Section 5.1) to its input. If the body preserves the input mean square and produces an output uncorrelated with the input, their sum has double the mean square. Across 32 residual blocks, the activation standard deviation balloons by \(2^{16} \approx 65{,}000\) before a single gradient step is taken.

Sound initialization therefore depends on the architecture’s wiring, not just individual layer types, and we need clean mechanisms for applying custom rules across a module tree. The first half of this section covers the mechanics: applying initializers across an entire module tree, bounding extreme draws with truncated normals, writing custom initializers, and editing weights in place. Slow down for the second half, where we analyze variance growth across residual blocks and fix it by scaling down or zeroing each block’s output projection.

5.3.1 Applying Initializers

As we saw in Section 4.4, default linear-layer initializers do not apply He initialization (Section 4.4.2.2). To apply He initialization (or the depth-dependent residual scaling we derive shortly) across an entire model, we walk the module tree (Section 5.1). Below we compare default versus He initialization on a 10-layer ReLU stack of width 256 (without biases) fed 1,024 standard-normal inputs.

PyTorch’s default draws from \(U(-1/\sqrt{n_\textrm{in}}, 1/\sqrt{n_\textrm{in}})\) (one-sixth of He’s variance). net.apply(init_he) invokes init_he on every submodule, using the in-place nn.init.kaiming_normal_ (trailing underscore) so existing nn.Parameter references remain intact:

Keras defaults to Glorot uniform. To reinitialize an existing Keras model, we loop over model.layers and assign a fresh draw in place—instantiating a new initializer_class() per layer because an unseeded Keras initializer instance replays the same numbers if reused:

NNX defaults to LeCun normal (half of He’s variance). We walk nnx.iter_modules(model), split a fresh random key per nnx.Linear layer, and update layer.kernel[...] in place with nnx.initializers.he_normal():

Gluon’s default draws from \(U(-0.07, 0.07)\). net.initialize(init.MSRAPrelu(factor_type='in', slope=0), force_reinit=True) applies He initialization (\(g = \sqrt{2}\)) across the block tree, overwriting existing weights:

net = nn.Sequential(*[module for _ in range(10) for module in
                      (nn.Linear(256, 256, bias=False), nn.ReLU())])
X = torch.randn(1024, 256)
print(f'default: mean square of the output {net(X).pow(2).mean():.2g}')

def init_he(module):
    if isinstance(module, nn.Linear):
        nn.init.kaiming_normal_(module.weight, nonlinearity='relu')

net.apply(init_he)
print(f'He:      mean square of the output {net(X).pow(2).mean():.2g}')
default: mean square of the output 1.9e-08
He:      mean square of the output 1.1
net = tf.keras.Sequential([
    tf.keras.layers.Dense(256, activation='relu', use_bias=False)
    for _ in range(10)])
X = tf.random.normal((1024, 256))
print(f'default: mean square of the output '
      f'{float(tf.reduce_mean(net(X) ** 2)):.2g}')

def reinit(model, initializer_class):
    for layer in model.layers:
        if isinstance(layer, tf.keras.layers.Dense):
            init = initializer_class()   # one object per layer
            layer.kernel.assign(init(layer.kernel.shape))

reinit(net, tf.keras.initializers.HeNormal)
print(f'He:      mean square of the output '
      f'{float(tf.reduce_mean(net(X) ** 2)):.2g}')
default: mean square of the output 0.0012
He:      mean square of the output 0.66
rngs = nnx.Rngs(d2l.get_key())
net = nnx.Sequential(*[module for _ in range(10) for module in
                       (nnx.Linear(256, 256, use_bias=False, rngs=rngs),
                        nnx.relu)])
X = jax.random.normal(d2l.get_key(), (1024, 256))
print(f'default: mean square of the output {(net(X) ** 2).mean():.2g}')

def reinit(model, key, init_fn):
    layers = [m for _, m in nnx.iter_modules(model)
              if isinstance(m, nnx.Linear)]
    for layer, subkey in zip(layers, jax.random.split(key, len(layers))):
        layer.kernel[...] = init_fn(
            subkey, layer.kernel.shape, layer.kernel.dtype)

reinit(net, d2l.get_key(), nnx.initializers.he_normal())
print(f'He:      mean square of the output {(net(X) ** 2).mean():.2g}')
default: mean square of the output 0.00095
He:      mean square of the output 0.56
net = nn.Sequential()
for _ in range(10):
    net.add(nn.Dense(256, activation='relu', use_bias=False, in_units=256))
net.initialize()
X = mnp.random.normal(size=(1024, 256))
print(f'default: mean square of the output {float((net(X) ** 2).mean()):.2g}')

net.initialize(init.MSRAPrelu(factor_type='in', slope=0), force_reinit=True)
print(f'He:      mean square of the output {float((net(X) ** 2).mean()):.2g}')
default: mean square of the output 2.2e-07
He:      mean square of the output 1.1

Under the default initializer, the output mean square collapses by orders of magnitude because every layer multiplies variance by a factor strictly less than one. Switching to He initialization brings the output mean square right back to within roughly a factor of two of 1 (the input mean square), with the remaining spread coming simply from random finite-width fluctuations compounding across ten layers.

This pattern is the whole interface. Later chapters package it as helper functions, such as init_cnn for convolutional networks, but each is a function like init_he handed to apply.

5.3.1.1 Truncated Normals

A standard Gaussian gets the variance right, but its tails extend to infinity. For a tiny layer that rarely matters; across the \(10^8\) weights of a large model, however, dozens of entries are virtually guaranteed to land beyond five standard deviations. To avoid extreme initial outliers, modern language and vision models such as BERT and the Vision Transformer draw from a truncated normal that rejects and redraws samples outside a fixed multiple of \(\sigma\) (Devlin et al. 2018; Dosovitskiy et al. 2021). Clipping the tails also trims the variance slightly, which some framework implementations compensate for by rescaling and others leave as is.

nn.init.trunc_normal_ takes absolute cutoffs a and b (defaulting to \(\pm 2\)), so truncating at \(\pm 2\sigma\) for std=0.02 requires passing a=-0.04, b=0.04:

tf.keras.initializers.TruncatedNormal truncates at \(\pm 2\sigma\) without rescaling (whereas HeNormal truncates and rescales to hit target variance):

nnx.initializers.truncated_normal takes cutoffs in multiples of \(\sigma\) (lower=-2.0, upper=2.0 by default):

Because mxnet.init has no built-in truncated normal, we redraw any entries outside \(\pm 2\sigma\) in a loop:

w = torch.empty(1000, 1000)
nn.init.normal_(w, std=0.02)
print(f'normal:    std {w.std():.4f}, max weight {w.abs().max():.4f}')
nn.init.trunc_normal_(w, std=0.02, a=-0.04, b=0.04)
print(f'truncated: std {w.std():.4f}, max weight {w.abs().max():.4f}')
normal:    std 0.0200, max weight 0.0998
truncated: std 0.0176, max weight 0.0400
w = tf.keras.initializers.RandomNormal(stddev=0.02)((1000, 1000)).numpy()
print(f'normal:    std {w.std():.4f}, max weight {abs(w).max():.4f}')
w = tf.keras.initializers.TruncatedNormal(stddev=0.02)((1000, 1000)).numpy()
print(f'truncated: std {w.std():.4f}, max weight {abs(w).max():.4f}')
normal:    std 0.0200, max weight 0.0977
truncated: std 0.0176, max weight 0.0400
w = nnx.initializers.normal(stddev=0.02)(d2l.get_key(), (1000, 1000))
print(f'normal:    std {w.std():.4f}, max weight {jnp.abs(w).max():.4f}')
w = nnx.initializers.truncated_normal(stddev=0.02)(d2l.get_key(), (1000, 1000))
print(f'truncated: std {w.std():.4f}, max weight {jnp.abs(w).max():.4f}')
normal:    std 0.0200, max weight 0.0957
truncated: std 0.0176, max weight 0.0400
def trunc_normal(sigma, shape):
    w = mnp.random.normal(0, sigma, shape)
    tails = mnp.abs(w) > 2 * sigma
    while bool(tails.any()):
        w = mnp.where(tails, mnp.random.normal(0, sigma, shape), w)
        tails = mnp.abs(w) > 2 * sigma
    return w

w = mnp.random.normal(0, 0.02, (1000, 1000))
print(f'normal:    std {float(w.std()):.4f}, '
      f'max weight {float(mnp.abs(w).max()):.4f}')
w = trunc_normal(0.02, (1000, 1000))
print(f'truncated: std {float(w.std()):.4f}, '
      f'max weight {float(mnp.abs(w).max()):.4f}')
normal:    std 0.0200, max weight 0.0992
truncated: std 0.0176, max weight 0.0400

Out of one million standard normal draws with \(\sigma = 0.02\), extreme weights approach \(5\sigma = 0.1\), whereas the truncated normal guarantees \(|w| \leq 0.04\). Clipping the tails lowers the empirical standard deviation from \(0.02\) to roughly \(0.0176\). Fixed-\(\sigma\) recipes like BERT simply accept that slight reduction, while variance-preserving schemes rescale after truncation to hit the exact target variance.

5.3.2 Custom Initializers and Direct Edits

Whenever built-in initializers do not match what a paper or architecture calls for, we can write our own initializer or edit parameter tensors directly. Both build on each framework’s initializer interface:

In PyTorch, an initializer is a function that overwrites a parameter in place.

In Keras, an initializer is an object that maps (shape, dtype) to a tensor: subclass tf.keras.initializers.Initializer and implement __call__. Layers then accept an instance through their initializer arguments.

In NNX, an initializer is any function with the signature (key, shape, dtype) -> array.

In Gluon, an initializer is a subclass of init.Initializer whose _init_weight method fills the array it is handed, in place.

5.3.2.1 A Rule with a Gain of Our Choice

We now implement the gain-parameterized normal rule from the introduction: draw weights with standard deviation \(g/\sqrt{n_\textrm{in}}\) for any chosen gain \(g\). He initialization corresponds to \(g = \sqrt{2}\), and our residual-network experiments below will also test \(g = 1\), \(g = 1/\sqrt{N}\) (for \(N\) blocks), and \(g = 0\) on each block’s output projection. Writing one reusable initializer handles all four cases cleanly.

The function follows the convention of torch.nn.init: it overwrites the tensor it is given, and its name ends in an underscore. A PyTorch weight has shape \((n_\textrm{out}, n_\textrm{in})\):

The subclass stores the gain. A Keras kernel has shape \((n_\textrm{in}, n_\textrm{out})\):

A function of the gain returns the initializer. An NNX kernel has shape \((n_\textrm{in}, n_\textrm{out})\):

The subclass stores the gain. A Gluon weight has shape \((n_\textrm{out}, n_\textrm{in})\):

def scaled_normal_(weight, gain):
    nn.init.normal_(weight, std=gain / math.sqrt(weight.shape[1]))
class ScaledNormal(tf.keras.initializers.Initializer):
    def __init__(self, gain):
        self.gain = gain

    def __call__(self, shape, dtype=None):
        std = self.gain / math.sqrt(shape[0])
        return std * tf.random.normal(shape, dtype=dtype or tf.float32)
def scaled_normal(gain):
    def init(key, shape, dtype=jnp.float32):
        std = gain / math.sqrt(shape[0])
        return std * jax.random.normal(key, shape, dtype)
    return init
class ScaledNormal(init.Initializer):
    def __init__(self, gain):
        super().__init__()
        self.gain = gain

    def _init_weight(self, name, data):
        std = self.gain / math.sqrt(data.shape[1])
        data[:] = std * mnp.random.normal(size=data.shape)

5.3.2.2 Direct Edits

Occasionally we want to modify parameter values directly, either right after initialization or after loading weights. Because ReLU is positively homogeneous, halving the final layer’s weights in our ten-layer stack halves every output activation and should therefore reduce the output mean square by a factor of four.

Wrap in-place edits on leaf nn.Parameter tensors in with torch.no_grad(): so autograd does not reject them.

w.assign(0.5 * w) updates the tf.Variable in place outside any GradientTape.

kernel[...] *= 0.5 mutates the array inside the tracked nnx.Param variable.

weight.data()[:] *= 0.5 edits the underlying array in place outside autograd.record().

before = net(X).pow(2).mean()
with torch.no_grad():
    net[-2].weight *= 0.5
print(f'{before:.2g} before, {net(X).pow(2).mean():.2g} after')
1.1 before, 0.28 after
before = float(tf.reduce_mean(net(X) ** 2))
w = net.layers[-1].kernel
w.assign(0.5 * w)
print(f'{before:.2g} before, {float(tf.reduce_mean(net(X) ** 2)):.2g} after')
0.66 before, 0.16 after
before = (net(X) ** 2).mean()
net.layers[-2].kernel[...] *= 0.5
print(f'{before:.2g} before, {(net(X) ** 2).mean():.2g} after')
0.56 before, 0.14 after
before = float((net(X) ** 2).mean())
net[-1].weight.data()[:] *= 0.5
print(f'{before:.2g} before, {float((net(X) ** 2).mean()):.2g} after')
1.1 before, 0.28 after

Sure enough, the output mean square drops to exactly one-quarter of its prior value.

5.3.3 Initialization of Residual Networks

5.3.3.1 Variance Growth across Blocks

Earlier we claimed that a deep residual stack explodes at initialization even when every individual layer follows He’s rule. To prove it, note that a residual block computes \(\mathbf{x}_{k+1} = \mathbf{x}_k + f_k(\mathbf{x}_k)\). Take the two-layer body from Section 5.1, expanded fourfold in the middle as in many modern residual blocks and transformers: a linear layer from width \(d\) to \(4d\), a ReLU, and a linear output projection from \(4d\) back to \(d\). All biases start at zero. The first layer uses He initialization (\(g = \sqrt{2}\)) because a ReLU follows it, while the output projection uses standard deviation \(g/\sqrt{4d}\) for a gain \(g\) that we leave free (\(g = 1\) being the standard linear-layer rule from Section 4.4).

Figure 5.3.1: The mean square of the signal at each point of one residual block. The He layer doubles \(m_k\), the ReLU halves it again, and the output projection multiplies it by \(g^2\). The identity path carries \(m_k\) around the body, and the two mean squares add at the sum.

Let \(m_k\) denote the mean square of the entries of \(\mathbf{x}_k\); Figure 5.3.1 follows it through the block. Passing through the first linear layer doubles \(m_k\), and the ReLU halves it back to \(m_k\) (Section 4.4.2.2). The output projection then scales the mean square by \(4d \cdot g^2/(4d) = g^2\), so the body output \(f_k(\mathbf{x}_k)\) has mean square \(g^2 m_k\). Now comes the crucial step: because the output projection’s weights are zero-mean and independent of \(\mathbf{x}_k\), the body output \(f_k(\mathbf{x}_k)\) is uncorrelated with the skip signal \(\mathbf{x}_k\). Their mean squares therefore add:

\[m_{k+1} = m_k + g^2 m_k = (1 + g^2)\, m_k. \tag{5.3.1}\]

Every block inflates the activation mean square by \(1 + g^2 > 1\) whenever \(g \neq 0\). At \(g = 1\), each block doubles the variance, so across \(N\) blocks the activation standard deviation grows by \(2^{N/2}\): \(2\times\) after 2 blocks, \(16\times\) after 8 blocks, and \(65{,}536\times\) after 32 blocks. Every single layer respected the single-layer variance rule, yet the network as a whole blew up because that local rule is blind to the skip connection adding the input back at every block.

5.3.3.2 Scaling Down the Output Projection

Across \(N\) blocks, the per-block factor compounds to \((1 + g^2)^N\). Keeping activations well scaled at arbitrary depth therefore requires shrinking \(g\) as \(N\) grows. Choosing \(g^2 = 1/N\), scaling every body’s output projection by \(1/\sqrt{N}\), bounds the compounding cleanly:

\[m_N = \left(1 + \frac{1}{N}\right)^N m_0 \leq e\, m_0, \tag{5.3.2}\]

because \((1 + 1/N)^N\) increases monotonically to \(e\). The activation standard deviation now grows by at most \(\sqrt{e} \approx 1.65\) regardless of depth (\(1.5\) after 2 blocks, \(1.60\) after 8, and \(1.64\) after 32). The same \(1/\sqrt{N}\) scaling is widely used in practice: GPT-2 (Radford et al. 2019), for example, shrinks the initial standard deviation of the output projection of every residual branch by \(1/\sqrt{N}\), where \(N\) is the total number of residual branches in the network. Its blocks normalize their inputs, so the growth is additive rather than multiplicative (see the exercises), and the same scaling keeps the sum bounded. Fixup (Zhang et al. 2019) derives a depth-dependent scaling of this kind for residual networks that have no normalization layers at all.

5.3.3.3 Zero Output Projections

What if we push this idea to its limit and set \(g = 0\)? At first glance, zero initialization sounds dangerous: zeroing every weight traps all hidden units in identical symmetry forever (Section 4.4). Zeroing only the output projection of each residual body is completely different. At step zero, every body outputs zero, so every block acts as an exact identity map and the entire network starts out cleanly behaved while depth engages gradually during training. Symmetry never freezes learning because the body’s first layer retains its random weights: on the first backward pass, the output projection receives a nonzero gradient (since its ReLU inputs are nonzero), moving away from zero immediately, and from the second step onward gradients flow freely into the first layer as well. Figure 5.3.2 lays out the three stages and contrasts them with the all-zeros trap.

Figure 5.3.2: Zeroing only the output projection \(W_{\mathrm{out}}\) of a residual body. In the first forward pass the body outputs \(f = 0\), so the block is the identity, but the hidden activations \(\mathbf{h}\) are nonzero. The first backward pass therefore gives \(W_{\mathrm{out}}\) the nonzero gradient \(\boldsymbol{\delta}\mathbf{h}^\top\), where \(\boldsymbol{\delta}\) is the gradient arriving at the body output, while \(W_{\mathrm{in}}\) receives none. From the next step on both layers train. Bottom row: with both layers zeroed, \(\mathbf{h} = 0\) and neither layer ever receives a gradient.

Zero-initialized branches have a clear lineage. Goyal et al. (2017) zeroed the final scale factor of each residual block to train ResNets with large minibatches. Fixup combines zero output projections with its depth-dependent scaling. Later work replaces the zero matrix with a single learnable scalar per branch that starts at zero (De and Smith 2020; Bachlechner et al. 2021).

5.3.3.4 Measuring Variance across Depth

We now test all three predictions empirically. WideResidualBlock implements our \(d \to 4d \to d\) residual block using our custom gain-scaled normal initializer, with gain \(\sqrt{2}\) on the expansion layer and a configurable out_gain on the output projection:

class WideResidualBlock(nn.Module):
    def __init__(self, d, out_gain):
        super().__init__()
        self.body = nn.Sequential(nn.Linear(d, 4 * d), nn.ReLU(),
                                  nn.Linear(4 * d, d))
        for layer, gain in ((self.body[0], math.sqrt(2)),
                            (self.body[2], out_gain)):
            scaled_normal_(layer.weight, gain)
            nn.init.zeros_(layer.bias)

    def forward(self, X):
        return X + self.body(X)
class WideResidualBlock(tf.keras.layers.Layer):
    def __init__(self, d, out_gain, **kwargs):
        super().__init__(**kwargs)
        self.body = tf.keras.Sequential([
            tf.keras.layers.Dense(
                4 * d, activation='relu',
                kernel_initializer=ScaledNormal(math.sqrt(2))),
            tf.keras.layers.Dense(
                d, kernel_initializer=ScaledNormal(out_gain))])

    def call(self, X):
        return X + self.body(X)
class WideResidualBlock(nnx.Module):
    def __init__(self, d, out_gain, *, rngs):
        self.body = nnx.Sequential(
            nnx.Linear(d, 4 * d, kernel_init=scaled_normal(math.sqrt(2)),
                       rngs=rngs),
            nnx.relu,
            nnx.Linear(4 * d, d, kernel_init=scaled_normal(out_gain),
                       rngs=rngs))

    def __call__(self, X):
        return X + self.body(X)
class WideResidualBlock(nn.Block):
    def __init__(self, d, out_gain):
        super().__init__()
        self.body = nn.Sequential()
        self.body.add(
            nn.Dense(4 * d, activation='relu',
                     weight_initializer=ScaledNormal(math.sqrt(2))),
            nn.Dense(d, weight_initializer=ScaledNormal(out_gain)))

    def forward(self, X):
        return X + self.body(X)

Next, std_across_depth builds a stack of num_blocks blocks of width 64, feeds in 256 \(\mathcal{N}(0, 1)\) inputs, and tracks the activation standard deviation at the input and after every block:

def std_across_depth(num_blocks, out_gain):
    net = nn.Sequential(*[WideResidualBlock(64, out_gain)
                          for _ in range(num_blocks)])
    X = torch.randn(256, 64)
    stds = [X.std().item()]
    with torch.no_grad():
        for block in net:
            X = block(X)
            stds.append(X.std().item())
    return stds
def std_across_depth(num_blocks, out_gain):
    net = tf.keras.Sequential([WideResidualBlock(64, out_gain)
                               for _ in range(num_blocks)])
    X = tf.random.normal((256, 64))
    stds = [float(tf.math.reduce_std(X))]
    for block in net.layers:
        X = block(X)
        stds.append(float(tf.math.reduce_std(X)))
    return stds
def std_across_depth(num_blocks, out_gain):
    rngs = nnx.Rngs(d2l.get_key())
    net = nnx.Sequential(*[WideResidualBlock(64, out_gain, rngs=rngs)
                           for _ in range(num_blocks)])
    X = jax.random.normal(rngs(), (256, 64))
    stds = [float(X.std())]
    for block in net.layers:
        X = block(X)
        stds.append(float(X.std()))
    return stds
def std_across_depth(num_blocks, out_gain):
    net = nn.Sequential()
    net.add(*[WideResidualBlock(64, out_gain) for _ in range(num_blocks)])
    net.initialize()  # deferred: the draws happen at the forward pass
    X = mnp.random.normal(0, 1, (256, 64))
    stds = [float(X.std())]
    for block in net:
        X = block(X)
        stds.append(float(X.std()))
    return stds

We compare all three gain schedules: unscaled (\(g = 1\)), scaled (\(g = 1/\sqrt{N}\)), and zero (\(g = 0\)), printing final standard deviations for \(N = 2, 8, 32\) and plotting the full depth profile at \(N = 32\):

gains = {'unscaled': lambda n: 1.0, 'scaled': lambda n: n ** -0.5,
         'zero': lambda n: 0.0}
profiles = {n: [std_across_depth(n, gain(n)) for gain in gains.values()]
            for n in (2, 8, 32)}
print(f'{"N":>3}' + ''.join(f'{name:>10}' for name in gains))
for n, stds in profiles.items():
    print(f'{n:>3}' + ''.join(f'{s[-1]:>10.3g}' for s in stds))
d2l.plot(list(range(33)), profiles[32], 'blocks', 'std of activations',
         legend=list(gains), yscale='log', figsize=(4.5, 3))
  N  unscaled    scaled      zero
  2      2.04      1.49     0.996
  8      13.8      1.55     0.988
 32  5.24e+04      1.57      1.01

  N  unscaled    scaled      zero
  2      2.05      1.53         1
  8      14.1      1.61     0.998
 32  4.23e+04      1.67         1

  N  unscaled    scaled      zero
  2      2.06      1.48      1.01
  8      18.6      1.59     0.997
 32  3.13e+04      1.73     0.994

  N  unscaled    scaled      zero
  2      2.08      1.47      1.01
  8      18.1      1.56     0.988
 32  2.86e+04      1.59         1

The empirical curves confirm our analysis. The unscaled stack grows by roughly \(\sqrt{2}\) per block (a straight line on the log scale), reaching anywhere from about 20,000 to 200,000 after 32 blocks (most individual runs sit slightly below the theoretical \(\sqrt{\mathbb{E}[m_{32}]} = 65{,}536\) because multiplying random positive factors produces a right-skewed distribution whose median falls below the square root of its mean). With initial activations already in the tens of thousands, there is little point even attempting to train the unscaled stack. By contrast, the \(1/\sqrt{N}\)-scaled stack stays comfortably between 1.4 and 1.9 at every depth, matching our 1.5–1.65 prediction, while zeroing the output projections holds the standard deviation flat at 1 across all 32 blocks.

Running a quick forward-pass diagnostic like this catches broken initializations immediately before wasting GPU time on training. Notice, though, that it cannot tell you whether \(1/\sqrt{N}\) scaling or zero initialization will optimize faster once training begins: both preserve signal scale at step zero, differing only in whether the residual branches start active or wake up over the first few updates.

Finally, remember that initialization controls only step zero. As gradient steps update the weights, nothing in initialization alone guarantees that activations stay well scaled, which is where normalization layers (Section 7.3) step in.

5.3.4 Summary

Framework default initializers do not use He initialization, so deep networks without normalization layers require explicit initialization (applied via constructor arguments, tree-wide callbacks, or direct in-place edits), and truncated normals prevent rare extreme outliers in large models. Crucially, per-layer variance rules (Section 4.4) are not enough for residual architectures: adding each block’s body back to its input multiplies the activation mean square by \(1 + g^2\), causing \(g = 1\) to blow up by \(\sqrt{2}\) per block. Scaling each block’s output projection by \(1/\sqrt{N}\) caps activation growth at \(\sqrt{e}\) for any depth \(N\), while zeroing the output projection starts every block as the identity without trapping the network in symmetry.

5.3.5 Exercises

  1. Truncated normal. For a normal distribution truncated at \(\pm 2\sigma\): what fraction of draws does the cut discard, and by how much does it shrink the standard deviation? Neither quantity has an elementary closed form; express both in terms of the standard normal density and distribution function and evaluate them using tabulated values. Verify the standard deviation against the printed output of the demonstration in Section 5.3.1.1, and the discarded fraction by counting the entries of its plain normal draw that lie beyond \(\pm 2\sigma\).

  2. [code] Normalized inputs. Many residual networks normalize the input of each body: the body reads \(\mathbf{x}_k / r_k\), where \(r_k\) is the root mean square of the entries of \(\mathbf{x}_k\) for each example, instead of \(\mathbf{x}_k\).

    1. Show that the body output then has mean square \(g^2\), whatever the scale of \(\mathbf{x}_k\), and that with \(m_0 = 1\) the mean square after \(N\) blocks is \(1 + N g^2\). How does the growth with depth compare with the unnormalized blocks of this section?
    2. Which gain keeps the mean square at the end of the stack equal to 2 for every \(N\)? Compare it with the scaling of GPT-2.
    3. Add the normalization to WideResidualBlock, measure the standard deviation across depth for \(g = 1\) and \(g = 1/\sqrt{N}\) at \(N = 32\), and compare with your predictions and with the measured plot in Section 5.3.3.4.
  3. [code] Kaiming initialization. Implement the Kaiming (He) initializers of Section 4.4.2.2, uniform and normal, with an explicit nonlinearity argument that switches the gain between 'relu' and 'linear'.

    1. Check both on the weight of a layer with 512 inputs and 256 outputs by mean, standard deviation, and range: the normal initializer against the scaled normal initializer of this section, and the uniform initializer against its predicted bound and standard deviation. Explain any difference in range.
    2. Apply your normal initializer to the stack of Section 5.3.3.4 with the 'relu' gain on both layers of every block. Predict the factor by which each block multiplies the mean square, and check it against a measured curve added to the plot of Section 5.3.3.4.

    Adapted from CMU 10-714, Homework 2 and Homework 4.

  4. [code] Gradients across depth. ● The backward pass through a block multiplies the gradient by the transposed Jacobian of \(\mathbf{x} \mapsto \mathbf{x} + f(\mathbf{x})\).

    1. Repeat the argument of this section for the backward pass. Show that the body multiplies the mean square of the gradient by \(g^2\), although the backward pass sums over the outputs of each layer rather than its inputs, so that each block again multiplies it by \(1 + g^2\).
    2. Verify the prediction for the three choices of gain at \(N = 32\): compute the gradient of the sum of the outputs of the stack with respect to its input, and compare its mean square with that of the gradient at the output.
  5. [code] Data-driven calibration. ● Initialize the stack of Section 5.3.3.4 with the unscaled choice of gain, then calibrate it block by block on one minibatch of unit-variance inputs. For each block in turn, run the minibatch forward to that block, measure the standard deviation of its body output \(f_k(\mathbf{x}_k)\), and rescale the block’s output projection so that this standard deviation becomes \(1/\sqrt{N}\). This adapts the layer-sequential unit-variance initialization of Mishkin and Matas (2016) to a residual stack.

    1. Explain why the blocks must be calibrated in order rather than all at once from a single forward pass.
    2. Explain why the calibration targets the body output rather than the block output \(\mathbf{x}_k + f_k(\mathbf{x}_k)\).
    3. Predict the standard deviation at the end of the calibrated stack. Plot the calibrated depth-versus-standard-deviation curve next to that of the scaled choice of gain, and explain the difference.

Discussions