AI Logic

Weight Initialization

SkipInit

Updated:

SkipInit residual blocks. The top shows the original block with BN normalization layers (BN, ReLU, and 3×3 convolution repeated twice, then added to the shortcut). The middle shows the SkipInit block, which removes BN and multiplies the end of the branch by a scalar α initialized to 0. The bottom describes how the branch's contribution differs with BN, without normalization, and with SkipInit

Background and overview

SkipInit is a method that multiplies the end of the branch of each residual block in a residual network by a single learnable scalar α\alpha and starts training from α=0\alpha = 0. It makes deep residual networks trainable even with normalization layers such as batch normalization removed. It was proposed in 2020 by Soham De and Samuel L. Smith (DeepMind) (NeurIPS 2020).

The paper's main subject is the question "why does batch normalization make deep residual networks trainable?" The paper showed that at initialization, batch normalization makes the output of the residual branch smaller than the shortcut by roughly the square root of the depth. As a result, residual blocks in deep networks are close to the identity map, passing their input through almost unchanged, at the start of training.

SkipInit was designed to test whether this explanation is correct. If shrinking the branch is the main benefit of batch normalization, it should be possible to train deep residual networks simply by removing the normalization layers and multiplying the branches by a small number. Indeed, a 1,000-layer Wide-ResNet was trained on CIFAR-10 without normalization layers.

The top of the figure shows the original block with batch normalization (BN), and the middle shows the SkipInit block. SkipInit simply removes BN and adds one α\alpha at the end of the branch.

Theory (how batch normalization shrinks the branch)

This section walks through the analysis in Section 2.1 of the paper. xℓx_\ell is the input to the ℓ\ell-th residual block, and fℓf_\ell is the function computed by its branch.

  1. In a residual block, the variances of the main path and the branch add up: The output of a residual block is xℓ+1=xℓ+fℓ(xℓ)x_{\ell+1} = x_\ell + f_\ell(x_\ell). With weights initialized to mean 0, the branch output and the main path are uncorrelated, so their variances add. Var(xℓ+1)=Var(xℓ)+Var(fℓ(xℓ))\mathrm{Var}(x_{\ell+1}) = \mathrm{Var}(x_\ell) + \mathrm{Var}(f_\ell(x_\ell))
  2. Without normalization, the variance doubles at every block: With He initialization, the variance of the branch output equals the variance of its input, so Var(xℓ+1)=2 Var(xℓ)\mathrm{Var}(x_{\ell+1}) = 2\,\mathrm{Var}(x_\ell), i.e., it grows as 2ℓ2^\ell. Moreover, the branch and the main path affect the output equally, so each block is far from the identity map.
  3. With batch normalization, the variance of the branch output is fixed at 1: Because the input is normalized inside the branch, Var(fℓ)≈1\mathrm{Var}(f_\ell) \approx 1. The main-path variance then grows only by 1 at a time, giving Var(xℓ)≈ℓ\mathrm{Var}(x_\ell) \approx \ell.
  4. The branch's contribution shrinks to 1/(ℓ+1)1/(\ell+1): The main path has variance ℓ\ell while the branch has variance 1, so the branch accounts for only 1/(ℓ+1)1/(\ell+1) of the variance of the block's output. In other words, batch normalization shrinks the branch activations by a factor of ℓ\sqrt{\ell}. Since block depth is proportional to the total number of residual blocks dd in the network, the branch is shrunk by a factor of roughly d\sqrt{d}.
  5. Shrinking in the forward pass shrinks the backward gradients by the same amount: When the branch signal is scaled by α\alpha, the gradient flowing back through that branch is also scaled by α\alpha. As a result, gradients are less likely to blow up even in deep networks.

As an analogy, it is like a game of telephone in which each person adds "their own addition" to "the original story" before passing it on. If each addition is as large as the original story, the story is buried after dozens of people. If the additions are kept small, the original story carries far down the line.

The paper confirmed by measurement that, for a convolutional residual network on CIFAR-10 (ReLU and batch normalization), the variance of the shortcut at initialization grows almost proportionally to block depth (Figure 2).

