-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Clean up random seed code #3645
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -407,7 +407,7 @@ def create_environment_factory( | |||||||
| env_path: Optional[str], | ||||||||
| docker_target_name: Optional[str], | ||||||||
| no_graphics: bool, | ||||||||
| seed: Optional[int], | ||||||||
| seed: int, | ||||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is always non-None. |
||||||||
| start_port: int, | ||||||||
| env_args: Optional[List[str]], | ||||||||
| ) -> Callable[[int, List[SideChannel]], BaseEnv]: | ||||||||
|
|
@@ -426,15 +426,12 @@ def create_environment_factory( | |||||||
| # container. | ||||||||
| # Navigate in docker path and find env_path and copy it. | ||||||||
| env_path = prepare_for_docker_run(docker_target_name, env_path) | ||||||||
| seed_count = 10000 | ||||||||
| seed_pool = [np.random.randint(0, seed_count) for _ in range(seed_count)] | ||||||||
|
|
||||||||
| def create_unity_environment( | ||||||||
| worker_id: int, side_channels: List[SideChannel] | ||||||||
| ) -> UnityEnvironment: | ||||||||
| env_seed = seed | ||||||||
| if not env_seed: | ||||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would only happen if you set ml-agents/ml-agents/mlagents/trainers/learn.py Lines 492 to 494 in 811825b
picked 0 for the seed. np.random's seed isn't set at this point, so if you set seed=0 you wouldn't get reproducible results. |
||||||||
| env_seed = seed_pool[worker_id % len(seed_pool)] | ||||||||
| # Make sure that each environment gets a different seed | ||||||||
| env_seed = seed + worker_id | ||||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See also https://www.johndcook.com/blog/2016/01/29/random-number-generator-seed-mistakes/ I don't know how to prove which would be better or worse here - the environments should diverge after a few steps, but seeding them the same feels strange for multiple environments.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think @harperj did some experiments with Walker and distributed training here - don't remember if there was a huge improvement but I remember the same-seeding being an issue in terms of diversity of experiences
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, actually this code has been problematic for a while but I just never got around to cleaning it up. In practice for most environments it is not an issue. I can't think of any reason we'd practically want all of the environments to be the same seed, so far we have just been fortunate that there are other sources of randomness which make it fine either way. |
||||||||
| return UnityEnvironment( | ||||||||
| file_name=env_path, | ||||||||
| worker_id=worker_id, | ||||||||
|
|
||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"no physics" does not imply "deterministic"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree. We called out physics before though just to let people know that the physics is non-determinstic. So just because you don't use a
Randomcall in your code, it doesn't mean you are guaranteed deterministic.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The inverse of the statement is also not true: physics simulations are not inherently non-deterministic (although it sounds like Unity's implementation is).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes 😄