Skip to main content
Open In Colab Batch normalization is a trainable transformation that standardizes each feature using statistics from a mini-batch. For feature jj in a batch of mm examples, μj=1m∑i=1mxij,σj2=1m∑i=1m(xij−μj)2,\mu_j = \frac{1}{m}\sum_{i=1}^{m}x_{ij}, \qquad \sigma_j^2 = \frac{1}{m}\sum_{i=1}^{m}(x_{ij}-\mu_j)^2, x^ij=xij−μjσj2+ϵ,yij=γjx^ij+βj.\hat{x}_{ij} = \frac{x_{ij}-\mu_j}{\sqrt{\sigma_j^2+\epsilon}}, \qquad y_{ij} = \gamma_j\hat{x}_{ij}+\beta_j. The normalization step fixes the batch mean and variance of each feature. The learned parameters γj\gamma_j and βj\beta_j then restore the scale and offset needed by the model. During training, the layer uses mini-batch statistics and updates running estimates. During inference, it uses the stored running estimates, so the output of an example no longer depends on the other examples in its batch. Batch normalization was introduced by Ioffe and Szegedy (2015) to accelerate optimization and permit larger learning rates. The paper attributed these improvements to reduced internal covariate shift, meaning changes in a layer’s input distribution as earlier layers are updated. Santurkar et al. (2018) later tested this explanation and found that distributional stability was not the source of the optimization benefit. The derivation below first develops the original internal-covariate-shift argument. The final section then evaluates that argument using the evidence reported by Santurkar et al.

Backpropagation through a single neuron

Consider a sigmoid neuron with input vector xx, weight vector ww, and bias bb: z=w⊤x+b,a=σ(z)=11+e−z.z = w^\top x + b, \qquad a = \sigma(z) = \frac{1}{1+e^{-z}}. For a scalar loss LL, the chain rule gives ∂L∂wi=∂L∂a∂a∂z∂z∂wi=∂L∂a σ′(z)xi.\frac{\partial L}{\partial w_i} = \frac{\partial L}{\partial a}\frac{\partial a}{\partial z}\frac{\partial z}{\partial w_i} = \frac{\partial L}{\partial a}\,\sigma'(z)x_i. Define the local error signal δ=∂L∂a σ′(z).\delta = \frac{\partial L}{\partial a}\,\sigma'(z). The parameter gradients are therefore ∇wL=δx,∂L∂b=δ.\nabla_w L = \delta x, \qquad \frac{\partial L}{\partial b}=\delta. The weight gradient is proportional to the input vector. Its magnitude depends on both the input scale and the local error signal. For a sigmoid, σ′(z)=a(1−a)\sigma'(z)=a(1-a) approaches zero as ∣z∣|z| grows, so saturation can suppress the gradient even when the input is large. The bias gradient contains no direct factor of xx.
Output from cell 3

Backpropagation through a dense layer

A dense layer followed by a ReLU computes z=Wx+b,a=ReLU⁡(z).z = Wx+b, \qquad a=\operatorname{ReLU}(z). Let g=∂L/∂ag=\partial L/\partial a be the upstream gradient and let r=1[z>0]r=\mathbb{1}[z>0] be the ReLU gate. The error signal at the pre-activation is δ=g⊙r.\delta = g\odot r. Applying the chain rule to each element WjkW_{jk} gives ∂L∂Wjk=δjxk,\frac{\partial L}{\partial W_{jk}}=\delta_jx_k, and the complete parameter gradients are ∂L∂W=δx⊤,∂L∂b=δ.\frac{\partial L}{\partial W}=\delta x^\top, \qquad \frac{\partial L}{\partial b}=\delta. The weight gradient is an outer product. Every row is the layer input scaled by one component of the error signal. For a mini-batch, the update is the sum or mean of these outer products over the examples. Consequently, changes in the distribution and scale of the layer inputs change the distribution and scale of its weight gradients. The bias gradient again has no direct multiplicative dependence on the input.
Output from cell 5

The internal covariate shift hypothesis

For layer ℓ\ell, z(ℓ)=W(ℓ)h(ℓ−1)+b(ℓ),∂L∂W(ℓ)=δ(ℓ)h(ℓ−1)⊤.z^{(\ell)}=W^{(\ell)}h^{(\ell-1)}+b^{(\ell)}, \qquad \frac{\partial L}{\partial W^{(\ell)}} =\delta^{(\ell)}{h^{(\ell-1)}}^\top. An update to an earlier layer changes h(ℓ−1)h^{(\ell-1)}. The next optimization step therefore presents layer ℓ\ell with a different input distribution and a different distribution of weight gradients. Ioffe and Szegedy called this moving target internal covariate shift. This term is specific to hidden activations during training and should not be confused with covariate shift between training and test data. The original argument proceeds as follows:
  1. Earlier layers continually change the coordinates received by later layers.
  2. Later layers must adapt to those changing coordinates.
  3. Standardizing intermediate features should make their scale more stable, improve gradient flow, and allow larger learning rates.
In the common Linear -> BatchNorm -> ReLU arrangement, batch normalization standardizes the pre-activation z(ℓ)z^{(\ell)} before the nonlinearity. This directly controls the coordinates delivered to the ReLU and indirectly controls the input delivered to the next layer. It does not directly normalize h(ℓ−1)h^{(\ell-1)} in the gradient formula for the current weight matrix, an important limitation of the simple argument. If a wide layer has approximately Gaussian pre-activations, the learned affine transformation gives y≈N(β,γ2).y \approx \mathcal{N}(\beta,\gamma^2). After a ReLU, the probability of an output being clipped to zero is approximately P(y≤0)=Φ ⁣(−β∣γ∣).P(y\leq 0)=\Phi\!\left(-\frac{\beta}{|\gamma|}\right). This calculation is useful, but it is not a definition of batch normalization. The layer fixes only the first two moments, not the shape of the distribution, and γ\gamma and β\beta are learned.
Output from cell 7

Evidence against the internal covariate shift explanation

The optimization benefit of batch normalization is well established. The claim that the benefit is caused by reduced internal covariate shift is not. Santurkar et al. (2018) tested the causal claim directly. Their experiments found that the distributional stability of layer inputs had little connection to successful training. They instead observed that batch normalization made the loss and gradients vary more smoothly with the parameters. A smoother objective makes a gradient step more predictive and supports larger learning rates. This result rejects a strong version of the original story. A moving hidden distribution may occur during training, but reducing that movement is neither a sufficient explanation nor the measured source of batch normalization’s benefit. Santurkar et al. instead support an optimization account based on a smoother loss landscape and more stable, predictive gradients.

References

PyTorch reference