Python / PyTorch Fundamentals Interview Questions
What is the purpose of torch.manual_seed() and how do you ensure reproducibility in PyTorch?
PyTorch uses pseudo-random number generators for weight initialisation, dropout masks, data shuffling, and more. Setting seeds explicitly ensures experiments are reproducible — critical for debugging, comparing model variants fairly, and scientific rigor.
import torch
import numpy as np
import random
import os
def set_seed(seed: int = 42):
"""Set all relevant seeds for full reproducibility."""
random.seed(seed) # Python's random module
np.random.seed(seed) # NumPy
torch.manual_seed(seed) # PyTorch CPU
torch.cuda.manual_seed_all(seed) # PyTorch all GPUs
# Force deterministic algorithms (may be slower!)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False # disable auto-tuner (non-deterministic)
os.environ["PYTHONHASHSEED"] = str(seed)
set_seed(42)
# Verify reproducibility
model1 = torch.nn.Linear(10, 5)
set_seed(42)
model2 = torch.nn.Linear(10, 5)
print(torch.equal(model1.weight, model2.weight)) # True — identical init
# DataLoader reproducibility — also needs a worker_init_fn for num_workers > 0
def seed_worker(worker_id):
worker_seed = torch.initial_seed() % 2**32
np.random.seed(worker_seed)
random.seed(worker_seed)
generator = torch.Generator()
generator.manual_seed(42)
from torch.utils.data import DataLoader
loader = DataLoader(
dataset,
batch_size=32,
shuffle=True,
num_workers=4,
worker_init_fn=seed_worker, # seeds each worker process
generator=generator, # seeds the shuffling order
)| Source of randomness | How to control it |
|---|---|
| Weight initialisation | torch.manual_seed(seed) |
| Dropout masks | Covered by torch.manual_seed (same RNG stream) |
| Data shuffling | DataLoader(generator=torch.Generator().manual_seed(seed)) |
| Multi-worker DataLoader | worker_init_fn to seed each subprocess |
| GPU non-determinism | torch.backends.cudnn.deterministic = True |
| cuDNN auto-tuner | torch.backends.cudnn.benchmark = False |
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
