Batch Normalization – day 25

What does batch normalization change? During training, it measures each feature across a mini-batch, centers and rescales its values, then applies a learned scale and shift. Follow one column down the first table and you can predict what happens to every entry in the second.

Understanding Batch Normalization in Deep Learning

Deep learning has revolutionized numerous fields, from computer vision to natural language processing. However, training deep neural networks can be challenging due to issues like unstable gradients. In particular, gradients can either explode (grow too large) or vanish (shrink too small) as they propagate through the network. This instability can slow down or completely halt the learning process. To address this, a powerful technique called Batch Normalization was introduced.

The Problem: Unstable Gradients

In deep networks, the issue of unstable gradients becomes more pronounced as the network depth increases. When gradients vanish, the learning process becomes very slow, as the model parameters are updated minimally. Conversely, when gradients explode, the model parameters may be updated too drastically, causing the learning process to diverge.

Introducing Batch Normalization

Batch Normalization (BN) is a technique designed to stabilize the learning process by normalizing the inputs to each layer within the network. Proposed by Sergey Ioffe and Christian Szegedy in 2015, this method has become a cornerstone in training deep neural networks effectively.

How Batch Normalization Works

Step 1: Compute the Mean and Variance

For each mini-batch of data, Batch Normalization first computes the mean (μB) and variance (σB2) for each feature. These statistics are then used to normalize the inputs.

Example:

Example Feature 1 Feature 2 Feature 3
1 1.0 3.0 2.0
2 2.0 4.0 3.0
3 3.0 5.0 4.0

Step 2: Normalize the Inputs

The inputs are normalized by subtracting the mean and dividing by the square root of the variance (with a small constant ε added to avoid division by zero):

x^(i)=x(i)−μBσB2+ε

This normalization ensures that the inputs have a mean of 0 and a variance of 1.

Step 3: Scale and Shift

After normalization, Batch Normalization introduces two learnable parameters for each feature: γ (scale) and β (shift). These parameters allow the network to adjust the normalized data to match the original input distribution if needed:

z(i)=γx^(i)+β

Visual Example: Scaling and Shifting Effect

In the image below:

Scaling and Shifting Visualization

Practical Example and Table

Example Feature 1 Feature 2 Feature 3 Normalized Feature 1 Normalized Feature 2 Normalized Feature 3 Scaled and Shifted Feature 1 Scaled and Shifted Feature 2 Scaled and Shifted Feature 3
1 1.0 3.0 2.0 -1.224 -1.224 -1.224 -1.336 -1.224 -1.112
2 2.0 4.0 3.0 0.0 0.0 0.0 0.5 0.0 -0.5
3 3.0 5.0 4.0 1.224 1.224 1.224 2.336 1.224 0.112

How to read the original table. The original table does not specify epsilon. Its displayed values agree to three decimal places with epsilon set to 0.001 and the scales and shifts listed below. The worked example uses a smaller epsilon of 0.00001. Keeping intermediate digits lets you see the small change at the endpoints.

Benefits of Batch Normalization

  1. Faster Training: By stabilizing the input distributions, BN allows for the use of higher learning rates, speeding up the training process.
  2. Reduced Sensitivity to Initialization: The network becomes less dependent on careful initialization of parameters, as BN mitigates the effects of poor initial conditions.
  3. Improved Regularization: BN can have a slight regularizing effect, reducing the need for other forms of regularization like dropout.

Conclusion

Batch Normalization is a vital technique in deep learning, enabling faster, more stable, and more effective training of deep neural networks. By normalizing the inputs within each mini-batch and then scaling and shifting them, BN addresses the issue of unstable gradients and enhances the network’s ability to learn complex patterns.

Work through one feature

Take Feature 1, with values 1, 2 and 3. Their mean is 2. Subtract it and the centered values are −1, 0 and 1. Their squared distances from the mean add up to 2. For this forward pass, divide by the three observations in the batch to obtain a variance of two thirds. Dividing by two instead would give the sample variance used in a different statistical calculation.

Use epsilon of 0.00001 for this worked example. The divisor, the square root of two thirds plus epsilon, is approximately 0.816503. The first input lies below the mean, so its normalized value is about −1.224736. With a scale of 1.5 and a shift of 0.5, its output is about −1.337104. The central input has normalized value zero and lands exactly on the shift, 0.5. This gives you a quick way to check the sign and the offset without repeating the entire calculation.

Why do three different columns normalize alike?

Feature 2 contains 3, 4 and 5. Feature 3 contains 2, 3 and 4. Their centers move, but their distances from their own centers are identical. After normalization, all three columns therefore match row by row. The affine step lets them diverge again. Compare the end points in this table. Each row describes one feature across all three examples.

On a small screen, swipe across the table to reach the last output. With a keyboard, focus the table area and use the arrow keys.

FeatureMeanScaleShiftFirst outputLast output
121.50.5−1.33712.3371
2410−1.22471.2247
330.5−0.5−1.11240.1124

Compare the three features

Three feature panels compare the original observations, normalized values and scaled and shifted outputs for the lesson’s three-example batch.
Each panel tracks the three observations in one feature before normalization, after normalization, and after scaling and shifting. Open the figure for a larger view.

Start with the middle observation in each panel. It lands at zero after centering and at the feature’s shift after the final step. Now look at the two outer observations. A larger positive scale spreads them farther from the center. A smaller positive scale pulls them closer. If the scale were negative, their order would reverse. The scale and shift here are example choices you can inspect by hand.

What changes between training and prediction?

During training, batch statistics depend on the other examples grouped with the current input. Imagine replacing the highest value in Feature 1 with 30. The first input is still 1, but its distance from the new mean changes. During ordinary inference, the layer uses stored estimates of training means and variances. That lets a deployed model handle one example at a time without making its prediction depend on who else happens to be in the batch.

Moments and learned parameters serve different jobs. The running estimates summarize features observed during training. Gamma and beta are model parameters learned through optimization. If predictions look different during evaluation, check which mode is active and whether the stored statistics represent the evaluation data. Applying the layer can help optimization, but speed, accuracy and the need for dropout depend on the model and training setup.

Check your understanding: what if every value in a feature is 4?

The mean is 4 and each centered value is zero. Epsilon keeps the denominator positive. Every normalized value is zero, so the output equals that feature’s shift for each example in this batch.

Which direction should I read the input table?

Read down a feature column to compute its statistics. Reading across a single example would combine different features. That would answer a different question and would not reproduce the historical table.

Sources and further reading

Read the original notation for the seven formulas
\(\mu_B\)

\(\sigma^2_B\)

\(\epsilon\)

\[
        \hat{x}^{(i)} = \frac{x^{(i)} - \mu_B}{\sqrt{\sigma_B^2 + \epsilon}}
    \]

\(\gamma\)

\(\beta\)

\[
        z^{(i)} = \gamma \hat{x}^{(i)} + \beta
    \]

Leave a reply

Your email address will not be published. Required fields are marked *

You Can Also Learn Deep Learning in 80 Days Inside the iOS App Continue this roadmap in a focused mobile study experience with offline access, flashcards, notes, and progress tracking.
Open on the App Store