A colleague adds np.random.seed(42) and torch.manual_seed(42) at the top of a training script before any weight initialization or data shuffling happens. Why bother setting a random seed like this?
Select an answer to reveal the explanation.
Short Explanation and Infographic
Deep learning has randomness baked in everywhere — how weights get initialized, how batches get shuffled, which neurons dropout knocks out. That randomness is normally driven by a pseudo-random number generator, and setting a seed just tells that generator 'start from this exact same starting point every time.' Run the script twice with the same seed and you get the same weight initialization, the same shuffle order, the same dropout mask — same results, which is huge for debugging and for anyone trying to reproduce your work. So 'ensures reproducibility' is correct. It doesn't disable randomness, it just makes the randomness deterministic and repeatable. It has zero effect on training speed. And it definitely doesn't guarantee better accuracy — a seed just controls which random path gets taken, good or bad, it doesn't make the model smarter.
Full explanation below image
Full Explanation
Setting a random seed via functions like np.random.seed(), torch.manual_seed(), or tf.random.set_seed() initializes the underlying pseudo-random number generator to a fixed, known starting state. Because computers generate 'randomness' deterministically from an internal seed value, fixing that seed means every subsequent call to a random operation — weight initialization, shuffling the order of training samples, applying dropout masks, sampling random augmentations — will produce the exact same sequence of values every time the script is run, as long as the seed and the order of random calls are unchanged. This is invaluable for reproducibility: it allows a researcher to rerun an experiment and get identical results, allows teammates to reproduce reported results, and makes debugging far easier because a bug that appears on one run will reliably reappear on the next run with the same seed rather than possibly vanishing due to different random luck.
Setting a seed does not disable randomness or the mechanisms that rely on it, such as dropout or data augmentation. Those operations still occur exactly as configured; the only difference is that the specific random values they use become fixed and repeatable rather than different on every run. Dropout still randomly zeroes activations, augmentation still applies random transformations — but which specific units get dropped or which specific transformation gets applied will be the same across runs with the same seed.
Setting a seed has no effect on training speed. It does not skip any computation; it simply fixes the random number stream that many operations already consume, with no change to the amount of forward/backward computation performed.
Setting a seed also does not guarantee higher accuracy. The seed determines which particular random initialization and random ordering occurs, and different seeds can lead to slightly different final performance purely by chance, but there is no seed value that inherently produces better results — a fixed seed is about consistency and reproducibility, not optimization. In practice, note that full determinism across hardware (especially GPUs) sometimes requires additional configuration beyond just setting a seed, since some GPU operations are non-deterministic by default for performance reasons.