The SkipInit procedure and the initial value of α

The SkipInit procedure is as follows.

  1. Remove all normalization layers from the residual network.
  2. Multiply the end of the branch of each residual block by a single learnable scalar α\alpha. The block output becomes xℓ+1=xℓ+αℓ fℓ(xℓ)x_{\ell+1} = x_\ell + \alpha_\ell\, f_\ell(x_\ell).
  3. Initialize α\alpha to 0 and train with the other layers kept at He initialization.

In the paper's experiments, deep networks could be trained if α\alpha was initialized to 1/d1/\sqrt{d} (dd is the number of residual blocks) or less, but not with α=1\alpha = 1. This agrees with the analysis that batch normalization shrinks the branch by a factor of about d\sqrt{d}. In practice, the paper recommends α=0\alpha = 0, because all blocks start as the identity map and there is no need to set a value according to depth.

Training proceeds even with α=0\alpha = 0. The gradient with respect to α\alpha is proportional to the branch output fℓ(xℓ)f_\ell(x_\ell), and the weights inside the branch are not 0, so the first update moves α\alpha away from 0, after which gradients begin to flow to the branch weights as well.

The paper also presents "Regularized SkipInit" with stronger regularization: SkipInit (α=0\alpha = 0) plus biases on all convolutions and a single Dropout right before the softmax. It is used to compensate for the regularizing effect that batch normalization has.

Experimental results

These are the results reported in the paper.

CIFAR-10, n-2 Wide-ResNet (test accuracy, higher is better. Minibatch 64, 200 epochs. "—" marks runs that could not be trained. Table 1)

Method16 layers100 layers1,000 layers
Batch normalization93.5%94.7%94.6%
SkipInit (α=1/d\alpha = 1/\sqrt{d})93.0%94.2%94.2%
SkipInit (α=0\alpha = 0)93.3%94.2%94.3%
SkipInit (α=1\alpha = 1)93.0%——
Block output × 1/21/\sqrt{2}92.4%88.9%—
SkipInit (α=0\alpha = 0, no L2 regularization)89.8%91.7%92.1%

ImageNet, ResNet-50-V2 (validation top-1 / top-5 accuracy, higher is better. 90 epochs. Table 2)

MethodBatch 256Batch 1,024Batch 4,096
Batch normalization75.0 / 92.2%74.9 / 92.1%74.9 / 91.9%
Fixup74.8 / 91.8%74.6 / 91.7%73.0 / 90.6%
SkipInit + biases74.9 / 91.9%74.6 / 91.8%70.8 / 89.2%
Fixup + Dropout75.8 / 92.5%75.6 / 92.5%74.8 / 91.8%
Regularized SkipInit75.6 / 92.4%75.5 / 92.5%72.7 / 90.7%
  • Robust to depth: SkipInit could be trained even at 1,000 layers, and the optimal learning rate barely depended on depth. Test accuracy was slightly below batch normalization at every depth, but the training loss was almost the same (Appendix D).
  • Equalizing the forward signal is not enough: Scaling the block output by 1/21/\sqrt{2} prevents activations from exploding, but could not train 1,000 layers. The key is making the branch smaller than the shortcut.
  • L2 regularization has no bearing on whether training is possible: In deep networks the L2 regularization term dominates the initial loss and the weights shrink abruptly, but 1,000 layers could still be trained with L2 regularization removed (with lower accuracy).
  • Falls short of batch normalization with large minibatches: On ImageNet it matched batch normalization at batch 256, but the gap widened at 4,096. On CIFAR-10 as well, SkipInit had a lower training loss than batch normalization for minibatches up to about 1,024, but test accuracy dropped with very large minibatches (Figure 3).
  • With small minibatches, adding regularization surpasses it: With very small minibatches, Regularized SkipInit beat batch normalization in test accuracy (Figure 4).

Practical caveats

SkipInit is meant for residual networks whose shortcuts are identity maps. It assumes a form like ResNet-V2 (pre-activation) or Wide-ResNet, where nothing follows the addition. On the original ResNet (V1), which applies ReLU after the addition, ImageNet top-1 accuracy was 70.0%, far below batch normalization (75.6%). Adding a single scalar bias after the addition brought it back to 75.2% (Appendix F, Table 7).

