Why is it required to set seeds separately for each library?

I have set the seed for torch in my code as:


Now, since I am importing numpy and tensorflow as well, is it required set the seeds for each library separately? If yes, why?

Yes, this is required since each library uses their own pseudorandom number generator, which needs to be seeded.

Thank you for the clarification.
So does the number I use matter? For example, since I used 3 for torch, should I use 3 for the other libraries also or it can be any random number?

No, I don’t think it matters since different PRNG implementations could be used as already seen in this small example:

# tensor([-2.0748])

# array([0.23747478])

Also note that not all libraries support the same backends (e.g. numpy doesn’t support the GPU) so a parity isn’t even possible for all use cases.

1 Like