
Source: Delving Deep into Rectifiers: Surpassing Human-Level Performance on ImageNet Classification by He et.al.
Introduction
Kaiming Initialization, also known as He Initialization, was introduced by Kaiming He et al. in the 2015 paper “Delving Deep into Rectifiers: Surpassing Human-Level Performance on ImageNet Classification”. It was designed specifically for deep neural networks that use ReLU (Rectified Linear Unit) and its variants, addressing the shortcomings of Xavier Initialization when used with non-saturating activation functions.
Why Do We Need Kaiming Initialization?
Xavier Initialization assumes activations are symmetric around zero (e.g., tanh), but ReLU outputs only non-negative values, breaking this assumption. When using ReLU activations, which have become increasingly popular compared to sigmoid and tanh, Xavier initialization can lead to shrinking activations and vanishing gradients.
Kaiming Initialization compensates for this by scaling the weights based on the number of input units (fan_in) only, ensuring that the variance of activations remains consistent across layers even when ReLU zeroes out negative inputs.
Kaiming Initialization Formula
For a weight matrix W, Kaiming Initialization sets:
Where $n_{\text{in}}$ is the number of input units to a layer.
This is typically implemented with either a uniform or normal distribution.
Motivation Behind Kaiming Initialization
In deep neural networks, especially those using ReLU activation functions, the outputs of neurons become sparse because ReLU zeroes out all negative values. This introduces an asymmetry in the activation distribution and reduces the variance of the outputs, which can lead to vanishing gradients during backpropagation.
Let’s explore this more formally. For a neuron computing:
Where:
- $w_i$ are the weights,
- $x_i$ are the inputs, assumed to be i.i.d. with zero mean and variance $\text{Var}(x)$,
- $z$ is the pre-activation output.
Forward Propagation
When ReLU is applied after $z$, roughly half of the values are zeroed out (since ReLU$(z) = \max(0, z)$). Thus, if $z$ has a symmetric distribution around 0:
To maintain variance across layers and avoid shrinking activations, Kaiming Initialization compensates for this drop by doubling the variance of the weights compared to Xavier:
This ensures that:
So the magnitude of activations remains stable throughout forward propagation.
Backward Propagation
The backward pass follows a similar logic. Since gradients also pass through ReLU, which suppresses half the inputs, their variance would shrink as well. By using the same initialization,
the gradients’ scale is preserved across layers, avoiding both vanishing and exploding gradients.
Distribution Choice
- If weights are drawn from a normal distribution:
- If drawn from a uniform distribution:
To understand why uniform distribution has a formula like this:
“3” comes from the variance formula of a uniform distribution:
The factor “2” compensates for the variance reduction caused by ReLU
The denominator 6 = 2 × 3, combining ReLU compensation and uniform variance formula
These choices ensure stable signal propagation through deep networks with ReLU or similar activations.
Code Example
This code defines a simple feedforward neural network (ReLUNet) with three fully connected layers using ReLU activation functions. It applies Kaiming (He) Initialization to the weights of each layer using nn.init.kaiming_uniform_, which is specifically designed to work well with ReLU activations by preserving variance across layers and promoting stable training.
import torch
import torch.nn as nn
import torch.nn.functional as F
class ReLUNet(nn.Module):
def __init__(self):
super(ReLUNet, self).__init__()
self.fc1 = nn.Linear(784, 256)
self.fc2 = nn.Linear(256, 128)
self.fc3 = nn.Linear(128, 10)
# Apply Kaiming Initialization (for ReLU)
nn.init.kaiming_uniform_(self.fc1.weight, nonlinearity='relu')
nn.init.kaiming_uniform_(self.fc2.weight, nonlinearity='relu')
nn.init.kaiming_uniform_(self.fc3.weight, nonlinearity='relu')
def forward(self, x):
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
return self.fc3(x)
# Example usage
model = ReLUNet()When to Use Kaiming Initialization?
Appropriate Use Cases
- Using ReLU or its variants (e.g., Leaky ReLU, GELU): Kaiming initialization is specifically designed for non-linearities like ReLU, which zero out negative values. It compensates for this by scaling the weights based on the number of input units (fan-in), helping maintain the variance of activations across layers.
- The layer’s fan-in dominates signal propagation behavior: Kaiming uses only the number of input units (fan-in) to scale weight variance. This is ideal when each layer’s output is more influenced by the number of inputs than outputs, as is typical in many feedforward or convolutional layers.
Inappropriate Use Cases
- When using symmetric activation functions such as tanh or sigmoid, the outputs are centered around zero and prone to saturation. Since Kaiming initialization is designed for ReLU-like nonlinearities, it does not account for this behavior and may lead to vanishing gradients. In these cases, Xavier initialization is more appropriate, as it balances both fan-in and fan-out to better preserve gradient flow.
- A natural question is: what if the activation functions change throughout the network? For example, if most layers use ReLU but the final layer uses a sigmoid or softmax for classification, will He initialization still work? In practice, He initialization remains suitable for the hidden layers with ReLU, while the output layer can be treated separately and initialized according to its own activation function.
Video Explanation
- In this video, Maziar Raissi explained about Kaiming initialization with extensive mathematical derivations (Runtime: 29 mins):
Related Articles:
