Fixup is an initialization method for training residual networks without normalization layers such as batch normalization. The name is short for fixed-update initialization (an initialization that keeps the size of updates fixed). It was proposed in 2019 by Hongyi Zhang, Yann N. Dauphin, and Tengyu Ma (ICLR 2019).
It consists of just three rules:
Initialize the classification layer and the last layer of each residual branch to 0.
Initialize the other layers with a standard method such as He initialization, and scale only the layers inside residual branches by Lβ1/(2mβ2) (L is the number of residual branches and m is the number of layers in one branch).
Add one scalar multiplier (initialized to 1) to each branch, and one scalar bias (initialized to 0) before each convolution, fully connected layer, and activation.
Normalization layers have been thought to stabilize the training of deep networks and make large learning rates usable. The paper showed that these benefits are not unique to normalization and can be achieved by choosing the initialization scale properly. On CIFAR-10, a 10,000-layer residual network without normalization was trained with the same learning rate of 0.1 as batch normalization.
The left side of the figure at the top shows the overall flow, and the right side shows the inside of a residual block. The Fixup block removes the normalization layers (BN) and adds scalar biases and a multiplier.
Theory (why standard initialization fails and how to fix it)
This section walks through the ideas in Sections 2 and 3 of the paper.
Without normalization, the output variance of a residual network grows exponentially with depth: A residual network accumulates the outputs of residual blocks Flβ (Eq. (1)).
xlβ=x0β+i=0βlβ1βFiβ(xiβ)
With a standard initialization such as He initialization, the variance of each branch's output is about the same as that of its input, so Var[xl+1β]β2Var[xlβ]. The variance roughly doubles with each added block.
Large outputs make gradients explode: The paper proves that in networks built from positively homogeneous functions such as ReLU (functions whose output is multiplied by Ξ± when the input is multiplied by Ξ±), the gradient norm of a layer is bounded from below by the cross-entropy loss (Theorems 1 and 2). As the outputs (logits) grow, the loss grows and so do the gradients (exploding gradients), so training with a large learning rate is impossible.
Set the goal as "one update changes the network output by Ξ(Ξ·)": Ξ· is the learning rate. The paper shows that the updates of the L residual branches move the network output in nearly the same direction. Therefore, to keep the total at Ξ(Ξ·), each branch's update must be held to Ξ(Ξ·/L).
Derive the scaling condition for one branch: Modeling an m-layer branch as the product of per-layer scalars aiβ, F(x)=(βi=1mβaiβ)x, the condition for an update of Ξ(Ξ·/L) is as follows (Eq. (5)).
βiβ[m]β{j}ββaiββx=Ξ(Lβ1β),jβargkminβakβ
Choose an initialization that satisfies the condition: Setting the last layer to amβ=0 and the remaining layers to aiβ=Lβ1/(2mβ2) makes the product of the remaining mβ1 layers equal Lβ1/2, satisfying Eq. (5). Because the last layer is 0, the branches output nothing at first, and the network starts training from a "shortcuts only" state.
For example, in ResNet-110, which stacks 54 basic blocks (two 3Γ3 convolutions, m=2), the first convolution is scaled to 54β1/2β0.136 times He initialization and the second convolution is set to 0.
The paper states that rule 2 (the rescaling) is the necessary and sufficient part for training very deep residual networks. Rules 1 and 3 are additional measures to approach the performance of networks with normalization.
The role of the biases and multipliers
The scalar biases and multipliers added in rule 3 recover part of what normalization layers did, with far fewer parameters.
Biases (initialized to 0): The preferred input mean of a weight layer may differ from that of an activation function. The paper reports that inserting just one scalar bias before each weight layer and activation substantially improved training performance. They correspond to the Ξ² (shift parameter) of normalization layers.
Multipliers (initialized to 1): They adjust the scale of a branch's output and correspond to the Ξ³ (scale parameter) of normalization layers. When weight decay is combined with learning rate decay, the weight norms shrink and the multipliers grow to compensate. The paper says that one multiplier per branch mimics how weight norms evolve in normalized networks, so a new learning rate schedule did not have to be searched for.
Because they are scalars, the number of added parameters is only proportional to the number of layers, far fewer than normalization layers, which have parameters per channel. In the experiments in Appendix C.1 of the paper, removing the biases slowed training, scaling the recommended factor by 0.1 slowed training, and scaling it by 10 made training fail.
Experimental results
These are the results reported in the paper.
CIFAR-10, ResNet-110 (test error, median of 5 runs, lower is better. Table 1)
Very deep networks still train: Training a width-1 Wide ResNet on CIFAR-10 and comparing test accuracy after 1 epoch across depths, Fixup matched batch normalization up to 10,000 layers. Scaling outputs by 1/2β and LSUV could not train at the same learning rate as batch normalization beyond 100 layers (Figure 3).
In image classification, adding regularization closes the gap: Fixup alone had lower training error than batch normalization and overfit. With stronger regularization from Mixup, it matched Group Normalization on ImageNet. For Wide ResNet-40-10 on CIFAR-10, with Mixup and Cutout added, the test error was 2.3% for Fixup versus 2.5% for batch normalization (Table 4).
In translation, on par with or better than normalization: Replacing Layer Normalization with Fixup kept training stable, and Dropout regularization was effective enough to prevent overfitting.
Practical caveats
Fixup does not replace the regularization effect of normalization layers. In the paper, too, models with Fixup alone fit the training data too closely, and their test error was higher than with batch normalization. When removing normalization layers, plan on adding regularization such as Mixup, Cutout, or Dropout.
Keep the following points in mind as well.
Count L and m correctly: L is the number of residual branches (residual blocks), not the total number of layers in the network. m is the number of weight layers in one branch: 2 for a basic block and 3 for a bottleneck block (the factor is Lβ1/2 for m=2 and Lβ1/4 for m=3).
PyTorch has no built-in version: torch.nn.init has no Fixup function, so rewrite the block definition and write the initialization by hand. When using kaiming_normal_ for the standard initialization, specify nonlinearity="relu".
The learning rate for the scalars is sometimes lowered: In the paper's ImageNet experiments, when strong regularization (Mixup) was used, reducing the learning rate of the scalar multipliers and biases to one tenth helped.
Shortcuts keep the standard initialization: Rule 2 shrinks only the layers inside residual branches. Layers on the shortcut path, such as the first convolution near the input, keep the standard initialization.
Comparison with other methods
The table lines up Fixup with other methods for stabilizing deep residual networks, from the same angles.
Method
What it does
Normalization layers
Statistics computed during training
Depth reported in the paper
Batch normalization
Normalizes each layer's output with the minibatch mean and variance
Used
Every step
10,000 layers (Figure 3 of the Fixup paper)
Scaling outputs by 1/2β
Multiplies the output of each residual block by 1/2β
Not used
No
Cannot train with a large learning rate beyond 100 layers
After orthogonal initialization, passes data through to bring each block's output variance close to 1
Not used
Only at initialization
Cannot train with a large learning rate beyond 100 layers
Fixup
Sets the last layer of each branch to 0 and scales the other layers by Lβ1/(2mβ2). Adds scalar biases and multipliers
Not used
No
10,000 layers
The paper explains that scaling outputs by 1/2β and LSUV keep the magnitudes of activations and gradients in check but do not account for the updates of all branches piling up in the same direction, so they cannot train very deep networks with a large learning rate.
Related initialization methods for training deep networks without normalization include SkipInit, ReZero, and T-Fixup.
Rough guidelines for choosing:
Use Fixup when minibatches are small or behavior should not differ between training and inference: Since no statistics are computed, it is unaffected by batch size or by switching between model.train() and model.eval().
Use normalization layers to avoid the effort of tuning regularization: Normalization layers also have a regularizing effect, while Fixup requires adding and tuning separate regularization.
PyTorch example
βΆShow code
python
import torch
import torch.nn as nn
def conv3x3(in_ch: int, out_ch: int, stride: int = 1) -> nn.Conv2d:
return nn.Conv2d(in_ch, out_ch, kernel_size=3, stride=stride, padding=1, bias=False)
# Fixup basic block (no normalization layers; follows Figure 1 (right) of the paper and the official implementation)
class FixupBasicBlock(nn.Module):
def __init__(self, in_ch: int, out_ch: int, stride: int = 1, downsample: nn.Module | None = None):
super().__init__()
# Rule 3: scalar biases (0) before convolutions and activations, a multiplier (1) on the branch
self.bias1a = nn.Parameter(torch.zeros(1))
self.conv1 = conv3x3(in_ch, out_ch, stride)
self.bias1b = nn.Parameter(torch.zeros(1))
self.relu = nn.ReLU(inplace=True)
self.bias2a = nn.Parameter(torch.zeros(1))
self.conv2 = conv3x3(out_ch, out_ch)
self.scale = nn.Parameter(torch.ones(1))
self.bias2b = nn.Parameter(torch.zeros(1))
self.downsample = downsample
def forward(self, x: torch.Tensor) -> torch.Tensor:
identity = x
out = self.conv1(x + self.bias1a)
out = self.relu(out + self.bias1b)
out = self.conv2(out + self.bias2a)
out = out * self.scale + self.bias2b
if self.downsample is not None:
# as in the official implementation, a parameter-free shortcut: subsample, then zero-pad channels to double them
identity = self.downsample(x + self.bias1a)
identity = torch.cat((identity, torch.zeros_like(identity)), 1)
return self.relu(out + identity)
def fixup_init(model: nn.Module, num_branches: int, m: int = 2) -> None:
"""L = num_branches (number of residual branches), m = number of layers in a branch"""
for module in model.modules():
if isinstance(module, FixupBasicBlock):
# Rule 2: scale He initialization by L^(-1/(2m-2)) (L^(-1/2) for m = 2)
nn.init.kaiming_normal_(module.conv1.weight, mode="fan_out", nonlinearity="relu")
with torch.no_grad():
module.conv1.weight.mul_(num_branches ** (-1.0 / (2 * m - 2)))
# Rule 1: last layer of the branch is 0
nn.init.zeros_(module.conv2.weight)
elif isinstance(module, nn.Linear):
# Rule 1: classification layer is also 0
nn.init.zeros_(module.weight)
nn.init.zeros_(module.bias)
class FixupResNet(nn.Module):
def __init__(self, blocks_per_stage=(18, 18, 18), num_classes: int = 10): # ResNet-110 shape
super().__init__()
self.conv1 = conv3x3(3, 16) # shortcut path: keep the standard initialization (PyTorch default)
self.bias1 = nn.Parameter(torch.zeros(1))
self.relu = nn.ReLU(inplace=True)
layers, in_ch = [], 16
for stage, n in enumerate(blocks_per_stage):
out_ch = 16 * 2 ** stage
for i in range(n):
stride = 2 if (stage > 0 and i == 0) else 1
down = None
if stride != 1:
down = nn.AvgPool2d(1, stride=stride)
layers.append(FixupBasicBlock(in_ch, out_ch, stride, down))
in_ch = out_ch
self.layers = nn.Sequential(*layers)
self.pool = nn.AdaptiveAvgPool2d(1)
self.bias2 = nn.Parameter(torch.zeros(1))
self.fc = nn.Linear(in_ch, num_classes)
fixup_init(self, num_branches=sum(blocks_per_stage), m=2)
def forward(self, x: torch.Tensor) -> torch.Tensor:
x = self.relu(self.conv1(x) + self.bias1)
x = self.layers(x)
x = self.pool(x).flatten(1)
return self.fc(x + self.bias2)
model = FixupResNet()
x = torch.randn(8, 3, 32, 32)
with torch.no_grad():
print(model(x).abs().max().item()) # the classification layer is 0, so the output right after initialization is 0
# no statistics are kept, so train() and eval() give the same output (unless Dropout is added)
model.train()
FAQ
Q. Does Fixup make normalization layers unnecessary?
A.
In terms of training stability and the ability to use large learning rates, the paper's experiments reached the same level without normalization layers. However, Fixup does not replace the regularization effect of normalization layers, so without additional regularization such as Mixup, the test error is higher than with normalization layers.
Q. If the last layer of a branch is set to 0, does training stall?
A.
No. Even if the weights of the last layer are 0, the input to that layer (the output of the first layer) is not 0, so gradients flow to the last layer's weights. Once the first update makes the last layer nonzero, the whole branch starts learning. The paper notes that starting branches at 0 has the advantage that training does not have to cancel out a random initial state.
Q. What is the scaling factor for bottleneck blocks (e.g., ResNet-50)?
A.
A bottleneck block has 3 layers in its branch, so m = 3 and the factor is L^(β1/4). ResNet-50 has 16 residual blocks, so layers 1 and 2 are scaled to 16^(β1/4) = 0.5 times He initialization, and layer 3 is set to 0.