# 7.5. Batch Normalization¶

Training deep neural nets is difficult. And getting them to converge in a reasonable amount of time can be tricky. In this section, we describe batch normalization (BN) [Ioffe & Szegedy, 2015], a popular and effective technique that consistently accelerates the convergence of deep nets. Together with residual blocks—covered in Section 7.6—BN has made it possible for practitioners to routinely train networks with over 100 layers.

## 7.5.1. Training Deep Networks¶

To motivate batch normalization, let us review a few practical challenges that arise when training ML models and neural nets in particular.

Choices regarding data preprocessing often make an enormous difference in the final results. Recall our application of multilayer perceptrons to predicting house prices (Section 4.10). Our first step when working with real data was to standardize our input features to each have a mean of

*zero*and variance of*one*. Intuitively, this standardization plays nicely with our optimizers because it puts the parameters are a-priori at a similar scale.For a typical MLP or CNN, as we train, the activations in intermediate layers may take values with widely varying magnitudes—both along the layers from the input to the output, across nodes in the same layer, and over time due to our updates to the model’s parameters. The inventors of batch normalization postulated informally that this drift in the distribution of activations could hamper the convergence of the network. Intuitively, we might conjecture that if one layer has activation values that are 100x that of another layer, this might necessitate compensatory adjustments in the learning rates.

Deeper networks are complex and easily capable of overfitting. This means that regularization becomes more critical.

Batch normalization is applied to individual layers (optionally, to all
of them) and works as follows: In each training iteration, we first
normalize the inputs (of batch normalization) by subtracting their mean
and dividing by their standard deviation, where both are estimated based
on the statistics of the current minibatch. Next, we apply a scaling
coefficient and a scaling offset. It is precisely due to this
*normalization* based on *batch* statistics that *batch normalization*
derives its name.

Note that if we tried to apply BN with minibatches of size \(1\), we would not be able to learn anything. That is because after subtracting the means, each hidden node would take value \(0\)! As you might guess, since we are devoting a whole section to BN, with large enough minibatches, the approach proves effective and stable. One takeaway here is that when applying BN, the choice of minibatch size may be even more significant than without BN.

Formally, BN transforms the activations at a given layer \(\mathbf{x}\) according to the following expression:

Here, \(\hat{\mathbf{\mu}}\) is the minibatch sample mean and \(\hat{\mathbf{\sigma}}\) is the minibatch sample standard deviation. After applying BN, the resulting minibatch of activations has zero mean and unit variance. Because the choice of unit variance (vs some other magic number) is an arbitrary choice, we commonly include coordinate-wise scaling coefficients \(\mathbf{\gamma}\) and offsets \(\mathbf{\beta}\). Consequently, the activation magnitudes for intermediate layers cannot diverge during training because BN actively centers and rescales them back to a given mean and size (via \(\mathbf{\mu}\) and \(\sigma\)). One piece of practitioner’s intuition/wisdom is that BN seems to allows for more aggressive learning rates.

Formally, denoting a particular minibatch by \(\mathcal{B}\), we calculate \(\hat{\mathbf{\mu}}_\mathcal{B}\) and \(\hat\sigma_\mathcal{B}\) as follows:

Note that we add a small constant \(\epsilon > 0\) to the variance estimate to ensure that we never attempt division by zero, even in cases where the empirical variance estimate might vanish. The estimates \(\hat{\mathbf{\mu}}_\mathcal{B}\) and \(\hat{\mathbf{\sigma}}_\mathcal{B}\) counteract the scaling issue by using noisy estimates of mean and variance. You might think that this noisiness should be a problem. As it turns out, this is actually beneficial.

This turns out to be a recurring theme in deep learning. For reasons that are not yet well-characterized theoretically, various sources of noise in optimization often lead to faster training and less overfitting. While traditional machine learning theorists might buckle at this characterization, this variation appears to act as a form of regularization. In some preliminary research, [Teye et al., 2018] and [Luo et al., 2018] relate the properties of BN to Bayesian Priors and penalties respectively. In particular, this sheds some light on the puzzle of why BN works best for moderate minibatches sizes in the \(50\)–\(100\) range.

