Batch normalization is a technique used in deep learning to normalize the inputs of each layer by adjusting and rescaling them based on their mean and variance statistics. It is done on a batch of inputs instead of individual inputs. The adjustments are learned during training and then applied during inference.
The main benefits of batch normalization are:
1. Faster training: During training, deep learning models can suffer from internal covariate shift, which refers to the problem of the distribution of activations changing as the model trains. This can cause vanishing gradients and slow down the training process. Batch normalization helps to reduce this effect by keeping the distribution of activations stable, resulting in faster convergence and training times.
2. Improved generalization: Batch normalization can help reduce overfitting by introducing some noise into the input, which makes the model more robust to unseen data.
3. Higher learning rates: Batch normalization can also allow the use of higher learning rates, which can speed up the training process and lead to better performance.
4. Removal of the need for parameter initialization and regularization: Batch normalization reduces the sensitivity of the network to the choice of initial values for the parameters and can even act as a regularization technique.
Here is an example of how batch normalization is implemented in PyTorch:
import torch.nn as nn
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(3, 64, kernel_size=3)
self.bn1 = nn.BatchNorm2d(64)
self.conv2 = nn.Conv2d(64, 128, kernel_size=3)
self.bn2 = nn.BatchNorm2d(128)
self.fc1 = nn.Linear(128 * 10 * 10, 256)
self.bn3 = nn.BatchNorm1d(256)
self.fc2 = nn.Linear(256, 10)
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = F.relu(x)
x = self.conv2(x)
x = self.bn2(x)
x = F.relu(x)
x = x.view(-1, 128 * 10 * 10)
x = self.fc1(x)
x = self.bn3(x)
x = F.relu(x)
x = self.fc2(x)
return x
ββ
In this example, batch normalization is applied after each convolutional layer and before each fully connected layer using βnn.BatchNorm2dβ and βnn.BatchNorm1dβ, respectively.