Python / PyTorch Fundamentals Interview Questions
What is weight initialization in PyTorch and why does it matter?
How a network's weights are initialised at the start of training significantly affects whether training converges quickly, slowly, or not at all. PyTorch's default initialisation (Kaiming uniform for Linear/Conv layers) works well in most cases, but understanding the principles helps when debugging training issues.
import torch import torch.nn as nn # PyTorch default: Linear layers use Kaiming Uniform initialisation layer = nn.Linear(256, 128) print(layer.weight.std().item()) # approximately sqrt(2/256) â 0.088 # Explicit initialisation methods def init_weights(m): if isinstance(m, nn.Linear): # Xavier/Glorot â good for Tanh/Sigmoid activations nn.init.xavier_uniform_(m.weight) # He/Kaiming â good for ReLU-family activations (PyTorch default) # nn.init.kaiming_uniform_(m.weight, nonlinearity="relu") nn.init.zeros_(m.bias) model = nn.Sequential( nn.Linear(784, 256), nn.ReLU(), nn.Linear(256, 128), nn.ReLU(), nn.Linear(128, 10), ) model.apply(init_weights) # applies init_weights to every sub-module # Why initialisation matters: too small â vanishing activations # too large â exploding activations, especially in deep nets x = torch.randn(100, 784) for layer in model: x = layer(x) if hasattr(layer, "weight"): print(f"{layer}: activation std={x.std().item():.4f}") # With good init, std should stay roughly stable across layers # Custom initialisation from scratch with torch.no_grad(): layer.weight.normal_(mean=0.0, std=0.02) # common for transformer init layer.bias.zero__()
| Method | Formula (roughly) | Best for |
|---|---|---|
| Xavier/Glorot | Var = 2/(fan_in+fan_out) | Tanh, Sigmoid activations |
| Kaiming/He (PyTorch default for Linear) | Var = 2/fan_in | ReLU, LeakyReLU activations |
| Zero init | All weights = 0 | NEVER for weights — breaks symmetry; OK for biases |
| Small normal (std≈0.02) | N(0, 0.02²) | Transformer architectures (BERT, GPT) |
More Related questions...