Fixing a trained model, you might (rightly) think that we would prefer
to use the entire dataset to estimate the mean and variance. Once
training is complete, why would we want the same image to be classified
differently, depending on the batch in which it happens to reside?
During training, such exact calculation is infeasible because the
activations for all data points change every time we update our model.
However, once the model is trained, we can calculate the means and
variances of each layer’s activations based on the entire dataset.
Indeed this is standard practice for models employing batch
normalization and thus BN layers function differently in *training mode*
(normalizing by minibatch statistics) and in *prediction mode*
(normalizing by dataset statistics).

We are now ready to take a look at how batch normalization works in practice.

## 7.5.2. Batch Normalization Layers¶

Batch normalization implementations for fully-connected layers and convolutional layers are slightly different. We discuss both cases below. Recall that one key differences between BN and other layers is that because BN operates on a full minibatch at a time, we cannot just ignore the batch dimension as we did before when introducing other layers.

### 7.5.2.1. Fully-Connected Layers¶

When applying BN to fully-connected layers, we usually insert BN after the affine transformation and before the nonlinear activation function. Denoting the input to the layer by \(\mathbf{x}\), the linear transform (with weights \(\theta\)) by \(f_{\theta}(\cdot)\), the activation function by \(\phi(\cdot)\), and the BN operation with parameters \(\mathbf{\beta}\) and \(\mathbf{\gamma}\) by \(\mathrm{BN}_{\mathbf{\beta}, \mathbf{\gamma}}\), we can express the computation of a BN-enabled, fully-connected layer \(\mathbf{h}\) as follows:

Recall that mean and variance are computed on the *same* minibatch
\(\mathcal{B}\) on which the transformation is applied. Also recall
that the scaling coefficient \(\mathbf{\gamma}\) and the offset
\(\mathbf{\beta}\) are parameters that need to be learned jointly
with the more familiar parameters \(\mathbf{\theta}\).

### 7.5.2.2. Convolutional Layers¶

Similarly, with convolutional layers, we typically apply BN after the
convolution and before the nonlinear activation function. When the
convolution has multiple output channels, we need to carry out batch
normalization for *each* of the outputs of these channels, and each
channel has its own scale and shift parameters, both of which are
scalars. Assume that our minibatches contain \(m\) each and that for
each channel, the output of the convolution has height \(p\) and
width \(q\). For convolutional layers, we carry out each batch
normalization over the \(m \cdot p \cdot q\) elements per output
channel simultaneously. Thus we collect the values over all spatial
locations when computing the mean and variance and consequently (within
a given channel) apply the same \(\hat{\mathbf{\mu}}\) and
\(\hat{\mathbf{\sigma}}\) to normalize the values at each spatial
location.

### 7.5.2.3. Batch Normalization During Prediction¶

As we mentioned earlier, BN typically behaves differently in training mode and prediction mode. First, the noise in \(\mathbf{\mu}\) and \(\mathbf{\sigma}\) arising from estimating each on minibatches are no longer desirable once we have trained the model. Second, we might not have the luxury of computing per-batch normalization statistics, e.g., we might need to apply our model to make one prediction at a time.

Typically, after training, we use the entire dataset to compute stable estimates of the activation statistics and then fix them at prediction time. Consequently, BN behaves differently during training and at test time. Recall that dropout also exhibits this characteristic.

## 7.5.3. Implementation from Scratch¶

Below, we implement a batch normalization layer with tensors from scratch:

```
from d2l import mxnet as d2l
from mxnet import autograd, np, npx, init
from mxnet.gluon import nn
npx.set_np()
def batch_norm(X, gamma, beta, moving_mean, moving_var, eps, momentum):
# Use autograd to determine whether the current mode is training mode or
# prediction mode
if not autograd.is_training():
# If it is the prediction mode, directly use the mean and variance
# obtained from the incoming moving average
X_hat = (X - moving_mean) / np.sqrt(moving_var + eps)
else:
assert len(X.shape) in (2, 4)
if len(X.shape) == 2:
# When using a fully connected layer, calculate the mean and
# variance on the feature dimension
mean = X.mean(axis=0)
var = ((X - mean) ** 2).mean(axis=0)
else:
# When using a two-dimensional convolutional layer, calculate the
# mean and variance on the channel dimension (axis=1). Here we
# need to maintain the shape of `X`, so that the broadcast
# operation can be carried out later
mean = X.mean(axis=(0, 2, 3), keepdims=True)
var = ((X - mean) ** 2).mean(axis=(0, 2, 3), keepdims=True)
# In training mode, the current mean and variance are used for the
# standardization
X_hat = (X - mean) / np.sqrt(var + eps)
# Update the mean and variance of the moving average
moving_mean = momentum * moving_mean + (1.0 - momentum) * mean
moving_var = momentum * moving_var + (1.0 - momentum) * var
Y = gamma * X_hat + beta # Scale and shift
return Y, moving_mean, moving_var
```

```
from d2l import torch as d2l
import torch
from torch import nn
def batch_norm(X, gamma, beta, moving_mean, moving_var, eps, momentum):
# Use torch.is_grad_enabled() to determine whether the current mode is
# training mode or prediction mode
if not torch.is_grad_enabled():
# If it is the prediction mode, directly use the mean and variance
# obtained from the incoming moving average
X_hat = (X - moving_mean) / torch.sqrt(moving_var + eps)
else:
assert len(X.shape) in (2, 4)
if len(X.shape) == 2:
# When using a fully connected layer, calculate the mean and
# variance on the feature dimension
mean = X.mean(dim=0)
var = ((X - mean) ** 2).mean(dim=0)
else:
# When using a two-dimensional convolutional layer, calculate the
# mean and variance on the channel dimension (axis=1). Here we
# need to maintain the shape of `X`, so that the broadcast
# operation can be carried out later
mean = X.mean(dim=(0, 2, 3), keepdim=True)
var = ((X - mean) ** 2).mean(dim=(0, 2, 3), keepdim=True)
# In training mode, the current mean and variance are used for the
# standardization
X_hat = (X - mean) / torch.sqrt(var + eps)
# Update the mean and variance of the moving average
moving_mean = momentum * moving_mean + (1.0 - momentum) * mean
moving_var = momentum * moving_var + (1.0 - momentum) * var
Y = gamma * X_hat + beta # Scale and shift
return Y, moving_mean, moving_var
```

```
from d2l import tensorflow as d2l
import tensorflow as tf
def batch_norm(X, gamma, beta, moving_mean, moving_var, eps):
# Compute reciprocal of square root of the moving variance element-wise
inv = tf.cast(tf.math.rsqrt(moving_var + eps), X.dtype)
# Scaling and shift
inv *= gamma
Y = X * inv + (beta - moving_mean * inv)
return Y
```

We can now create a proper `BatchNorm`

layer. Our layer will maintain
proper parameters corresponding for scale `gamma`

and shift `beta`

,
both of which will be updated in the course of training. Additionally,
our layer will maintain a moving average of the means and variances for
subsequent use during model prediction.

Putting aside the algorithmic details, note the design pattern
underlying our implementation of the layer. Typically, we define the
math in a separate function, say `batch_norm`

. We then integrate this
functionality into a custom layer, whose code mostly addresses
bookkeeping matters, such as moving data to the right device context,
allocating and initializing any required variables, keeping track of
running averages (here for mean and variance), etc. This pattern enables
a clean separation of math from boilerplate code. Also note that for the
sake of convenience we did not worry about automatically inferring the
input shape here, thus we need to specify the number of features
throughout. Do not worry, the `BatchNorm`

layer will care of this for
us.

