Which numbers does batch normalization remember? During training, a layer uses the current batch to normalize its inputs and updates estimates for future predictions. At inference, those stored estimates take over. The four Keras models below show where that change matters in an actual network.
Introduction to Batch Normalization
Batch normalization is a widely used technique in deep learning that significantly improves the performance and stability of neural networks. Introduced by Sergey Ioffe and Christian Szegedy in 2015, this technique addresses the issues of vanishing and exploding gradients that can occur during training, particularly in deep networks.
Why Batch Normalization?
In deep learning, as data propagates through the layers of a neural network, it can lead to shifts in the distribution of inputs to layers deeper in the network—a phenomenon known as internal covariate shift. This shift can cause issues such as vanishing gradients, where gradients become too small, slowing down the training process, or exploding gradients, where they become too large, leading to unstable training. Traditional solutions like careful initialization and lower learning rates help, but they don’t entirely solve these problems.
What is Batch Normalization?
Batch normalization (BN) mitigates these issues by normalizing the inputs of each layer within a mini-batch, ensuring that the inputs to a given layer have a consistent distribution. This normalization happens just before or after the activation function of each hidden layer.
Here’s a step-by-step breakdown of how batch normalization works:
- Zero-Centering and Normalization:
For each mini-batch, compute the mean () and variance () of the inputs.
Normalize the inputs by subtracting the mean and dividing by the standard deviation:
Here, is a small constant added for numerical stability. - Scaling and Shifting:
After normalization, the data is scaled and shifted using two learned parameters, (scale) and (shift):
This allows the network to undo the normalization if it proves beneficial.
Benefits of Batch Normalization
- Reduces Internal Covariate Shift: By normalizing each layer’s inputs, batch normalization stabilizes the learning process, allowing the network to use higher learning rates.
- Acts as a Regularizer: BN adds noise to each hidden layer’s input in every mini-batch, providing a regularizing effect similar to dropout.
- Eliminates the Need for Careful Initialization: The network becomes less sensitive to the scale of initial weights, allowing for simpler initialization schemes.
Implementing Batch Normalization in Keras
In these listings, follow the layer calls from top to bottom. On a narrow screen, swipe across the code to see long lines. With a keyboard, focus a code block and use the horizontal arrow keys.
Here’s how you can implement batch normalization in a Keras model:
import tensorflow as tf
model = tf.keras.Sequential([
tf.keras.layers.Flatten(input_shape=[28, 28]),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Dense(300, activation='relu', kernel_initializer="he_normal"),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Dense(100, activation='relu', kernel_initializer="he_normal"),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Dense(10, activation='softmax')
])
This model demonstrates the use of batch normalization layers both before and after dense layers, which is a common practice.
Understanding the Parameters
- Gamma () and Beta (): These are trainable parameters that allow the network to learn the optimal scale and shift for each normalized input.
- Moving Mean and Moving Variance: During training, the network tracks the mean and variance for each batch to use them during testing, where the input might not be as well-behaved as during training.
Understanding the Math Behind Batch Normalization
In Part 1, we covered the basic concept and implementation of Batch Normalization (BN). Now, let’s delve deeper into the mathematical foundations and explore more advanced aspects.
Batch normalization operates by normalizing the inputs of each layer across the mini-batch. Here’s a more detailed breakdown of the algorithm:
- Calculate the Mean:
- Calculate the Variance:
- Normalize the Input:
The small constant ensures numerical stability by preventing division by zero. - Scale and Shift:
Here, and are trainable parameters that allow the model to scale and shift the normalized output.
Training and Test Time Differences
One important consideration when using batch normalization is the difference between how the network behaves during training versus inference (test time).
- Training: During training, the mean and variance are calculated on each mini-batch, allowing the network to adapt to the distribution of the input data dynamically.
- Inference: During inference, the network uses a running average of the mean and variance from training to normalize the inputs. This ensures consistency and avoids issues that might arise from having a smaller or differently distributed batch.
Most deep learning frameworks, including Keras, handle this switch between training and inference automatically when using the BatchNormalization layer.
Implementing Batch Normalization in Keras: Code Examples
Here are some practical examples to demonstrate different ways to implement Batch Normalization in Keras.
1. Batch Normalization Before the Activation Function:
import tensorflow as tf
model = tf.keras.Sequential([
tf.keras.layers.Flatten(input_shape=[28, 28]),
tf.keras.layers.BatchNormalization(), # BN before activation
tf.keras.layers.Dense(300, kernel_initializer="he_normal"),
tf.keras.layers.Activation("relu"),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Dense(100, kernel_initializer="he_normal"),
tf.keras.layers.Activation("relu"),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Dense(10, activation='softmax')
])
In this configuration, Batch Normalization is applied before the activation functions. This approach is recommended because it often leads to faster convergence and better performance.
2. Batch Normalization After the Activation Function:
import tensorflow as tf
model = tf.keras.Sequential([
tf.keras.layers.Flatten(input_shape=[28, 28]),
tf.keras.layers.Dense(300, kernel_initializer="he_normal"),
tf.keras.layers.Activation("relu"),
tf.keras.layers.BatchNormalization(), # BN after activation
tf.keras.layers.Dense(100, kernel_initializer="he_normal"),
tf.keras.layers.Activation("relu"),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Dense(10, activation='softmax')
])
Here, Batch Normalization is applied after the activation functions. While this approach can still be effective, it may not be as beneficial as applying BN before the activation, especially when using non-linear activations like ReLU.
3. Using Batch Normalization Without Bias:
In certain cases, you can disable the bias term in the dense layers when using Batch Normalization. The bias term is typically unnecessary because Batch Normalization itself includes a trainable shift parameter ().
import tensorflow as tf
model = tf.keras.Sequential([
tf.keras.layers.Flatten(input_shape=[28, 28]),
tf.keras.layers.Dense(300, use_bias=False, kernel_initializer="he_normal"), # No bias
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Activation("relu"),
tf.keras.layers.Dense(100, use_bias=False, kernel_initializer="he_normal"), # No bias
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Activation("relu"),
tf.keras.layers.Dense(10, activation='softmax')
])
By setting use_bias=False, we eliminate the redundant bias terms from the dense layers. This setup can lead to a more efficient model since the bias term would otherwise be corrected by the Batch Normalization’s shift parameter.
Hyperparameters in Batch Normalization
While the defaults often work well, Batch Normalization has several hyperparameters that you might need to tweak:
- Momentum:
This controls how quickly the running averages of mean and variance decay. A value close to 1 (e.g., 0.9, 0.99, 0.999) means that the model gives more weight to recent mini-batches.
Larger datasets with smaller mini-batches may require higher momentum values to maintain stability. - Axis:
Determines which axis should be normalized. By default, it normalizes the last axis (e.g., features for fully connected layers or channels for convolutional layers).
Adjusting the axis parameter allows BN to be applied in various configurations depending on the data structure (e.g., 2D convolutional layers).
Challenges and Considerations
While Batch Normalization is a powerful tool, it’s not without its challenges:
- Increased Complexity: Adding BN layers increases the complexity of your model. Each BN layer introduces additional parameters (, ) and operations that increase computational overhead.
- Training Time: The addition of BN layers can slow down the training time per epoch due to the extra computations required. However, BN typically reduces the total number of epochs needed to converge, often resulting in a faster overall training process.
- Small Batch Sizes: BN may become less effective with very small batch sizes since the mean and variance estimates become less reliable. In such cases, alternatives like Layer Normalization (LN) or Group Normalization (GN) might be better suited.
Practical Tips for Using Batch Normalization
- Positioning BN Layers:
Place BN layers before the activation function for ReLU and other non-saturating activations. However, you can experiment with positioning to see what works best for your specific model and dataset. - Use with Dropout:
While BN and Dropout are both regularization techniques, using them together can sometimes result in diminished returns. Often, it’s advisable to use one or the other, though some models benefit from both. - Tuning Momentum:
Experiment with different momentum values, especially if your dataset or batch size changes. Higher momentum can help stabilize training on large datasets with small batch sizes. - Handling Small Batches:
If your model’s batch size is small (e.g., due to memory constraints), consider using other normalization techniques like Layer Normalization, which normalizes across the feature axis rather than across the batch axis. - Monitoring Performance:
Keep an eye on training and validation loss curves when using Batch Normalization. If the validation loss starts to increase while the training loss decreases, you may need to adjust your BN layers or other hyperparameters.
Conclusion
Batch Normalization has revolutionized the training of deep neural networks by making them more stable and allowing for the use of higher learning rates. While it introduces additional complexity and computational overhead, its benefits in terms of faster convergence, reduced internal covariate shift, and regularization often outweigh the downsides.
By carefully integrating and tuning Batch Normalization in your models, you can achieve state-of-the-art performance across a wide range of deep learning tasks.
How to compare the four Keras listings
Imagine drawing a line immediately before each normalization call. The values that cross that line are the values used to estimate the moments. When the line comes after ReLU, the batch may contain many zeros because the activation has already clipped negative values. When the line comes before ReLU, negative preactivations still contribute to the mean and variance. This changes the calculation even when the two networks have the same number of hidden units.
The introductory model normalizes the flattened input and the outputs of its two activated hidden layers. The first numbered example expresses those activations as separate layers, with the same hidden-layer ordering. The second numbered example omits the normalization of the flattened input. The third turns off the two hidden Dense biases and places normalization between each hidden Dense layer and its activation. Its output Dense layer still uses a bias.
When is a bias redundant? During training, adding the same bias to every example in one feature also shifts that feature’s batch mean. Subtracting the shifted mean cancels that bias. This reasoning applies when normalization directly follows the linear operation. An intervening ReLU can change which entries become zero, so moving the normalization across it changes the argument.
A four-batch experiment with the moving mean
Start one feature’s stored mean at zero. Give the layer four consecutive training batches, each with mean 10. For each update, multiply the previous stored mean by momentum, then add the current batch mean multiplied by one minus momentum. The first batch has the same mean in both experiments. Only the weight assigned to the previous estimate changes.
For momentum 0.5, the first update puts the stored mean halfway between 0 and 10, at 5. On the next update, halfway between 5 and 10 is 7.5. For momentum 0.9, the first update moves only to 1. The next gap is 9, and moving one tenth of that gap brings the stored mean to 1.9. Use this pair of steps to predict which curve should sit higher before looking at the figure.
| Batches seen | Momentum 0.5 | Momentum 0.9 |
|---|---|---|
| 0 | 0 | 0 |
| 1 | 5 | 1 |
| 2 | 7.5 | 1.9 |
| 3 | 8.75 | 2.71 |
| 4 | 9.375 | 3.439 |

A larger momentum smooths the estimate but takes longer to catch up when the feature distribution changes. During a training forward pass it is still the current batch’s moments that normalize the values. Inference uses the stored moments. If a deployed model behaves differently on single examples, verify the mode as well as how representative the training batches were. The table isolates the mean update; a real normalization layer also maintains a moving variance.
Check your understanding
If the next batch also has mean 10, where does the 0.5 curve go?
It moves halfway from 9.375 to 10, reaching 9.6875. The gap halves with every additional batch in this experiment.
Does a batch of one always mean one observation per feature?
A dense input with one row supplies one value per feature. A convolutional input can still supply several spatial positions per channel even with one image. The chosen feature axis and the input shape together determine how many values contribute.
Do these four models establish which placement trains fastest?
They illustrate different computations. Comparing convergence would require training on the same task with controlled data splits, initialization, optimizer and schedule, then examining validation performance and training time. The checks for this lesson exercised the model definitions and forward passes.
Sources and interpretation
Ioffe and Szegedy introduced internal covariate shift as a motivation for batch normalization. Later experiments by Santurkar and colleagues examined smoother optimization as an explanation for its benefits. Treat the historical benefits list as motivation. Whether a particular network trains faster, needs dropout or tolerates a larger learning rate is an experimental question.
- Ioffe and Szegedy, original batch normalization paper
- Keras BatchNormalization reference for momentum, axis and inference
- Santurkar and colleagues, How Does Batch Normalization Help Optimization?
Read the original notation in source order
\[
\mu_B = \frac{1}{m_B} \sum_{i=1}^{m_B} x^{(i)}
\]
\(\mu_B\)
\(\sigma_B^2\)
\[
\hat{x}^{(i)} = \frac{x^{(i)} - \mu_B}{\sqrt{\sigma_B^2 + \epsilon}}
\]
\(\epsilon\)
\(\gamma\)
\(\beta\)
\[
z^{(i)} = \gamma \hat{x}^{(i)} + \beta
\]
\(\gamma\)
\(\beta\)
\[
\mu_B = \frac{1}{m_B} \sum_{i=1}^{m_B} x^{(i)}
\]
\[
\sigma_B^2 = \frac{1}{m_B} \sum_{i=1}^{m_B} \left(x^{(i)} - \mu_B\right)^2
\]
\[
\hat{x}^{(i)} = \frac{x^{(i)} - \mu_B}{\sqrt{\sigma_B^2 + \epsilon}}
\]
\(\epsilon\)
\[
z^{(i)} = \gamma \hat{x}^{(i)} + \beta
\]
\(\gamma\)
\(\beta\)
\(\beta\)
\(\gamma\)
\(\beta\)