-
Notifications
You must be signed in to change notification settings - Fork 7.1k
* Replace np.random by random #354
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
this would now cause breaking changes in users code if they were previously setting Perhaps a better solution would be to pass in a random state to the functions, and if it is Thoughts @fmassa ? |
What about linking seed to actually torch.manual_seed ? |
@ducha-aiki in this case all torchvision random stuff is needed to rewrite with torch.random what is much more breaking change than just drop np.random |
I think we need to have some uniformity in here, so we might need to go through a breaking change (which is very small though). Maybe we will need to pass a initial random state, which will be updated by the random transforms. def random_flip(img, random_state=None):
if random_state is not None:
torch.set_rng_state(random_state)
# perform the random operations using torch
...
# now update the random state
if random_state is not None:
random_state.copy_(torch.get_rng_state)
# and maybe set the torch rng state back to its original value But that's a bit annoying and might bring some additional overhead and maybe issues on multithreading behaving the same for different threads. |
I think this should be unified. Thanks! |
I use np.random in my dataset class, and noticed the issue on multithreading behaving the same for different threads. If I also use your transform functions that uses the standard random library, should I add a line to the worker_init_fn also, and actually I do not understand what's the problem behind the multithreading, can you give some explanation? |
@laoreja import multiprocessing as mp
import random
import numpy as np
def task():
print("-- Task --")
print(random.random(), np.random.rand())
print(random.random(), np.random.rand())
print("-- End Task --")
random.seed(12345)
np.random.seed(12345)
workers = [mp.Process(target=task, args=()) for i in range(4)]
print("Run")
for w in workers:
w.start() and the output
So, you do not need to add new seed for the workers if use HTH |
Thanks! |
To be able to fix random state with a single command:
instead of