```
class BatchNorm(nn.Block):
# num_features: the number of outputs for a fully-connected layer
# or the number of output channels for a convolutional layer.
# num_dims: 2 for a fully-connected layer and 4 for a convolutional layer.
def __init__(self, num_features, num_dims, **kwargs):
super().__init__(**kwargs)
if num_dims == 2:
shape = (1, num_features)
else:
shape = (1, num_features, 1, 1)
# The scale parameter and the shift parameter involved in gradient
# finding and iteration are initialized to 0 and 1 respectively
self.gamma = self.params.get('gamma', shape=shape, init=init.One())
self.beta = self.params.get('beta', shape=shape, init=init.Zero())
# All the variables not involved in gradient finding and iteration are
# initialized to 0 on the CPU
self.moving_mean = np.zeros(shape)
self.moving_var = np.zeros(shape)
def forward(self, X):
# If X is not on the CPU, copy `moving_mean` and `moving_var` to the
# device where `X` is located
if self.moving_mean.ctx != X.ctx:
self.moving_mean = self.moving_mean.copyto(X.ctx)
self.moving_var = self.moving_var.copyto(X.ctx)
# Save the updated `moving_mean` and `moving_var`
Y, self.moving_mean, self.moving_var = batch_norm(
X, self.gamma.data(), self.beta.data(), self.moving_mean,
self.moving_var, eps=1e-12, momentum=0.9)
return Y
```

```
class BatchNorm(nn.Module):
# num_features: the number of outputs for a fully-connected layer
# or the number of output channels for a convolutional layer.
# num_dims: 2 for a fully-connected layer and 4 for a convolutional layer.
def __init__(self, num_features, num_dims):
super().__init__()
if num_dims == 2:
shape = (1, num_features)
else:
shape = (1, num_features, 1, 1)
# The scale parameter and the shift parameter involved in gradient
# finding and iteration are initialized to 0 and 1 respectively
self.gamma = nn.Parameter(torch.ones(shape))
self.beta = nn.Parameter(torch.zeros(shape))
# All the variables not involved in gradient finding and iteration are
# initialized to 0 on the CPU
self.moving_mean = torch.zeros(shape)
self.moving_var = torch.zeros(shape)
def forward(self, X):
# If X is not on the CPU, copy `moving_mean` and `moving_var` to the
# device where `X` is located
if self.moving_mean.device != X.device:
self.moving_mean = self.moving_mean.to(X.device)
self.moving_var = self.moving_var.to(X.device)
# Save the updated `moving_mean` and `moving_var`
Y, self.moving_mean, self.moving_var = batch_norm(
X, self.gamma, self.beta, self.moving_mean,
self.moving_var, eps=1e-5, momentum=0.9)
return Y
```

```
class BatchNorm(tf.keras.layers.Layer):
def __init__(self, **kwargs):
super(BatchNorm, self).__init__(**kwargs)
def build(self, input_shape):
weight_shape = [input_shape[-1], ]
self.gamma = self.add_weight(name='gamma', shape=weight_shape,
initializer=tf.initializers.ones, trainable=True)
self.beta = self.add_weight(name='beta', shape=weight_shape,
initializer=tf.initializers.zeros, trainable=True)
self.moving_mean = self.add_weight(name='moving_mean',
shape=weight_shape, initializer=tf.initializers.zeros,
trainable=False)
self.moving_variance = self.add_weight(name='moving_variance',
shape=weight_shape, initializer=tf.initializers.ones,
trainable=False)
super(BatchNorm, self).build(input_shape)
def assign_moving_average(self, variable, value):
momentum = 0.9
delta = variable * momentum + value * (1 - momentum)
return variable.assign(delta)
@tf.function
def call(self, inputs, training):
if training:
axes = list(range(len(inputs.shape) - 1))
batch_mean = tf.reduce_mean(inputs, axes, keepdims=True)
batch_variance = tf.reduce_mean(tf.math.squared_difference(
inputs, tf.stop_gradient(batch_mean)), axes, keepdims=True)
batch_mean = tf.squeeze(batch_mean, axes)
batch_variance = tf.squeeze(batch_variance, axes)
mean_update = self.assign_moving_average(
self.moving_mean, batch_mean)
variance_update = self.assign_moving_average(
self.moving_variance, batch_variance)
self.add_update(mean_update)
self.add_update(variance_update)
mean, variance = batch_mean, batch_variance
else:
mean, variance = self.moving_mean, self.moving_variance
output = batch_norm(inputs, moving_mean=mean, moving_var=variance,
beta=self.beta, gamma=self.gamma, eps=1e-5)
return output
```