Keep the following points in mind as well.

  • For training with large minibatches, batch normalization has the advantage: The paper names as batch normalization's second benefit that it raises the upper limit of learning rates that can be used stably. This benefit matters with large minibatches, and SkipInit does not provide it.
  • Add regularization: Batch normalization also has a regularizing effect. SkipInit alone gives slightly lower test accuracy, so add separate regularization, as in Regularized SkipInit with Dropout and biases.
  • Do not put normalization layers on the shortcut: The paper reports that variants that do not make the branch smaller than the shortcut, such as adding normalization layers on the shortcut or scaling the whole block output by 1/21/\sqrt{2}, could not train 1,000 layers (Appendix D.2).
  • PyTorch has no built-in version: It only requires multiplying the end of the branch by one nn.Parameter(torch.zeros(())), so changing one line of the block definition implements it (see the implementation example below). Since there are no normalization layers, the output is the same under model.train() and model.eval() (unless Dropout is added).

Comparison with other methods

The figure lines up the residual branches of methods that train deep residual networks without normalization layers. From top to bottom: Fixup, SkipInit, ReZero, and T-Fixup; the small yellow boxes are learnable scalars.

Comparison of residual blocks without normalization. From top: Fixup (adds scalar biases and a multiplier, sets the last convolution to 0, and shrinks the others), SkipInit (multiplies the end of the branch by α initialized to 0), ReZero (multiplies any branch F by α initialized to 0), T-Fixup (adds no scalars and shrinks the attention and MLP weights according to depth)

MethodHow the branch is shrunkAdded parametersInitial branch outputMain target
Batch normalizationNormalizes the branch input with minibatch statistics (resulting in about 1/d1/\sqrt{d})Per-channel γ,β\gamma, \betaSmall but not 0CNNs
FixupLast layer 0, other layers × L−1/(2m−2)L^{-1/(2m-2)}Scalar multipliers and biases0Residual networks
SkipInitMultiplies the end of the branch by a scalar α\alpha (from 0)One scalar per block0ResNets with identity shortcuts
ReZeroMultiplies the end of the branch by a scalar α\alpha (from 0) and also removes normalization layersOne scalar per block0Transformer, ResNet, fully connected, etc.
T-FixupShrinks the attention and MLP weights according to depthNoneSmall but not 0Transformer

The paper says that ResNet-V2 can be trained with just one of Fixup's rules, either "set the last layer to 0" or "shrink the layers", because both make the branch activations small and play the same role as SkipInit. On ImageNet, SkipInit and Fixup had nearly the same accuracy with small minibatches, and Fixup was slightly better with large minibatches. ReZero is a method of nearly the same form proposed independently around the same time as SkipInit.

Rough guidelines for choosing:

  • To remove normalization layers from a ResNet, use SkipInit: It only adds one line to the block, with no need to set a depth-dependent value.
  • To train fast with large minibatches, keep batch normalization: The upper limit of stable learning rates is higher, and accuracy holds up better as the minibatch grows.
  • For Transformers, use ReZero or T-Fixup: The SkipInit paper experimented only with ResNets.

PyTorch example

Show code
python
import torch
import torch.nn as nn


# SkipInit residual block (the pre-activation Wide-ResNet basic block with BN removed)
class SkipInitBlock(nn.Module):
    def __init__(self, in_ch: int, out_ch: int, stride: int = 1):
        super().__init__()
        self.relu = nn.ReLU()
        # Regularized SkipInit adds biases to the convolutions (bias=True)
        self.conv1 = nn.Conv2d(in_ch, out_ch, 3, stride=stride, padding=1, bias=True)
        self.conv2 = nn.Conv2d(out_ch, out_ch, 3, padding=1, bias=True)
        # SkipInit: learnable scalar multiplied at the end of the branch, initialized to 0
        self.alpha = nn.Parameter(torch.zeros(()))
        self.shortcut = None
        if stride != 1 or in_ch != out_ch:
            # 1×1 conv shortcut only for blocks that change shape (identity otherwise)
            self.shortcut = nn.Conv2d(in_ch, out_ch, 1, stride=stride, bias=True)
        for conv in (self.conv1, self.conv2):
            nn.init.kaiming_normal_(conv.weight, nonlinearity="relu")  # He initialization
            nn.init.zeros_(conv.bias)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        out = self.relu(x)
        identity = x if self.shortcut is None else self.shortcut(out)
        out = self.conv1(out)
        out = self.conv2(self.relu(out))
        return identity + self.alpha * out  # x_{l+1} = x_l + α f_l(x_l)


