-
Notifications
You must be signed in to change notification settings - Fork 356
Fixed #1153 Track Random Seed #1154
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
base: main
Are you sure you want to change the base?
Changes from all commits
2df389e
0171f9c
6fd085d
6b25f9a
35a3cad
c6378c4
81b13ab
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 | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,79 @@ | ||||||
| from contextlib import contextmanager | ||||||
|
|
||||||
| import numpy as np | ||||||
|
|
||||||
| # Note that an initial SEED = 0 is disallowed | ||||||
| # in order to account for unit testing | ||||||
| SEED = np.random.randint(1, 4_294_967_295, dtype=np.uint32) | ||||||
| RNG = np.random.RandomState(seed=SEED) | ||||||
|
|
||||||
|
|
||||||
| def set_seed(seed): | ||||||
| """ | ||||||
| Permanently set the RNG seed to a different value | ||||||
|
|
||||||
| Parameters | ||||||
| ---------- | ||||||
| seed : int | ||||||
| The random seed for (permanently) setting the random number generator to | ||||||
|
|
||||||
| Returns | ||||||
| ------- | ||||||
| None | ||||||
| """ | ||||||
| global SEED | ||||||
| global RNG | ||||||
| SEED = seed | ||||||
| RNG = np.random.RandomState(seed=SEED) | ||||||
|
|
||||||
|
|
||||||
| @contextmanager | ||||||
| def fix_seed(seed): | ||||||
| """ | ||||||
| A context manager for setting the RNG seed to a fixed, hardcoded, safe seed | ||||||
| and then returning the RNG back to its previous state prior to the seed change | ||||||
|
|
||||||
| This is typically used when you want to generate a specific random sequence once. | ||||||
| To repeat the same random sequence, use `fix_state` instead. If you are picking | ||||||
| a random seed directly before calling `fix_seed` then you probably want to use | ||||||
| `fix_state` instead! | ||||||
|
Collaborator
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. Can you please elaborate the last sentence?
|
||||||
|
|
||||||
| Parameters | ||||||
| ---------- | ||||||
| seed : int | ||||||
| The random seed for (temporarily) setting the random number generator to | ||||||
|
|
||||||
| Returns | ||||||
| ------- | ||||||
| None | ||||||
| """ | ||||||
| state = RNG.get_state() | ||||||
|
Collaborator
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. To be consistent with the context manager
Suggested change
|
||||||
| RNG.seed(seed) | ||||||
|
Collaborator
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. IIUC, the RNG state will be set to a fixed state in this context manager (similar to what
Collaborator
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. When you have a |
||||||
| try: | ||||||
| yield | ||||||
| finally: | ||||||
| RNG.set_state(state) | ||||||
|
|
||||||
|
|
||||||
| @contextmanager | ||||||
| def fix_state(): | ||||||
| """ | ||||||
| A context manager for setting the RNG state to a fixed, hardcoded, safe state | ||||||
| and then returning the RNG back to its previous state prior to the state change | ||||||
|
|
||||||
| This is typically used when you want to repeat the same random sequence more than | ||||||
| once. | ||||||
|
|
||||||
| Parameters | ||||||
| ---------- | ||||||
| None | ||||||
|
|
||||||
| Returns | ||||||
| ------- | ||||||
| None | ||||||
| """ | ||||||
| curr_state = RNG.get_state() | ||||||
| try: | ||||||
| yield | ||||||
| finally: | ||||||
| RNG.set_state(curr_state) | ||||||
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.
Next line shows
np.random.RandomState(seed=SEED). The numpy's doc provides the following statement regarding the value of seed:Since the value of
highinnp.random.randintis excluded (see doc), the paramhighshould be set to2**32 == 4_294_967_296