04 / 14 · Training Neural Networks
Normalization and initialization
Why the scale of activations and gradients is multiplied at every layer, how Glorot and He initialization follow from keeping that factor at one, what batch, layer, group and RMS normalization compute and why they help, and how residual connections carry signals through depth.
On this page 8
The previous note trained a network of three layers and asked how to keep it from fitting its training set too well. A network of thirty layers has the opposite problem first: with the default initialization it does not fit its training set at all. The signal that reaches the top of the stack and the gradient that reaches the bottom are each multiplied by a factor at every layer, and over thirty layers any factor other than one grows or shrinks geometrically. This note derives the initializations that set that factor to one, shows what the normalization layers compute and what is still debated about why they work, and explains how a residual connection carries the signal and its gradient through depth. It ends with one experiment that puts the three tools side by side.
Notation. A layer computes the pre-activation and the activation ; biases are zero at initialization and left out. and are the layer’s fan-in and fan-out, its input and output widths. The weights start as independent draws with mean 0 and variance . The adjoint is the gradient of the loss with respect to the pre-activation. For a normalization layer, is the batch size, and are a mean and a variance, the standardized value, and the learned scale and shift.
Baseline: a deep stack at its default initialization#
Take a plain stack of 50 bias-free ReLU layers of width 256 and feed it 512 standard normal inputs. The table gives the mean square of the pre-activations at four depths, for weights with four variances: unscaled, , the variance that PyTorch’s nn.Linear uses by default, and . The last column is the mean square of the adjoint at the first layer divided by that at the last, for a loss that is a fixed random projection of .
| weight variance | layer 1 | layer 10 | layer 25 | layer 50 | gradient, layer 1 / layer 50 |
|---|---|---|---|---|---|
| overflow | overflow | not a number | |||
| , PyTorch default | |||||
With unscaled weights the signal overflows 32-bit arithmetic before layer 25. With the default it shrinks by about a factor of 6 per layer, and the gradient that reaches the first layer is of the one at the top: the first layers never move. Only the variance keeps both the signal and the gradient at their scale. On real data the effect is the same: the thirty-layer network of the experiment, at its default initialization, stays at the loss of a uniform guess over 10 classes for all of training. The Machine Learning course draws the same picture for 30 layers (initialization that preserves the scale), and the Generative Models course traces the mechanism for one unit per layer (vanishing and exploding gradients). Here we derive the factor for both directions at once and then see what initialization cannot fix.
Why the scale is multiplied at every layer#
Forward. One pre-activation is . The weights have mean 0 and are independent of each other and of , so the terms are uncorrelated and their variances add:
The first line uses independence, the second that all inputs of a layer have the same distribution, the third only names the ratio : the fraction of the second moment that the activation lets through. For the identity . For a ReLU and a symmetric around 0, half of the distribution is set to zero and the other half kept, so and .
Backward. The chain rule gives . The -th entry is a sum over the outputs of the layer, and the same argument, with the usual assumption that is independent of at initialization, gives
For the identity ; for a ReLU the slope is 1 or 0 with probability each, so .
Over layers the two recursions multiply:
Any constant factor other than 1 per layer is raised to the power . Worked example. For ReLU and square layers of width : unscaled weights give the forward factor , and at layer 10, as in the table; the largest 32-bit number, , is passed at about layer 19. gives the factor and at layer 50; PyTorch’s default gives and . The table’s measured values follow these powers up to random fluctuation.
The gradient of a weight matrix is , a product of a backward and a forward quantity. Its size at layer carries both products, so a network can learn evenly across its layers only when both factors stay near 1.
Initialization that keeps the factor at one#
Glorot (Xavier) initialization#
For a symmetric activation that is linear near zero, such as tanh at small inputs, . Setting each factor to 1:
The two conditions agree only for a square layer, so Glorot and Bengio (2010), eq. 12, require the average of fan-in and fan-out to give 1. Drawn from a uniform distribution on , whose variance is , this means (their eq. 16). In PyTorch: nn.init.xavier_uniform_ and xavier_normal_.
He (Kaiming) initialization#
For a ReLU, :
Instead of a compromise, He et al. (2015), section 2.2, observe that one direction is enough. With the fan-in choice the backward factor of layer is , and the product over the stack telescopes:
The gradient is rescaled by the ratio of the last and the first width, a constant that does not grow with depth. In PyTorch: nn.init.kaiming_normal_(w, mode='fan_in', nonlinearity='relu').
Worked example. For a layer, Glorot gives standard deviation and He ; drawn in the Code section they come out as and . PyTorch writes both through a gain , with variance for He’s form: for ReLU, for a leaky ReLU with negative slope (so for ), and for tanh. The default of nn.Linear is a uniform draw with variance , which is He’s form with , six times too small for a plain ReLU stack; it relies on normalization layers to restore the scale, as the convolution section of the Generative Models course explains.
What the formulas do not promise#
Optional depth: the gain of tanh, and fluctuations around the average. The rest of the note does not depend on it.
The derivation controls an average over random weights. Two things fall outside it.
Saturating activations have no exact answer. The factor of tanh depends on the variance itself: at small inputs , but for a standard normal . Iterate with standard normal. With the variance decays like : after 50 layers. With PyTorch’s it settles at a fixed point , but there the backward factor is , so gradients grow by about 21% per layer. This is the order-to-chaos picture of Schoenholz et al. (2017): a deep tanh stack either loses its forward signal slowly or amplifies its gradients, and no gain makes both factors exactly 1 away from zero.
Averages are not single networks. In the table above, the He stack has mean squares , , and at layers 1, 10, 25 and 50: the logarithm of the scale performs a random walk around its average. Hanin and Rolnick (2018) show that for fully connected ReLU networks the variance of this walk grows with the sum of the reciprocals of the widths, , so a deep and narrow network fluctuates more than a shallow and wide one, and that residual networks, correctly weighted, avoid the problem.
What initialization cannot fix#
He initialization keeps the scale. It does not keep the examples apart. For two inputs whose pre-activations have correlation , the correlation after a He-initialized ReLU layer is
the normalized arc-cosine kernel of the ReLU. Its only fixed point is , and from two orthogonal inputs the correlation climbs to after one layer, after 10, after 30 and after 50. Every input is mapped close to the same direction, and the last layer sees nearly the same vector for every example. This is the rank collapse of deep unnormalized networks (Daneshmand et al., 2020). In the experiment at the end, the thirty-layer network with He initialization starts with an average cosine of between the outputs of 256 different digits, and after 12 epochs its validation accuracy ranges from to over three seeds.
Batch normalization in training and in evaluation#
Initialization fixes the scale once; during training the weights change, and so does the scale. Batch normalization (Ioffe and Szegedy, 2015) standardizes each feature over the batch at every step. The definition, the running averages, the scale invariance and the use with convolutions are written out in the Generative Models course. The figure shows the two modes of the layer on that course’s example batch.
Batch normalization in training mode and in evaluation mode
- Training mode: compute the mean and variance of the feature over the batch, and .
- Standardize every value: .
- Scale and shift by the learned and .
- Update the running averages with momentum : , and the running variance from 1 to , with the unbiased batch variance.
- Evaluation mode (
model.eval()): standardize with the running averages instead. After one step they are far from the batch statistics, and the input 6 becomes instead of .
The gradient through the batch statistics#
Optional depth: the backward pass of the layer. The rest of the note uses only its two consequences.
In training mode and depend on every example of the batch, so the gradient does too. Write and (the upstream gradient times ). From and we get , and then
where is 1 for and 0 otherwise, and the bars are averages over the batch. The first line is the quotient rule; the second sums it against . The result is the upstream gradient with its mean removed and its component along removed, divided by . Two consequences: the gradients of a batch always sum to 0 and are orthogonal to , and each example’s gradient depends on all the others. Worked example. For the batch and : , , , and the gradient is . Only the first output was pushed, yet all four inputs receive a gradient, and they sum to 0. Autograd in the Code section gives the same numbers.
Small batches and the gap between training and evaluation#
The batch statistics are estimates from values, and two failures follow from that.
Very small batches. With the standardized values are always : for any two different numbers , the mean is their midpoint and the standard deviation half their distance. The layer outputs whatever the inputs were; in the Code section the batch becomes for , . With the variance is 0 and PyTorch refuses to normalize in training mode. Statistics from a handful of examples are noisy, and the noise grows as the batch shrinks; on ImageNet, a ResNet-50 with 2 images per GPU had a top-1 error 10.6 points higher with batch normalization than with group normalization, introduced below (Wu and He, 2018).
Running averages lag behind. With momentum , the running average weights the batch from steps ago by , an average over roughly the last 10 to 20 batches. They estimate the statistics of weights that have changed since, and a model evaluated early, or right after a large step, sees a mismatch between the two modes, as in step 5 of the figure. Forgetting model.eval() causes the opposite error: predictions that depend on the other examples of the evaluation batch.
Why does it help? The debated explanations#
Batch normalization allows larger learning rates and makes deep convolutional networks train much faster; why is still argued.
- Internal covariate shift. The original motivation: each layer’s input distribution keeps changing as the layers below learn, and standardizing it stabilizes learning (Ioffe and Szegedy, 2015, section 2). Santurkar et al. (2018) injected random noise with changing mean and variance after the normalization layers, which recreates the shift, and the networks still trained about as well as with plain batch normalization (section 2.1). They argue instead that it makes the loss and its gradients smoother along the training path (section 3).
- Scale invariance. A weight vector followed by batch normalization can be multiplied by any without changing the output. Differentiating in at gives , and differentiating in gives . A gradient step therefore turns the direction of by about , an effective learning rate for the direction, and since the step is orthogonal to it can only grow and shrink that rate. Arora, Li and Lyu (2019) show that this tunes the effective rate automatically. In the Code section, multiplying the weights before a batch-norm layer by 10 leaves the output unchanged and divides the gradient by 10. How weight decay interacts with this is in Optimizing deep networks.
- Residual blocks close to the identity. In a residual network, batch normalization on the branch scales it down relative to the skip path by a factor of about the square root of the depth, so that at initialization each block is close to the identity (De and Smith, 2020). This is the mechanism of the residual section below.
- Noise as regularization. Each example is normalized with statistics of a random batch, a form of noise; the original paper found that it reduced the need for dropout (section 4.2.1).
Not every effect is good. Batch normalization keeps the examples apart, since subtracting the batch mean removes the direction they share: in the experiment below, the average cosine between outputs is with it against without. But in a deep stack without residual connections it makes the gradients explode at initialization, by a factor that grows exponentially with depth whatever the weight variance (Yang et al., 2019): in the experiment, the first layer’s weight gradient is 168 times the last one’s.
Normalizing over other axes#
Every normalization layer computes the same thing, , with the mean and variance taken over a set of values. They differ only in . For a batch of feature maps with examples, channels and positions:
Which values share one mean and one variance
- Batch norm: one channel, all examples and positions. Depends on the batch.
- Layer norm (Ba, Kiros and Hinton, 2016): one example, all channels and positions. In a Transformer, one token’s features.
- Instance norm (Ulyanov, Vedaldi and Lempitsky, 2016): one example and one channel, its positions; used for style transfer and in CycleGAN.
- Group norm (Wu and He, 2018): one example and a group of channels. With it is layer norm, with instance norm; the paper’s default is . It does not depend on the batch, which is why detection and segmentation networks, trained with one or two images per GPU, use it.
Worked example. For images, channels of : batch norm averages over values per statistic, layer norm over , instance norm over 196, and group norm with 32 groups over . Only batch norm’s count depends on ; at it falls to 392.
RMSNorm (Zhang and Sennrich, 2019) uses layer norm’s set but divides by the root mean square without subtracting the mean; the formula and an example are in the Transformer note. What it gives up is an invariance: layer norm returns the same output when a constant is added to every feature, RMSNorm only when all features are multiplied by a positive constant (both checked in the Code section). The authors’ hypothesis is that the invariance to re-scaling does the work, and they report running times 7% to 64% lower than with layer norm on different models. Where the normalization sits in a Transformer block, before or after the residual sum, is a separate choice discussed in pre-norm versus post-norm.
What normalizing one example cannot do. Layer norm and RMSNorm restore the scale of each example, but they never compare examples, so they cannot undo the rank collapse of the previous section. In the experiment below a plain thirty-layer stack with layer norm after every linear layer starts with an average cosine of between examples and never leaves the loss , exactly like the stack without normalization. In Transformers layer norm works because the residual stream carries the input past every block.
Residual connections: an identity path, and the variance it adds#
A residual block computes . Its Jacobian is , so the gradient reaches every block through a term that passes through no weight matrix; the Machine Learning course derives this and the Transformer note expands the product of such Jacobians. What the identity path does to the forward scale is the other half of the story.
At initialization, the last weight matrix of the branch has mean 0 and is independent of everything else, so and the variances add:
Take a branch with He variance for and for , which returns the mean square of its input. Three choices give three growth laws over blocks, starting from mean square 1:
- No normalization, : each block doubles the mean square, , so at the top. Measured: , and after 10, 25 and 50 blocks.
- Pre-norm, : the branch sees an input of mean square 1 and adds 1 per block, , so 51 at the top. Measured: , and .
- Scaled branch, : each block multiplies the mean square by , and , below for any depth. Measured: , and .
The backward pass has the same structure, , and the same additive accounting. This is why every deep residual network does one of these things. Pre-norm Transformers normalize the branch input. ResNets with batch normalization get the down-scaling from the normalization on the branch (De and Smith, 2020) and often go further by setting the scale of the last batch-norm layer of each block to 0, so that every block starts as the exact identity (Goyal et al., 2017, section 5.1). Fixup trains residual networks without any normalization by zeroing the last layer of each branch and scaling the others down with depth (Zhang, Dauphin and Ma, 2019).
The identity path also keeps examples apart. Since contains unchanged, the differences between two inputs are carried up the stack, and the correlation grows much more slowly than in a plain stack: for the pre-norm network the theory gives after 10 blocks and after 50, against and for the plain He stack.
Scale and the difference between examples across depth
An experiment: thirty layers on digits#
The setting: the 1797 bundled digit images, 70% for training and 30% for validation, a linear input layer to width 64, thirty hidden layers of width 64 and a linear output layer, trained with Adam at learning rate , batches of 32, for 12 epochs. Plain layers compute ; residual blocks compute , the pre-norm form. The first two columns are measured at initialization on 256 training images: the average cosine between the network’s final hidden vectors for different images, and the norm of the first hidden layer’s weight gradient divided by the last one’s. The last two are means over three seeds.
| configuration | cosine at init | gradient, first / last | training loss | validation accuracy |
|---|---|---|---|---|
| plain, default init | 1.000 | 2.303 | 0.102 | |
| plain, He init | 0.993 | 1.14 | 0.425 | 0.834 |
| plain, batch norm | 0.335 | 168 | 1.274 | 0.556 |
| plain, layer norm | 0.995 | 0.246 | 2.303 | 0.100 |
| residual, default init | 0.850 | 0.813 | 0.021 | 0.966 |
| residual, pre-norm with layer norm | 0.758 | 5.06 | 0.036 | 0.961 |
Each row matches one mechanism of the note. The default stack loses both the scale and the differences between examples, and learns nothing. He initialization restores the gradient but not the differences, and trains unevenly. Batch norm keeps the examples apart but its gradients explode, and in 12 epochs it gets only part of the way. Layer norm restores the scale of each example but cannot separate them. Both residual networks train to a training loss near 0 and above 96% validation accuracy; with PyTorch’s default initialization the plain residual branch is small enough that the stack works without normalization at this depth. These are three seeds of one small setting; with more epochs the plain configurations improve, but the order between failing and training does not change.
What changed#
| Plain deep stack | With the tools of this note | |
|---|---|---|
| Scale of activations | multiplied by per layer | factor 1 at init (Glorot, He); held during training by normalization |
| Scale of gradients | multiplied by per layer | factor 1 at init; identity path in residual blocks |
| Differences between examples | collapse towards one direction | kept by batch norm and by residual connections |
| Dependence on the batch | none | batch norm only: noisy for small batches, two modes |
| Residual stack scale | without control | with pre-norm, below with scaled branches |
| Thirty layers on digits | stuck at loss 2.303 | 0.96–0.97 validation accuracy |
Limitations#
- Initialization is a statement about averages at step 0. Once training starts, weights and inputs are no longer independent, and the derivations no longer hold; single networks fluctuate around the average, more so when they are narrow.
- Batch norm couples examples. It needs batches large enough for stable statistics, behaves differently in training and evaluation, and its running averages must be recomputed whenever the weights change without training steps, for example after averaging weights.
- The explanations are partial. Why batch norm helps is still debated, and the arguments above (smoothness, scale invariance, identity bias, noise) are each supported in some settings and not a complete account.
- Normalizing one example does not separate examples. Layer norm, group norm and RMSNorm work in practice inside residual architectures; in a plain deep stack they do not prevent rank collapse.
- Saturating activations have no exact fix. For tanh no gain keeps both the forward and the backward factor at 1 away from zero.
What you should remember#
- Per layer, the forward variance is multiplied by and the backward variance by , with for ReLU and 1 for a linear activation; over layers the factors are raised to the power .
- Glorot: . He: , with the backward product telescoping to . PyTorch’s default shrinks a plain ReLU stack by 6 per layer.
- Deep unnormalized ReLU stacks map every input towards one direction; initialization cannot prevent it.
- Batch norm standardizes each feature over the batch; its gradient removes the batch mean and the component along ; it uses batch statistics in training and running averages in evaluation, and fails for very small batches.
- Batch, layer, instance and group norm differ only in the set of values that share a mean and a variance; RMSNorm drops the mean and the invariance to shifts.
- A residual block adds variances: without normalization, with pre-norm, below with branches scaled by or zero-initialized; it keeps both the gradient path and the differences between examples.
Code#
The scale of 50 layers under four initializations, Glorot and He, the gain of tanh, the backward pass of batch norm checked against autograd, its two modes and its scale invariance, the axes of the normalization layers, residual stacks with three growth laws, and the experiment of the last section. Seeds are fixed; exact numbers can differ slightly across platforms and library versions. The script runs in about 40 seconds on a laptop CPU, almost all of it in the experiment.
Data. Optical Recognition of Handwritten Digits, E. Alpaydin and C. Kaynak (1998), UCI Machine Learning Repository, CC BY 4.0. Loaded through scikit-learn; the transformations used in this example are specified in the code.
import math, numpy as np, torch
from torch import nn
import torch.nn.functional as F
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
# 1. the scale of z_l in a stack of 50 bias-free ReLU layers of width 256, for four weight variances v
n, L = 256, 50
torch.manual_seed(0)
x0 = torch.randn(512, n, requires_grad=True)
r = torch.randn(512, n) # a fixed random direction for the loss at the top
for name, v in [('N(0,1)', 1.0), ('1/n', 1 / n), ('default', 1 / (3 * n)), ('He', 2 / n)]:
torch.manual_seed(1)
h, zs = x0, []
for l in range(L):
z = h @ (torch.randn(n, n) * math.sqrt(v)).T
z.retain_grad(); zs.append(z); h = torch.relu(z)
(zs[-1] * r).sum().backward()
ms = ['%.3g' % zs[i].pow(2).mean().item() for i in (0, 9, 24, 49)]
print(name, ms, 'gradient ratio z_1 / z_50: %.3g' % (zs[0].grad.pow(2).mean() / zs[-1].grad.pow(2).mean()).item())
# N(0,1) ['258', '2.41e+21', 'inf', 'nan'] gradient ratio z_1 / z_50: nan
# 1/n ['1.01', '0.00199', '2.48e-08', '2.19e-15'] gradient ratio z_1 / z_50: 2.94e-15
# default ['0.336', '3.37e-08', '2.93e-20', '3.05e-39'] gradient ratio z_1 / z_50: 1.23e-38
# He ['2.02', '2.04', '0.833', '2.46'] gradient ratio z_1 / z_50: 1.66
# 2. Glorot and He for a 256 x 256 layer, and the gain of tanh: the variance a deep tanh stack settles at
torch.manual_seed(0); W = torch.empty(256, 256)
print(round(nn.init.xavier_normal_(W).std().item(), 4), round(nn.init.kaiming_normal_(W, nonlinearity='relu').std().item(), 4))
# 0.0624 0.0887 (theory 0.0625 and 0.0884)
xi = np.random.default_rng(0).standard_normal(2_000_000)
for g in (1.0, 5 / 3):
q, qs = 1.0, []
for l in range(200):
q = g * g * np.mean(np.tanh(math.sqrt(q) * xi) ** 2); qs.append(q)
chi = g * g * np.mean((1 - np.tanh(math.sqrt(q) * xi) ** 2) ** 2) # backward factor per layer at that variance
print('gain %.3f' % g, [round(float(qs[i]), 4) for i in (9, 49, 199)], 'backward factor %.3f' % chi)
# gain 1.000 [0.0521, 0.0101, 0.0024] backward factor 0.995
# gain 1.667 [1.1776, 1.1776, 1.1776] backward factor 1.211
# 3. batch norm: the gradient through the batch statistics, checked against autograd
x = torch.tensor([1.0, 2.0, 3.0, 6.0], requires_grad=True)
g = torch.tensor([1.0, 0.0, 0.0, 0.0]) # upstream gradient dL/dxhat
mu, var = x.mean(), x.var(unbiased=False)
xhat = (x - mu) / torch.sqrt(var + 1e-5)
(xhat * g).sum().backward()
by_hand = (g - g.mean() - xhat * (g * xhat).mean()) / torch.sqrt(var + 1e-5)
print([round(v, 4) for v in x.grad.tolist()], [round(v, 4) for v in by_hand.tolist()], round(x.grad.sum().item(), 6))
# [0.2482, -0.21, -0.1336, 0.0954] [0.2482, -0.21, -0.1336, 0.0954] 0.0
# 4. train and eval statistics: one training step of BatchNorm1d, then the same inputs in eval mode
bn = nn.BatchNorm1d(1)
with torch.no_grad(): bn.weight.fill_(2.0); bn.bias.fill_(0.5)
xb = torch.tensor([[1.0], [2.0], [3.0], [6.0]])
print([round(v, 3) for v in bn(xb).flatten().tolist()], round(bn.running_mean.item(), 3), round(bn.running_var.item(), 3))
bn.eval()
print([round(v, 3) for v in bn(xb).flatten().tolist()])
bn.train()
print([round(v, 3) for v in bn(torch.tensor([[4.0], [10.0]])).flatten().tolist()]) # a batch of two: always beta -/+ gamma
# [-1.638, -0.569, 0.5, 3.707] 0.3 1.367
# [1.698, 3.408, 5.119, 10.252]
# [-1.5, 2.5]
# 5. scale invariance: multiplying the weights before batch norm by 10 leaves the output and divides the gradient by 10
torch.manual_seed(0)
Wa = torch.randn(8, 4, requires_grad=True); Wb = (10 * Wa.detach()).requires_grad_()
xs, bn2 = torch.randn(16, 4), nn.BatchNorm1d(8, eps=1e-12)
ya, yb = bn2(xs @ Wa.T), bn2(xs @ Wb.T)
ya.pow(3).sum().backward(); yb.pow(3).sum().backward()
print(round((ya - yb).abs().max().item(), 6), round((Wa.grad.norm() / Wb.grad.norm()).item(), 3), abs(round((Wa.detach() * Wa.grad).sum().item(), 4)))
# 0.0 10.0 0.0 (the last number: the gradient is orthogonal to the weights)
# 6. which values share a statistic, for a batch of 32 feature maps with 256 channels of 14 x 14
N, C, HW, G = 32, 256, 14 * 14, 32
print({'batch': N * HW, 'layer': C * HW, 'instance': HW, 'group': C // G * HW})
# {'batch': 6272, 'layer': 50176, 'instance': 196, 'group': 1568}
t = torch.randn(2, 4, 3, 3)
print(torch.allclose(nn.GroupNorm(1, 4, affine=False)(t), F.layer_norm(t, t.shape[1:]), atol=1e-5),
torch.allclose(nn.GroupNorm(4, 4, affine=False)(t), F.instance_norm(t), atol=1e-5))
# True True: one group is layer norm, one channel per group is instance norm
v = torch.tensor([1.0, 2.0, 3.0, 6.0]); rms = nn.RMSNorm(4, elementwise_affine=False)
print(torch.allclose(F.layer_norm(v + 5, (4,)), F.layer_norm(v, (4,)), atol=1e-5), torch.allclose(rms(v + 5), rms(v), atol=1e-5))
# True False: layer norm ignores a shift of all features, RMSNorm does not
# 7. stacks of 50 blocks: plain He layers, and residual blocks x + F(x) with F = W2 relu(W1 u), Var W1 = 2/n, Var W2 = 1/n
def stack(mode, x, L=50):
torch.manual_seed(1)
out = []
for l in range(L):
W1, W2 = torch.randn(n, n) * math.sqrt(2 / n), torch.randn(n, n) * math.sqrt(1 / n)
if mode == 'plain':
x = torch.relu(x @ (torch.randn(n, n) * math.sqrt(2 / n)).T)
else:
u = F.layer_norm(x, (n,)) if mode == 'pre-norm' else x
branch = torch.relu(u @ W1.T) @ W2.T
x = x + (branch / math.sqrt(L) if mode == 'scaled' else branch)
out.append(x)
return out
a, b = torch.randn(n), torch.randn(n)
xx = torch.stack([a, b - (a @ b) / (a @ a) * a]) # two orthogonal inputs
xx = xx / xx.pow(2).mean(1, keepdim=True).sqrt() # each with mean square 1
for mode in ('plain', 'none', 'pre-norm', 'scaled'):
hs = stack(mode, xx)
ms = ['%.3g' % hs[i].pow(2).mean().item() for i in (9, 24, 49)]
cos = [round(F.cosine_similarity(hs[i][0], hs[i][1], dim=0).item(), 3) for i in (9, 29, 49)]
print(mode, ms, cos)
# plain ['0.765', '1.85', '2.93'] [0.887, 0.984, 0.996] theory: cosine 0.872, 0.972, 0.988
# none ['867', '2.9e+07', '4.14e+14'] [0.71, 0.92, 0.94] theory: 2^l; cosine 0.708, 0.915, 0.960
# pre-norm ['11.3', '26', '52.7'] [0.499, 0.553, 0.546] theory: 1 + l; cosine 0.441, 0.553, 0.595
# scaled ['1.2', '1.66', '2.73'] [0.067, 0.125, 0.22] theory: 1.02^l; cosine 0.060, 0.164, 0.251
# 8. thirty layers of width 64 on digits: initialization, normalization and residual connections
X, y = load_digits(return_X_y=True)
Xtr, Xva, ytr, yva = train_test_split(X / 16.0, y, test_size=0.3, random_state=0, stratify=y)
Xtr, Xva = torch.tensor(Xtr, dtype=torch.float32), torch.tensor(Xva, dtype=torch.float32)
ytr, yva = torch.tensor(ytr), torch.tensor(yva)
class Net(nn.Module):
def __init__(self, norm, he, res, width=64, depth=30):
super().__init__()
self.inp, self.out, self.res = nn.Linear(64, width), nn.Linear(width, 10), res
self.lins = nn.ModuleList([nn.Linear(width, width) for _ in range(depth)])
make = {'none': nn.Identity, 'batch': lambda: nn.BatchNorm1d(width), 'layer': lambda: nn.LayerNorm(width)}[norm]
self.norms = nn.ModuleList([make() for _ in range(depth)])
if he:
for lin in self.lins: nn.init.kaiming_normal_(lin.weight, nonlinearity='relu'); nn.init.zeros_(lin.bias)
def forward(self, x):
h = self.inp(x)
for lin, norm in zip(self.lins, self.norms):
h = h + lin(F.relu(norm(h))) if self.res else F.relu(norm(lin(h))) # residual blocks are pre-norm
return self.out(h)
configs = [('plain, default init', 'none', False, False), ('plain, He', 'none', True, False),
('plain, batch norm', 'batch', False, False), ('plain, layer norm', 'layer', False, False),
('residual', 'none', False, True), ('residual, layer norm', 'layer', False, True)]
print(' cos at init grad first/last train loss val acc')
for name, norm, he, res in configs:
torch.manual_seed(0)
model = Net(norm, he, res)
h = model.inp(Xtr[:256]) # diagnostics at initialization, 256 training images
for lin, nm in zip(model.lins, model.norms):
h = h + lin(F.relu(nm(h))) if res else F.relu(nm(lin(h)))
z = F.normalize(h, dim=1); cos = ((z @ z.T).sum() - 256) / (256 * 255)
F.cross_entropy(model.out(h), ytr[:256]).backward()
ratio = model.lins[0].weight.grad.norm() / model.lins[-1].weight.grad.norm()
runs = []
for seed in range(3):
torch.manual_seed(seed)
model = Net(norm, he, res)
opt = torch.optim.Adam(model.parameters(), lr=1e-3)
loader = torch.utils.data.DataLoader(torch.utils.data.TensorDataset(Xtr, ytr), batch_size=32, shuffle=True, drop_last=True)
for epoch in range(12):
model.train()
for xb, yb in loader:
opt.zero_grad(); F.cross_entropy(model(xb), yb).backward(); opt.step()
model.eval()
with torch.no_grad():
runs.append([F.cross_entropy(model(Xtr), ytr).item(), (model(Xva).argmax(1) == yva).float().mean().item()])
m = np.mean(runs, axis=0)
print(f'{name:22} {cos.item():10.3f} {ratio.item():14.3g} {m[0]:12.3f} {m[1]:8.3f}')
# cos at init grad first/last train loss val acc
# plain, default init 1.000 1.97e-11 2.303 0.102
# plain, He 0.993 1.14 0.425 0.834
# plain, batch norm 0.335 168 1.274 0.556
# plain, layer norm 0.995 0.246 2.303 0.100
# residual 0.850 0.813 0.021 0.966
# residual, layer norm 0.758 5.06 0.036 0.961
Next#
With the scale under control, a deep network can be trained; how fast and how well depends on the optimizer. Its loss surface is not convex, its best learning rate changes during training, its weight decay interacts with the normalization layers of this note, and its numbers may be stored in 16 bits. The next note takes up the learning-rate schedule and warm-up, AdamW, gradient clipping, the link between batch size and learning rate, flat minima, and mixed precision.
References#
- X. Glorot, Y. Bengio. Understanding the difficulty of training deep feedforward neural networks. AISTATS, 2010. Section 4.2 (variance of activations and gradients), eqs. 12 and 16.
- K. He, X. Zhang, S. Ren, J. Sun. Delving deep into rectifiers: surpassing human-level performance on ImageNet classification. ICCV, 2015. Section 2.2 (initialization of rectifier networks).
- S. S. Schoenholz, J. Gilmer, S. Ganguli, J. Sohl-Dickstein. Deep information propagation. ICLR, 2017.
- B. Hanin, D. Rolnick. How to start training: the effect of initialization and architecture. NeurIPS, 2018.
- H. Daneshmand, J. Kohler, F. Bach, T. Hofmann, A. Lucchi. Batch normalization provably avoids rank collapse for randomly initialised deep networks. NeurIPS, 2020.
- S. Ioffe, C. Szegedy. Batch normalization: accelerating deep network training by reducing internal covariate shift. ICML, 2015. Sections 2 and 3, section 4.2.1 (fewer dropout layers).
- S. Santurkar, D. Tsipras, A. Ilyas, A. Madry. How does batch normalization help optimization? NeurIPS, 2018. Section 2.1 (noisy batch norm), section 3 (smoothness).
- S. Arora, Z. Li, K. Lyu. Theoretical analysis of auto rate-tuning by batch normalization. ICLR, 2019.
- G. Yang, J. Pennington, V. Rao, J. Sohl-Dickstein, S. S. Schoenholz. A mean field theory of batch normalization. ICLR, 2019.
- S. De, S. L. Smith. Batch normalization biases residual blocks towards the identity function in deep networks. NeurIPS, 2020.
- J. L. Ba, J. R. Kiros, G. E. Hinton. Layer normalization. arXiv, 2016.
- D. Ulyanov, A. Vedaldi, V. Lempitsky. Instance normalization: the missing ingredient for fast stylization. arXiv, 2016.
- Y. Wu, K. He. Group normalization. ECCV, 2018.
- B. Zhang, R. Sennrich. Root mean square layer normalization. NeurIPS, 2019.
- P. Goyal et al. Accurate, large minibatch SGD: training ImageNet in 1 hour. arXiv, 2017. Section 5.1 (zero γ in the last batch-norm layer of each residual block).
- H. Zhang, Y. N. Dauphin, T. Ma. Fixup initialization: residual learning without normalization. ICLR, 2019.
- I. Goodfellow, Y. Bengio, A. Courville. Deep Learning. MIT Press, 2016. Section 8.4 (parameter initialization), section 8.7.1 (batch normalization).
- A. Zhang, Z. C. Lipton, M. Li, A. J. Smola. Dive into Deep Learning. Cambridge University Press, 2023. Section 5.4 (numerical stability and initialization), section 8.5 (batch normalization), section 8.6 (residual networks).
- S. J. D. Prince. Understanding Deep Learning. MIT Press, 2023. Section 7.5 (initialization), chapter 11 (residual networks and batch normalization).
- Глубинное обучение 1 (Deep Learning 1), Faculty of Computer Science, HSE University. Course materials, repository xiyori/intro-to-dl-hse, branch 2025-2026: lecture 2 (batch normalization in training and evaluation mode).