class SkipInitWRN(nn.Module):
    def __init__(self, blocks_per_stage: int = 2, widen: int = 2, num_classes: int = 10, drop: float = 0.6):
        super().__init__()
        widths = [16 * widen, 32 * widen, 64 * widen]
        self.stem = nn.Conv2d(3, 16, 3, padding=1)
        layers, in_ch = [], 16
        for stage, w in enumerate(widths):
            for i in range(blocks_per_stage):
                stride = 2 if (stage > 0 and i == 0) else 1
                layers.append(SkipInitBlock(in_ch, w, stride))
                in_ch = w
        self.blocks = nn.Sequential(*layers)
        self.head = nn.Sequential(
            nn.ReLU(), nn.AdaptiveAvgPool2d(1), nn.Flatten(),
            nn.Dropout(drop),  # Regularized SkipInit: one Dropout right before the softmax
            nn.Linear(in_ch, num_classes),
        )

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        return self.head(self.blocks(self.stem(x)))


model = SkipInitWRN(blocks_per_stage=166)  # depth equivalent to an n-2 Wide-ResNet with 6n+4 = 1,000 layers
x = torch.randn(4, 3, 32, 32)
model.eval()  # disable Dropout; with no normalization layers, train() and eval() are otherwise the same
with torch.no_grad():
    print(model(x).shape)  # torch.Size([4, 10])
    block = model.blocks[1]  # a block whose shape does not change (identity shortcut)
    h = torch.randn(4, 32, 32, 32)
    print(torch.equal(block(h), h))  # True: α = 0, so right after initialization the block is the identity map

FAQ

Q. Are SkipInit and ReZero the same thing?
A.

They have nearly the same form: both multiply the residual branch by a learnable scalar initialized to 0. They were proposed independently around the same time. The SkipInit paper aims to explain why batch normalization works and to verify that on ResNets. The ReZero paper starts from how signals propagate (dynamical isometry) and shows that it applies broadly to Transformers and fully connected networks as well.

Q. What happens if α is set to 1 instead of 0?
A.

In the paper's CIFAR-10 experiments, initializing with α=1\alpha = 1 trained 16 layers but not 100 or 1,000 layers. The branch acts with the same magnitude as the shortcut, so, as in a residual network without normalization, the output explodes as the network gets deeper. Setting α\alpha to 1/d1/\sqrt{d} or less (dd is the number of residual blocks) made training possible.

Q. With SkipInit, is batch normalization no longer needed?
A.

It substitutes for batch normalization in making deep networks trainable, but not in every respect. The paper organizes the benefits of batch normalization into three: making deep residual networks trainable, enabling large learning rates, and a regularizing effect. SkipInit reproduces only the first, so batch normalization gives higher accuracy when training with large minibatches or without added regularization.

References

  1. S. De and S. L. Smith, "Batch Normalization Biases Residual Blocks Towards the Identity Function in Deep Networks," NeurIPS, 2020.
  2. H. Zhang, Y. N. Dauphin, and T. Ma, "Fixup Initialization: Residual Learning Without Normalization," ICLR, 2019.
  3. T. Bachlechner, B. P. Majumder, H. H. Mao, G. W. Cottrell, and J. McAuley, "ReZero is All You Need: Fast Convergence at Large Depth," arXiv, 2020.
  4. K. He, X. Zhang, S. Ren, and J. Sun, "Identity Mappings in Deep Residual Networks," ECCV, 2016.
  5. S. Zagoruyko and N. Komodakis, "Wide Residual Networks," BMVC, 2016.