# Randomness in weight init is not guaranteed on linux

**URL:** <https://discuss.pytorch.org/t/randomness-in-weight-init-is-not-guaranteed-on-linux/148380>\
**Category:** Uncategorized\
**Created:** [April 5, 2022, 8:07pm UTC](https://discuss.pytorch.org/t/randomness-in-weight-init-is-not-guaranteed-on-linux/148380 "2022-04-05T20:07:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alon](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alon/32/5661_2.png) [@Alon](https://discuss.pytorch.org/u/Alon)\
**Post date:** [April 5, 2022, 8:07pm UTC](https://discuss.pytorch.org/t/randomness-in-weight-init-is-not-guaranteed-on-linux/148380/1 "2022-04-05T20:07:19Z")

</div>

Following this closed issue: [np.random generates the same random numbers for each data batch · Issue #5059 · pytorch/pytorch (github.com)](https://github.com/pytorch/pytorch/issues/5059)

We know that NumPy on Linux (in contrary to Windows) uses the same seed every time unless manually set. The issue was closed by adding a re-seeding to the DataLoader  
(see [[DataLoader] Add Numpy seeding to worker of DataLoader by ejguan · Pull Request #56488 · pytorch/pytorch (github.com)](https://github.com/pytorch/pytorch/pull/56488))

However, as far as I can tell, the issue still exists in the weight initialization when creating a model or when calling reset\_parameters() to reset the weights of a module.

Would it be possible to implement the seeding in the reset\_parameters() methods?  
Otherwise to ensure randomness across repeats on Linux, I think that a manual NumPy random seed should be forced.

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [April 5, 2022, 11:40pm UTC](https://discuss.pytorch.org/t/randomness-in-weight-init-is-not-guaranteed-on-linux/148380/2 "2022-04-05T23:40:14Z")

</div>

The `.reset_parameters()` methods should not depend on numpy (and would break if numpy is not installed as an optional dependency). Seeding PyTorch should be enough to get reproducible results.  
Could you post a code snippet showing the issue you are hitting, please?

---

<div class="post-metadata">

**Author:** ![Alon](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alon/32/5661_2.png) [@Alon](https://discuss.pytorch.org/u/Alon)\
**Post date:** [April 11, 2022, 6:14pm UTC](https://discuss.pytorch.org/t/randomness-in-weight-init-is-not-guaranteed-on-linux/148380/3 "2022-04-11T18:14:51Z")

</div>

Upon writing a minimized code snippet to reproduce this, I found that I had an import in my code to an old (and unused) python file that had `torch.manual_seed(2021)` under the imports lines (and not inside a function) which subsequently caused all following torch functions to use this seed 😊  
I think it’s a worthwhile cautionary tale so it may be of some value if anyone ever reads this.
