How do I initialize weights in PyTorch?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Weight initialization has a direct effect on how easily a neural network trains. In PyTorch, you can rely on sensible defaults for many layers, but understanding when to apply Xavier, Kaiming, or custom initialization will help you avoid unstable gradients and slow convergence.
Why Initialization Matters
When weights start too small, signals shrink as they pass through layers and gradients can vanish. When weights start too large, activations and gradients can explode. Good initialization keeps the scale of activations in a useful range so optimization begins from a stable point.
PyTorch modules already initialize parameters when you create them, but those defaults are generic. If your model architecture or activation functions have specific needs, it is common to override them after constructing the model.
The weights exist as soon as the layers are created. Initialization means replacing those values with a strategy suited to the network.
Common Initialization Strategies
PyTorch exposes initialization helpers in torch.nn.init. Two of the most common choices are Xavier initialization and Kaiming initialization.
Xavier, also called Glorot initialization, works well for activations that keep values roughly centered, such as tanh. Kaiming, also called He initialization, is usually a better fit for ReLU-style networks because it preserves variance more effectively when half the activations may become zero.
This example initializes every nn.Linear layer with Kaiming uniform weights and zero bias terms. Zero bias is a common default because it does not create symmetry problems the way zero weights would.
If you are using tanh or sigmoid, Xavier is often a better choice:
Initializing Different Layer Types
Convolutional and recurrent layers also benefit from deliberate initialization. For example, convolutional layers in ReLU networks commonly use Kaiming initialization, while embeddings may use a small normal distribution.
The important part is matching the initialization to the layer behavior and activation pattern rather than applying one rule blindly everywhere.
Verifying That Initialization Happened
It is worth checking statistics after initialization, especially in experimental models:
You do not need exact target values, but wildly unexpected means or standard deviations usually signal a bug such as forgetting to call apply or accidentally reinitializing the wrong module.
Common Pitfalls
- Initializing all weights to zero prevents neurons in the same layer from learning different features.
- Using Xavier for a deep
ReLUstack can work, but Kaiming is usually the better default. - Forgetting biases when writing a custom initializer leaves part of the layer at its original default values.
- Reinitializing a pretrained model destroys learned weights, so custom initialization should happen before training, not after loading a checkpoint.
- Applying initialization rules by layer name rather than module type can break when the architecture changes.
Summary
- PyTorch provides initialization helpers in
torch.nn.initfor common strategies. - Kaiming initialization is a strong default for
ReLUnetworks. - Xavier initialization often fits
tanhand similar activations better. - Use
model.applyto walk the module tree and initialize each layer consistently. - Verify parameter statistics when debugging training instability or suspicious convergence.
Related reading
- How do I keep track of the time the CPU is used vs the GPUs for deep learning?
- How do I know if tensorflow using cuda and cudnn or not?
- How do I load a keras saved model with custom Optimizer
- How do I load a local model with torch.hub.load?
- How do I load custom image based datasets into Pytorch for use with a CNN?
- How do I print the model summary in PyTorch?
- How do I install tensorflow_text?
- How do I install TensorFlow's tensorboard?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.