Recommended resources
The freely available resources this lesson will direct you to.
- Batch Normalization - Part 3: Backpropagation & Inferencewww.youtube.com
- Dropout in Neural Network | Detailed Explanation with implementation in Python from Scratchwww.youtube.com
- The Math Behind Batch Normalizationtowardsdatascience.com
- Understanding the backward pass through Batch Normalization Layerkratzert.github.io
Introduction
In our last lesson, we explored L2 regularization, a technique that combats overfitting by penalizing the magnitude of a model's weights. This encourages the network to learn smoother, simpler functions. We saw how this penalty term leads directly to the "weight decay" update rule, where weights are multiplicatively shrunk at each training step.
Today, we shift our focus from controlling parameter values to two powerful techniques that manipulate the network in entirely different ways. We will investigate Dropout, which regularizes by altering the network's architecture at every step, and Batch Normalization, which stabilizes training by transforming the data as it flows between layers. While they have different primary goals—regularization vs. stabilization—both have become indispensable tools for training modern deep neural networks.
Our objective is to understand the precise mechanisms behind each technique, from their mathematical formulation to their practical implementation, and to clarify their distinct roles in building robust and efficient models.
Part 1: Dropout - Regularization by Randomness
In a large, complex network, it's possible for groups of neurons to "co-adapt," meaning they become highly reliant on each other to correct mistakes. This can lead to the network memorizing noise, as these specialized neuron cliques may not generalize well to unseen data.
Dropout offers a radical solution: what if we prevent neurons from reliably co-adapting by making their presence unpredictable?
The core idea of Dropout is to randomly set the outputs of some neurons to zero during each forward pass of the training phase. This has two profound effects:
- It forces each neuron to learn more robust features that are useful on their own, without relying on specific other neurons being active.
- It's akin to training an ensemble of many different, smaller neural networks. Since a different set of neurons is dropped at each step, we are implicitly averaging over an exponential number of thinned architectures.

The Mechanism of Inverted Dropout
A naive implementation of Dropout would simply zero-out neurons during training. However, this would create a mismatch between training and testing. At test time, all neurons are active, so the total input to the next layer would be significantly larger than it was during training.
To solve this, we use a technique called inverted dropout. During training, after zeroing out the dropped neurons, we scale up the activations of the remaining "active" neurons by the inverse of the probability of keeping a neuron.
Let's formalize this. Suppose we want to apply Dropout to a layer's activation vector with a keep_prob of .
-
Create a Mask: We generate a binary mask vector of the same shape as , where each element is drawn from a Bernoulli distribution, . Each element of is 1 with probability and 0 with probability .
-
Apply the Mask: We perform element-wise multiplication to drop neurons: .
-
Scale (Invert): We scale the result to maintain the expected output magnitude: .
This scaling ensures that the expected value of the layer's output is the same during training and testing. At test time, we simply use the full network without any dropout or scaling, making inference computationally identical to a standard network.
During backpropagation, the gradient is only passed through the neurons that were active (where the mask was 1). The scaling factor is also applied to the gradient to ensure the updates are of the correct magnitude.
The following video provides an excellent and complete walkthrough of Dropout, from the core intuition to a from-scratch Python implementation.
Dropout in Neural Network | Detailed Explanation with implementation in Python from Scratch
This video, "Dropout in Neural Network" by Learn With Jay, provides a detailed explanation and implementation.
Watch from the beginning to understand the intuition behind Dropout as a way to prevent co-adaptation and its similarity to L2 regularization in spreading out weights. Next, focus on the mathematical implementation in the forward pass from this section. Pay close attention to the creation of the mask and the crucial scaling step (inverted dropout). Then, see how this affects the backward pass from this part. Notice that the same mask and scaling are applied to the gradients. Finally, observe the "no-op" nature of Dropout at test time, explained from this segment.
Part 2: Batch Normalization - Stabilizing a Moving Target
As a network trains, the weights in the early layers are constantly updated. This causes the distribution of activations fed into later layers to continuously change—a problem known as Internal Covariate Shift. Each layer is trying to learn from a non-stationary input distribution, which is like trying to hit a moving target. This can destabilize training, forcing us to use lower learning rates and be very careful with weight initialization.
Batch Normalization (BN) addresses this problem by explicitly normalizing the inputs to each layer for every mini-batch.
The Mechanism of Batch Normalization
For a mini-batch of examples, BN transforms the activations for each feature independently. The process consists of two main steps:
-
Normalization:
- First, calculate the mean and variance of the activations for a specific feature across the current mini-batch .
- Then, normalize each activation to have zero mean and unit variance. An epsilon is added for numerical stability.
-
Scale and Shift:
- Forcing all layer inputs to have a fixed distribution might be too constraining. For example, for a sigmoid activation, this would force inputs into the linear regime around zero, reducing the network's non-linear capacity.
- To restore the network's expressive power, BN introduces two new learnable parameters per feature: a scale parameter and a shift parameter . These are updated via gradient descent just like the network's weights.
This allows the network to learn the optimal mean and variance for each layer's input. If the network finds it optimal, it can learn and to effectively undo the normalization and recover the original activations.

