Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Same random seed for Numpy and PyTorch

I am trying to compare my functions in vanilla Python/Numpy to those of PyTorch. For that I would like to set the seed of both to make the results comparable.

Is it possible to set the seed for numpy like np.random.seed(x) and PyTorch like torch.manual_seed(x) so that both produce the same random numbers?

Right now, with the same x value np.random.uniform(0, 1) and torch.rand(1) produce different outputs.

like image 556
oezguensi Avatar asked Nov 24 '25 08:11

oezguensi


1 Answers

What do you think of this solution?

Instead of

np.random.seed(0)

np.random.rand(0, 1)

you could use

torch.manual_seed(0) 

torch.rand(1).numpy()

That way, you will have the same random numbers between PyTorch and NumPy, but still as a NumPy array if you want...

like image 66
Imahn Avatar answered Nov 26 '25 21:11

Imahn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!