## 7.5.4. Using a Batch Normalization LeNet¶

To see how to apply `BatchNorm`

in context, below we apply it to a
traditional LeNet model (Section 6.6). Recall that BN is
typically applied after the convolutional layers and fully-connected
layers but before the corresponding activation functions.

```
net = nn.Sequential()
net.add(nn.Conv2D(6, kernel_size=5),
BatchNorm(6, num_dims=4),
nn.Activation('sigmoid'),
nn.MaxPool2D(pool_size=2, strides=2),
nn.Conv2D(16, kernel_size=5),
BatchNorm(16, num_dims=4),
nn.Activation('sigmoid'),
nn.MaxPool2D(pool_size=2, strides=2),
nn.Dense(120),
BatchNorm(120, num_dims=2),
nn.Activation('sigmoid'),
nn.Dense(84),
BatchNorm(84, num_dims=2),
nn.Activation('sigmoid'),
nn.Dense(10))
```

```
net = nn.Sequential(
nn.Conv2d(1, 6, kernel_size=5), BatchNorm(6, num_dims=4), nn.Sigmoid(),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(6, 16, kernel_size=5), BatchNorm(16, num_dims=4), nn.Sigmoid(),
nn.MaxPool2d(kernel_size=2, stride=2), nn.Flatten(),
nn.Linear(16*4*4, 120), BatchNorm(120, num_dims=2), nn.Sigmoid(),
nn.Linear(120, 84), BatchNorm(84, num_dims=2), nn.Sigmoid(),
nn.Linear(84, 10))
```

```
# Recall that this has to be a function that will be passed to
# `d2l.train_ch6()` so that model building/compiling need to be within
# `strategy.scope()` in order to utilize the CPU/GPU devices that we have.
def net():
return tf.keras.models.Sequential([
tf.keras.layers.Conv2D(filters=6, kernel_size=5,
input_shape=(28, 28, 1)),
BatchNorm(),
tf.keras.layers.Activation('sigmoid'),
tf.keras.layers.MaxPool2D(pool_size=2, strides=2),
tf.keras.layers.Conv2D(filters=16, kernel_size=5),
BatchNorm(),
tf.keras.layers.Activation('sigmoid'),
tf.keras.layers.MaxPool2D(pool_size=2, strides=2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(120),
BatchNorm(),
tf.keras.layers.Activation('sigmoid'),
tf.keras.layers.Dense(84),
BatchNorm(),
tf.keras.layers.Activation('sigmoid'),
tf.keras.layers.Dense(10)]
)
```

As before, we will train our network on the Fashion-MNIST dataset. This code is virtually identical to that when we first trained LeNet (Section 6.6). The main difference is the considerably larger learning rate.

```
lr, num_epochs, batch_size = 1.0, 10, 256
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)
d2l.train_ch6(net, train_iter, test_iter, num_epochs, lr)
```

```
loss 0.246, train acc 0.910, test acc 0.859
17949.0 examples/sec on gpu(0)
```

```
lr, num_epochs, batch_size = 1.0, 10, 256
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)
d2l.train_ch6(net, train_iter, test_iter, num_epochs, lr)
```

```
loss 0.247, train acc 0.910, test acc 0.867
36710.5 examples/sec on cuda:0
```

```
lr, num_epochs, batch_size = 1.0, 10, 256
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)
net = d2l.train_ch6(net, train_iter, test_iter, num_epochs, lr)
```

```
loss 0.243, train acc 0.911, test acc 0.864
38025.0 examples/sec on /GPU:0
```

Let us have a look at the scale parameter `gamma`

and the shift
parameter `beta`

learned from the first batch normalization layer.

