Equations · Ioffe and Szegedy, “Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift” (2015)

The batch normalization (BatchNorm) formula, explained

BatchNorm normalizes each feature using the mean and variance of that feature across the mini-batch, then applies a learned scale and shift. It is computed per feature (per channel in a CNN), across examples. LayerNorm does the opposite: per example, across features.

μB=1m∑i=1mxi,σB2=1m∑i=1m(xi−μB)2x^i=xi−μBσB2+ϵyi=γx^i+β≡BNγ,β(xi)\begin{aligned} \mu_{\mathcal{B}} &= \frac{1}{m}\sum_{i=1}^{m} x_i, \quad \sigma_{\mathcal{B}}^2 = \frac{1}{m}\sum_{i=1}^{m} (x_i - \mu_{\mathcal{B}})^2 \\ \hat{x}_i &= \frac{x_i - \mu_{\mathcal{B}}}{\sqrt{\sigma_{\mathcal{B}}^2 + \epsilon}} \\ y_i &= \gamma \hat{x}_i + \beta \equiv \mathrm{BN}_{\gamma,\beta}(x_i) \end{aligned}

Open in the decoder →

Term by term

  1. B={x1,…,xm}\mathcal{B} = \{x_1, \ldots, x_m\}

    The mini-batch: the mm values of one feature, one per example. The calligraphic B\mathcal{B} is a name for the set, used here as a subscript label.

  2. μB=1m∑i=1mxi\mu_{\mathcal{B}} = \frac{1}{m}\sum_{i=1}^{m} x_i

    The batch mean of the feature.

  3. σB2\sigma_{\mathcal{B}}^2

    The batch variance (dividing by mm, not m−1m - 1). σ\sigma is a standard deviation here, not the sigmoid.

  4. x^i=xi−μBσB2+ϵ\hat{x}_i = \frac{x_i - \mu_{\mathcal{B}}}{\sqrt{\sigma_{\mathcal{B}}^2 + \epsilon}}

    The normalized value: mean 0, variance 1 across the batch. ϵ\epsilon (about 10−510^{-5}) avoids division by zero.

  5. yi=γx^i+βy_i = \gamma \hat{x}_i + \beta

    A learned scale γ\gamma and shift β\beta per feature, so the network can undo the normalization where that helps.

  6. ≡BNγ,β(xi)\equiv \mathrm{BN}_{\gamma,\beta}(x_i)

    "Is by definition": the whole transform is named BN\mathrm{BN}, with its parameters γ,β\gamma, \beta written as subscripts.

Shapes

A small worked example

One feature over a batch of four: (1,3,5,7)(1, 3, 5, 7). μB=4\mu_{\mathcal{B}} = 4, σB2=9+1+1+94=5\sigma_{\mathcal{B}}^2 = \frac{9 + 1 + 1 + 9}{4} = 5, so x^≈(−1.34,−0.45,0.45,1.34)\hat{x} \approx (-1.34, -0.45, 0.45, 1.34). With γ=2\gamma = 2, β=1\beta = 1: y≈(−1.68,0.11,1.89,3.68)y \approx (-1.68, 0.11, 1.89, 3.68). At inference there is no batch, so running averages of μ\mu and σ2\sigma^2 collected during training are used instead.

In code

import numpy as np

def batch_norm(x, gamma, beta, eps=1e-5):
    # x: (B, d); statistics over the batch axis
    mu = x.mean(axis=0)
    var = x.var(axis=0)
    return gamma * (x - mu) / np.sqrt(var + eps) + beta

Every symbol in it

X,D,L\mathcal{X}, \mathcal{D}, \mathcal{L}Calligraphic lettersxi,  Wq,  pθ,  dmodelx_i,\; W_q,\; p_\theta,\; d_{\text{model}}Subscript (index or label)x1,…,xnx_1, \ldots, x_nDots (ellipsis)μ\muMu∑i=1Nxi\sum_{i=1}^{N} x_iSummationab\frac{a}{b}Fractionσ(z)\sigma(z)Sigma (lowercase)Var⁡[X]\operatorname{Var}[X]Variancex2,  βt,  WQx^2,\; \beta^t,\; W^QSuperscript (power or label)y^\hat{y}Hat (estimate)dk\sqrt{d_k}Square rootϵ\epsilonEpsilonγ\gammaGammaβ1,β2\beta_1, \beta_2Betaf≡0f \equiv 0Identically equal

Other equations