AI Logic

Glossary

Weight Initialization

Updated:

On the left, a graph of the output magnitude at each layer. The horizontal axis is the number of layers counted from the input (0 to 10), and the vertical axis is the standard deviation of the output (log scale). For linear layers with n_in = 100, a weight standard deviation σ of 0.2 doubles the magnitude at every layer (leading to exploding gradients), LeCun initialization with σ² = 1/n_in (σ = 0.1) keeps the magnitude constant, and σ = 0.05 halves it at every layer (leading to vanishing gradients). On the right, a top-to-bottom list of the main initialization methods: all zeros / constant, small random values, LeCun initialization W ~ N(0, 1/n_in), Xavier initialization W ~ N(0, 2/(n_in + n_out)), and He initialization (considers only n_in, for ReLU).

Description

Weight initialization is the process of setting the initial values of the weights before training starts.

Its purpose is to prevent the vanishing gradient problem and the exploding gradient problem in activation functions.

There are several initialization methods.

  1. Zero initialization / constant initialization

This method sets all weights to 0 or to a constant value.

Because many units end up with similar input-output relationships early in training, it is known to reduce the model's nonlinearity (and thus its expressive power).

  1. Random initialization

This method initializes the weights with small random values close to 0 to break the symmetry problem and achieve higher accuracy.

It reduces the tendency of units to learn the same features from the inputs, but saturation of the activation function can cause the vanishing gradient problem or slow down gradient descent.

  1. LeCun initialization

An initialization method proposed to counter the growth in variance that comes with a larger number of inputs and to prevent the vanishing gradient problem and the exploding gradient problem.

It is effective for differentiable activation functions.

W∼N(0,1nin)W \sim \mathcal{N}(0, \frac{1}{n_{in}})
  1. Xavier initialization

A method that initializes the weights so that the variance of the activation function outputs is equal across all layers.

It prevents the vanishing gradient problem and the exploding gradient problem, but it cannot be used with non-differentiable activation functions and does not help with the dying neuron problem.

W∼N(0,2nin+nout)W \sim \mathcal{N}(0, \frac{2}{n_{in} + n_{out}})

or

W∼U(−6nin+nout,6nin+nout)W \sim \mathcal{U} ( -\sqrt{\frac{6}{n_{in} + n_{out}}}, \sqrt{\frac{6}{n_{in} + n_{out}}} )
  1. He initialization

An initialization method that is effective for non-differentiable activation functions such as ReLU.

It considers only the inputs from the previous neurons (n_in) and is designed to compensate for the input information lost in functions such as ReLU.

He initialization sets the variance of the weights so that the magnitude of the input signals does not grow or shrink exponentially as layers are stacked.

Reference: WEIGHT INITIALIZATION TECHNIQUES FOR DEEP LEARNING ALGORITHMS IN REMOTE SENSING: RECENT TRENDS AND FUTURE PERSPECTIVES(2021)

PyTorch example

Show code
python
import torch
import torch.nn as nn
import torch.nn.init as init

# PyTorch
class NN(nn.Module):
  def __init__(self):
    super().__init__()
    self.linear = nn.Linear(64, 128)

    # Initialization (You can select 3 methods.)
    self.init_LeCun(self.linear)
    # self.init_Xavier(self.linear)
    # self.init_He(self.linear)

  # LeCun Initialization
  def init_LeCun(self, layer):
    init.normal_(layer.weight, mean=0.0, std=(1.0 / layer.in_features) ** 0.5)
    init.zeros_(layer.bias)

  # Xavier Initialization
  def init_Xavier(self, layer):
    init.xavier_normal_(layer.weight)
    init.zeros_(layer.bias)

  # He Initialization
  def init_He(self, layer):
    init.kaiming_normal_(layer.weight, mode='fan_in', nonlinearity='relu')
    init.zeros_(layer.bias)

  def forward(self, x):
    x = self.linear(x)
    return x

FAQ

Q. Why is weight initialization important?
A.

With improper initialization, vanishing or exploding gradients occur, and training stalls or becomes unstable. In deep neural networks in particular, the initial values strongly affect training performance.

Q. What are the representative initialization methods?
A.

They include Xavier (Glorot) initialization and He initialization. The former suits mainly Sigmoid and Tanh, and the latter suits ReLU-family activation functions.

Q. How should an initialization method be chosen?
A.

The basic rule is to choose based on the activation function: He initialization is recommended for the ReLU family, and Xavier initialization for Sigmoid and Tanh. When BatchNorm is used, the influence of the initial values is somewhat reduced.