```
net[1].gamma.data().reshape(-1,), net[1].beta.data().reshape(-1,)
```

```
(array([2.0491238, 1.3080053, 2.8814194, 1.8625139, 1.4381742, 1.5589936], ctx=gpu(0)),
array([ 1.4796219 , 0.16893028, -3.0726378 , -0.18331419, -0.38265368,
-0.17052764], ctx=gpu(0)))
```

```
net[1].gamma.reshape((-1,)), net[1].beta.reshape((-1,))
```

```
(tensor([1.2385, 1.2548, 3.1070, 2.0117, 1.0825, 1.0813], device='cuda:0',
grad_fn=<ViewBackward>),
tensor([-0.4637, -0.4793, 2.4170, 0.2376, -0.4139, -0.5242], device='cuda:0',
grad_fn=<ViewBackward>))
```

```
tf.reshape(net.layers[1].gamma, (-1,)), tf.reshape(net.layers[1].beta, (-1,))
```

```
(<tf.Tensor: shape=(6,), dtype=float32, numpy=
array([1.4603854, 1.5732766, 1.3480722, 2.7757716, 1.693321 , 1.2305957],
dtype=float32)>,
<tf.Tensor: shape=(6,), dtype=float32, numpy=
array([ 0.1882435 , 0.07475264, -0.5621295 , -3.017201 , -0.05751451,
-0.8848195 ], dtype=float32)>)
```

## 7.5.5. Concise Implementation¶

Compared with the `BatchNorm`

class, which we just defined ourselves,
we can use the `BatchNorm`

class defined in high-level APIs directly.
The code looks virtually identical to the application our implementation
above.

```
net = nn.Sequential()
net.add(nn.Conv2D(6, kernel_size=5),
nn.BatchNorm(),
nn.Activation('sigmoid'),
nn.MaxPool2D(pool_size=2, strides=2),
nn.Conv2D(16, kernel_size=5),
nn.BatchNorm(),
nn.Activation('sigmoid'),
nn.MaxPool2D(pool_size=2, strides=2),
nn.Dense(120),
nn.BatchNorm(),
nn.Activation('sigmoid'),
nn.Dense(84),
nn.BatchNorm(),
nn.Activation('sigmoid'),
nn.Dense(10))
```

```
net = nn.Sequential(
nn.Conv2d(1, 6, kernel_size=5), nn.BatchNorm2d(6), nn.Sigmoid(),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(6, 16, kernel_size=5), nn.BatchNorm2d(16), nn.Sigmoid(),
nn.MaxPool2d(kernel_size=2, stride=2), nn.Flatten(),
nn.Linear(256, 120), nn.BatchNorm1d(120), nn.Sigmoid(),
nn.Linear(120, 84), nn.BatchNorm1d(84), nn.Sigmoid(),
nn.Linear(84, 10))
```

```
def net():
return tf.keras.models.Sequential([
tf.keras.layers.Conv2D(filters=6, kernel_size=5,
input_shape=(28, 28, 1)),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Activation('sigmoid'),
tf.keras.layers.MaxPool2D(pool_size=2, strides=2),
tf.keras.layers.Conv2D(filters=16, kernel_size=5),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Activation('sigmoid'),
tf.keras.layers.MaxPool2D(pool_size=2, strides=2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(120),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Activation('sigmoid'),
tf.keras.layers.Dense(84),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Activation('sigmoid'),
tf.keras.layers.Dense(10),
])
```

Below, we use the same hyper-parameters to train out model. Note that as usual, the high-level API variant runs much faster because its code has been compiled to C++/CUDA while our custom implementation must be interpreted by Python.

```
d2l.train_ch6(net, train_iter, test_iter, num_epochs, lr)
```

```
loss 0.250, train acc 0.908, test acc 0.895
32099.4 examples/sec on gpu(0)
```

```
d2l.train_ch6(net, train_iter, test_iter, num_epochs, lr)
```

```
loss 0.251, train acc 0.907, test acc 0.883
64989.3 examples/sec on cuda:0
```

```
d2l.train_ch6(net, train_iter, test_iter, num_epochs, lr)
```

```
loss 0.247, train acc 0.909, test acc 0.863
60736.1 examples/sec on /GPU:0
```

```
<tensorflow.python.keras.engine.sequential.Sequential at 0x7f5c2a019f10>
```

## 7.5.6. Controversy¶

Intuitively, batch normalization is thought to make the optimization landscape smoother. However, we must be careful to distinguish between speculative intuitions and true explanations for the phenomena that we observe when training deep models. Recall that we do not even know why simpler deep neural networks (MLPs and conventional CNNs) generalize well in the first place. Even with dropout and L2 regularization, they remain so flexible that their ability to generalize to unseen data cannot be explained via conventional learning-theoretic generalization guarantees.

In the original paper proposing batch normalization, the authors, in
addition to introducing a powerful and useful tool, offered an
explanation for why it works: by reducing *internal covariate shift*.
Presumably by *internal covariate shift* the authors meant something
like the intuition expressed above—the notion that the distribution of
activations changes over the course of training. However there were two
problems with this explanation: (1) This drift is very different from
*covariate shift*, rendering the name a misnomer. (2) The explanation
offers an under-specified intuition but leaves the question of *why
precisely this technique works* an open question wanting for a rigorous
explanation. Throughout this book, we aim to convey the intuitions that
practitioners use to guide their development of deep neural networks.
However, we believe that it is important to separate these guiding
intuitions from established scientific fact. Eventually, when you master
this material and start writing your own research papers you will want
to be clear to delineate between technical claims and hunches.

Following the success of batch normalization, its explanation in terms
of *internal covariate shift* has repeatedly surfaced in debates in the
technical literature and broader discourse about how to present machine
learning research. In a memorable speech given while accepting a Test of
Time Award at the 2017 NeurIPS conference, Ali Rahimi used *internal
covariate shift* as a focal point in an argument likening the modern
practice of deep learning to alchemy. Subsequently, the example was
revisited in detail in a position paper outlining troubling trends in
machine learning [Lipton & Steinhardt, 2018]. In the technical
literature other authors ([Santurkar et al., 2018])
have proposed alternative explanations for the success of BN, some
claiming that BN’s success comes despite exhibiting behavior that is in
some ways opposite to those claimed in the original paper.

We note that the *internal covariate shift* is no more worthy of
criticism than any of thousands of similarly vague claims made every
year in the technical ML literature. Likely, its resonance as a focal
point of these debates owes to its broad recognizability to the target
audience. Batch normalization has proven an indispensable method,
applied in nearly all deployed image classifiers, earning the paper that
introduced the technique tens of thousands of citations.

## 7.5.7. Summary¶

During model training, batch normalization continuously adjusts the intermediate output of the neural network by utilizing the mean and standard deviation of the minibatch, so that the values of the intermediate output in each layer throughout the neural network are more stable.

The batch normalization methods for fully connected layers and convolutional layers are slightly different.

Like a dropout layer, batch normalization layers have different computation results in training mode and prediction mode.

Batch Normalization has many beneficial side effects, primarily that of regularization. On the other hand, the original motivation of reducing covariate shift seems not to be a valid explanation.

## 7.5.8. Exercises¶

Can we remove the fully connected affine transformation before the batch normalization or the bias parameter in convolution computation?

Find an equivalent transformation that applies prior to the fully connected layer.

Is this reformulation effective. Why (not)?

Compare the learning rates for LeNet with and without batch normalization.

Plot the decrease in training and test error.

What about the region of convergence? How large can you make the learning rate?

Do we need Batch Normalization in every layer? Experiment with it?

Can you replace Dropout by Batch Normalization? How does the behavior change?

Fix the coefficients

`beta`

and`gamma`

, and observe and analyze the results.Review the online documentation for

`BatchNorm`

to see the other applications for Batch Normalization.Research ideas: think of other normalization transforms that you can apply? Can you apply the probability integral transform? How about a full rank covariance estimate?