Weight Normalization represents a neuron's weight vector w as a scalar g for its length and a vector v for its direction, and learns these two instead. Salimans and Kingma (OpenAI) proposed it in 2016.
Whereas batch normalization normalizes activations with the mini-batch mean and variance, weight normalization changes how the weights themselves are represented. Because it uses no statistics of the input data, it has the following advantages.
Samples in a batch do not depend on each other. It is independent of batch size and works as is in recurrent models such as LSTM.
It adds no noise. Batch normalization injects noise into the gradient through fluctuations in per-mini-batch statistics, but weight normalization is a deterministic computation. It suits noise-sensitive tasks such as reinforcement learning and generative models.
It is cheap. It needs no extra memory, and the extra computation is negligible.
In the figure at the top of the article, the left shows which part of the weight matrix is normalized (one row per output neuron), the center shows the split into length and direction, and the right shows the direction of the gradient and how ā„vā„ grows.
How it is computed
A neuron takes a weighted sum of k inputs x, adds a bias b, and passes the result through an activation function Ļ (Eq. (1) of the paper).
y=Ļ(wā x+b)
Weight normalization computes the following in order.
Express the weights with v and g.v is a k-dimensional vector and g is a scalar; both are learnable parameters (Eq. (2) of the paper).
w=ā„vā„gāv
This makes ā„wā„=g regardless of the value of v. The direction is set by v alone, and the length by g alone.
The forward pass uses the resulting w as usual:y=Ļ(wā x+b).
Update v and g directly by gradient descent. Their gradients follow from the ordinary weight gradient āwāL (Eq. (3) of the paper).
āgāL=ā„vā„āwāLā vā,āvāL=ā„vā„gāāwāLāā„vā„2gāgāLāv
The idea of normalizing weight vectors existed before, but earlier methods updated w directly and rescaled its length after each update. Weight normalization differs in that it reparameterizes the weights as v and g from the start and learns those two.
In a convolutional layer, the whole filter of one output channel (input channels Ć kernel height Ć width) is treated as one weight vector, and each output channel gets its own g.
The paper also tried learning g as g=es through its logarithm s, but test performance was nearly unchanged and training was slightly slower. Normally g is learned directly.
Why training becomes faster
The paper explains this by rewriting the gradient āvāL as follows (Eq. (4) of the paper).
āvāL=ā„vā„gāMwāāwāL,Mwā=Iāā„wā„2wwā¤ā
Mwā is a projection matrix that removes the component along w. The equation shows that weight normalization does two things.
It scales the gradient by g/ā„vā„.
It removes the component along the current weight direction from the gradient. Noise that was riding on that component disappears along with it. The paper notes that w is often close to the dominant eigenvector of the gradient covariance matrix, so removing it brings the covariance matrix closer to the identity, which may speed up training.
Another important effect is that ā„vā„ grows naturally and adjusts the effective learning rate. Because the update Īv of v is perpendicular to w (and thus to v), the Pythagorean theorem says the length grows with every update. With ā„Īvā„/ā„vā„=c,
ā„vā²ā„=1+c2āā„vā„ā„ā„vā„
If the learning rate is too large and updates are big, ā„vā„ grows quickly and the gradient scale g/ā„vā„ shrinks. Once updates become small, 1+c2āā1 and the growth stops. For this reason, the paper reports that networks with weight normalization train well over a wide range of learning rates.
This property holds exactly for gradient descent without momentum. It does not hold exactly for methods with per-parameter learning rates such as Adam or when using momentum, but the paper observed the same tendency in its experiments.
Relation to batch normalization: in a single-layer network whose inputs are whitened (independent components with mean 0 and variance 1), the pre-activation t=vā x has mean 0 and standard deviation ā„vā„. In that case, normalizing the pre-activation with batch normalization is equivalent to normalizing the weights with weight normalization. The paper describes weight normalization as a "cheaper and less noisy approximation" of batch normalization.
Initialization and mean-only batch normalization
Batch normalization also keeps the scale of each layer's output constant, which makes training robust to poor initialization. Weight normalization lacks this effect, so the paper also proposes data-dependent initialization (Section 3 of the paper).
Sample each component of v from a normal distribution with mean 0 and standard deviation 0.05.
Feed one mini-batch through and, for each neuron, compute the mini-batch mean μ[t] and standard deviation Ļ[t] of t=ā„vā„vā xā.
Set g and b as follows (Eq. (6) of the paper). The pre-activation values for the first mini-batch then have mean 0 and variance 1.
gāĻ[t]1ā,bāĻ[t]āμ[t]ā
This initialization is done only once, before training. See the article on data-dependent initialization for details. It cannot be applied to recurrent models such as RNNs and LSTMs, however, which must rely on standard initialization.
Weight normalization also largely decouples the scale of activations from v, but the mean still depends on v. The paper therefore also proposes combining it with mean-only batch normalization, which subtracts the mini-batch mean but does not divide by the standard deviation (Eq. (7) of the paper). At inference time, a moving average of the training-time means is used. With the mean alone, the paper explains, there is no spiky noise from estimating the variance, and the noise is gentle and close to Gaussian.
Experimental results
Image classification: the same CNN was trained on CIFAR-10 without data augmentation, changing only how its weights are represented (Adam, 200 epochs, mini-batch size 100). Test error rates are as follows (table in Figure 2 of the paper).
In terms of training speed, both weight normalization and batch normalization progressed much faster than standard weights. Batch normalization made slightly more progress per epoch early in training, but its computation was about 16% slower than standard weights, whereas weight normalization was not noticeably slower. In final error rate, weight normalization alone was on par with standard weights, and the best result came from combining it with mean-only batch normalization.
Generative models: in a convolutional VAE (MNIST and CIFAR-10), weight normalization reduced the variance across random seeds and converged to better values (Figure 3 of the paper). In DRAW, a generative model built from LSTMs (MNIST), adding weight normalization to the weights alone, while keeping the initialization and learning rate tuned for the original model, made convergence much faster (Figure 4 of the paper).
Reinforcement learning: DQN was trained on Atari games. The paper states that batch normalization could not be run at realistic mini-batch sizes, because noise in the mini-batch statistics destabilized training. The best evaluation scores are as follows (table in Figure 6 of the paper).
Game
Standard weights
Weight normalization
Breakout
410
403
Enduro
1,250
1,448
Seaquest
7,188
7,375
Space Invaders
1,779
2,179
Scores improved in three of the four games.
Practical caveats
Weight normalization alone does not equalize the output scale of each layer. Unlike batch normalization, it does not correct the scale of activations every time, so with poor initialization the output scale drifts from layer to layer. The paper recommends using it together with data-dependent initialization. PyTorch's weight_norm does not perform this initialization automatically (right after registration, it starts from the same values as the original weights).
In PyTorch, use torch.nn.utils.parametrizations.weight_norm. The older torch.nn.utils.weight_norm is deprecated. In the new function, g is named parametrizations.weight.original0 and v is named parametrizations.weight.original1 (weight_g and weight_v in the old function). Code that refers to these names directly must be rewritten.
Also keep the following in mind.
dim determines the unit over which the length is normalized. The default dim=0 is per output (per row in nn.Linear, per output channel in nn.Conv2d), which matches the paper. With dim=None, the whole weight tensor shares a single length.
The computation is the same during training and inference. Weight normalization uses no data statistics, so the output is the same under model.train() and model.eval() (when combined with mean-only batch normalization, only that part uses a moving average at inference).
In image classification accuracy, it may fall short of batch normalization. On CIFAR-10 in the paper, weight normalization alone reached 8.46%, short of batch normalization (8.05%). In the group normalization paper, ResNet-50 on ImageNet had an error rate of 28.2%, higher than batch normalization (23.6%).
In GAN discriminators, it has been pointed out to lower the rank of weight matrices. The Spectral Normalization paper (Miyato et al. 2018) argues that weight normalization, which equalizes the length of each row, tends to favor low-rank weights that use only a few features, reducing the features the discriminator uses.
Weights (length of each output's weight vector set to g)
Activations (per channel, statistics over the batch)
Activations (per sample, statistics over all features)
Weights (largest singular value of the matrix set to 1)
Data statistics
Not used
Used
Used
Not used
Sensitivity to batch size
No
Yes
No
No
Training vs. inference
Same computation
Computation differs (moving average)
Same computation
Same values (power iteration stopped at inference)
Extra computation
Negligible
Statistics per activation
Statistics per activation
One power iteration
Main uses
RNNs, reinforcement learning, generative models
Image classification with CNNs
Transformers, RNNs
GAN discriminators
A rough guide for choosing:
To avoid noise from batch statistics (reinforcement learning, RNNs, generative models with small batches): weight normalization, combined with data-dependent initialization.
Image classification with CNNs and sufficiently large batches: batch normalization.
Transformers or RNNs where activation scales should be equalized every time: layer normalization.
To stabilize GAN discriminators (to bound the Lipschitz constant): Spectral Normalization.
PyTorch example
ā¶Show code
python
import torch
import torch.nn as nn
from torch.nn.utils.parametrizations import weight_norm
from torch.nn.utils import parametrize
# Add weight normalization to Linear. dim=0: a length g per output (per row)
m = weight_norm(nn.Linear(20, 40), name="weight", dim=0)
g = m.parametrizations.weight.original0 # length g shape [40, 1]
v = m.parametrizations.weight.original1 # direction v shape [40, 20]
print(g.shape, v.shape)
# Right after registration, the values equal the original weights (starts with g = āvā)
w = m.weight # w = g Ā· v / āvā is computed on every access
print(torch.allclose(w.norm(dim=1, keepdim=True), g)) # True: āwā = g
# Computation exactly as in Eq. (2) of the paper
w_manual = g * v / v.norm(dim=1, keepdim=True)
print(torch.allclose(w_manual, w)) # True
# In convolutions, each output channel has one g
conv = weight_norm(nn.Conv2d(3, 16, kernel_size=3))
print(conv.parametrizations.weight.original0.shape) # [16, 1, 1, 1]
# Data-dependent initialization from Section 3 of the paper (Linear example). Done once before training
@torch.no_grad()
def data_dependent_init(layer, x):
v = layer.parametrizations.weight.original1
v.normal_(0, 0.05) # v: mean 0, standard deviation 0.05
t = x @ (v / v.norm(dim=1, keepdim=True)).T # t = vĀ·x / āvā
mu, sigma = t.mean(dim=0), t.std(dim=0)
layer.parametrizations.weight.original0.copy_((1 / sigma).unsqueeze(1)) # g ā 1/Ļ[t]
layer.bias.copy_(-mu / sigma) # b ā āμ[t]/Ļ[t]
x = torch.randn(256, 20)
data_dependent_init(m, x)
y = m(x)
print(y.mean(dim=0).abs().max() < 1e-4, y.std(dim=0).mean()) # mean ā 0, standard deviation ā 1
# After training, convert back to an ordinary weight
parametrize.remove_parametrizations(m, "weight")
print(type(m.weight)) # Parameter (the value of w is kept as is)
FAQ
Q. Can weight normalization and batch normalization be used together?
A.
Yes. In the paper, combining weight normalization with "mean-only batch normalization" (subtracting only the mini-batch mean, without dividing by the standard deviation) gave the lowest error rate on CIFAR-10 (7.31%). The roles are split: weight normalization handles the scale, and mean-only batch normalization handles the mean.
Q. How should the learning rate be chosen?
A.
With weight normalization, āvā grows and automatically lowers the effective learning rate, so the paper reports that training works over a wider range of learning rates than with standard weights. In the paper's CIFAR-10 experiments, the Adam learning rate was chosen from 0.0003 to 0.01, and the best was 0.003 with weight normalization and 0.0003 with standard weights. In DQN as well, 0.0003 worked well with weight normalization and 0.0001 with standard weights. Starting from a somewhat larger learning rate than for standard weights is reasonable.
Q. Does model.eval() change its behavior?
A.
No. Weight normalization only changes how the weights are represented and uses no data statistics. To speed up inference, remove_parametrizations can also convert it back to an ordinary weight with w fixed.