The following reading provides a concise mathematical summary of these steps.
The Math Behind Batch Normalization
This article from Towards Data Science provides a clear overview of the mathematics and mechanisms of Batch Normalization.
Please read the sections titled Normalization Step and Scale and Shift Step. Focus on understanding the two core equations: the normalization formula and the scale-and-shift transformation.
Backpropagation and Inference
The backward pass through a Batch Normalization layer is more complex than it might first appear. Because the mean and variance are computed across the entire mini-batch, the gradient of the loss with respect to a single input depends on all other inputs in that same batch. This introduces dependencies that span the entire batch.
For a rigorous, first-principles understanding of this gradient flow, the following resource provides an exceptional step-by-step breakdown using a computational graph.
Understanding the backward pass through Batch Normalization Layer
This classic blog post by Frederik Kratzert meticulously derives the backpropagation equations for Batch Normalization.
First, review the computational graph to visualize the forward pass. Next, read through the detailed, step-by-step derivation in the section The Backpass. You don't need to memorize every equation, but follow the logic of how the chain rule is applied at each node (summation, multiplication, inversion, etc.) to distribute the gradients back to the inputs. Finally, examine the naive implementation code to see how these derived steps translate directly into code.
A final critical point is how BN operates at inference time. Since we might be processing a single input, we can't compute a batch mean and variance. The solution is to estimate the population statistics during training.
- During training, the BN layer maintains an exponential moving average of the means () and variances () calculated from each mini-batch.
- At test time, these "frozen" running averages are used in place of batch-specific statistics for normalization. The learned and are used as usual.
This video expertly explains both the backpropagation derivation and the crucial training vs. inference distinction.
Batch Normalization - Part 3: Backpropagation & Inference
This video, "Batch Normalization - Part 3: Backpropagation & Inference," from the ML For Nerds channel, covers the advanced details.
For a dynamic walkthrough of the backpropagation derivation, watch from this segment. This complements the blog post with a verbal explanation of the chain rule applied to the BN computational graph. To understand the critical difference between training and inference, and the role of moving averages, watch from this part. This is a key practical aspect of using Batch Normalization.
Conclusion
We have now explored two fundamentally different but equally important techniques for training deep networks.
Key Takeaways:
-
Dropout is a regularization technique.
- Mechanism: It randomly sets neuron activations to zero during training and scales the rest using inverted dropout. At test time, it does nothing.
- Role: It prevents neuron co-adaptation, forcing the network to learn more robust, redundant features and acting as a form of model ensembling.
-
Batch Normalization is a training stabilization technique.
- Mechanism: It normalizes layer inputs per mini-batch to have zero mean and unit variance, then applies a learnable scale () and shift (). It uses moving averages of statistics for inference.
- Role: It mitigates internal covariate shift, allowing for higher learning rates, faster convergence, and reduced sensitivity to weight initialization. It also has a minor regularizing effect.
Preview of the Next Lesson:
Batch Normalization significantly reduces the network's sensitivity to the scale of its initial weights. However, it doesn't eliminate the need for careful initialization altogether. Poorly chosen initial weights can still lead to issues like vanishing or exploding gradients, especially in very deep networks without BN. In our next lesson, we will investigate this problem from first principles and derive modern weight initialization strategies like Xavier/Glorot and He initialization, which are designed to maintain a stable signal variance as data propagates through the network.