Changed unittest seeding behaviour (in code and documentation)

上级 fe89688e
...@@ -252,13 +252,15 @@ following: ...@@ -252,13 +252,15 @@ following:
>>> def setUp(self): >>> def setUp(self):
>>> unittest_tools.seed_rng() >>> unittest_tools.seed_rng()
>>> # OR ... call with an explicit seed >>> # OR ... call with an explicit seed
>>> unittest_tools.seed_rng(234234) >>> unittest_tools.seed_rng(234234) #use only if really necessary!
The behaviour of seed_rng is as follows: The behaviour of seed_rng is as follows:
* if the environment variable THEANO_UNITTEST_SEED is defined, it will be used to seed the random number generator (and override any seed provided by the user) * If an explicit seed is given, it will be used for seending numpy's rng.
* if THEANO_UNITTEST_SEED is not defined, the user-supplied seed will be used to seed the rng * If not, it will try to get a seed from the THEANO_UNITTEST_SEED variable.
* if THEANO_UNITTEST_SEED is not defined and no seed is given, the rng will be seeded with a random seed. * If THEANO_UNITTEST_SEED is set to "random", it will seed the rng. with None, which is equivalent to seeding with a random seed.
* If THEANO_UNITTEST_SEED is not defined, it will use a default seed of 666.
The main advantage of using unittest_tools.seed_rng is that it allows us to The main advantage of using unittest_tools.seed_rng is that it allows us to
change the seed used in the unitests, without having to manually edit all the change the seed used in the unitests, without having to manually edit all the
...@@ -267,13 +269,13 @@ changing the seed on every run (hence achieving a higher confidence that the ...@@ -267,13 +269,13 @@ changing the seed on every run (hence achieving a higher confidence that the
variables are correct), while still making sure unittests are deterministic. variables are correct), while still making sure unittests are deterministic.
Users who prefer their unittests to be random (when run on their local machine) Users who prefer their unittests to be random (when run on their local machine)
can simply undefine THEANO_UNITTEST_SEED. can simply set THEANO_UNITTEST_SEED to 'random'.
Similarly, to provide a seed to numpy.random.RandomState, simply use: Similarly, to provide a seed to numpy.random.RandomState, simply use:
>>> rng = numpy.random.RandomState(unittest_tools.fetch_seed()) >>> rng = numpy.random.RandomState(unittest_tools.fetch_seed())
>>> # OR providing an explicit seed >>> # OR providing an explicit seed
>>> rng = numpy.random.RandomState(unittest_tools.fetch_seed(1231)) >>> rng = numpy.random.RandomState(unittest_tools.fetch_seed(1231)) #again not recommended
Note that the ability to change the seed from one nosetest to another, is incompatible with the method of hard-coding the baseline variables (against which we compare the theano outputs). These must then be determined "algorithmically". Although this represents more work, the test suite will be better because of it. Note that the ability to change the seed from one nosetest to another, is incompatible with the method of hard-coding the baseline variables (against which we compare the theano outputs). These must then be determined "algorithmically". Although this represents more work, the test suite will be better because of it.
......
...@@ -155,8 +155,9 @@ automatic code generation, but that way is much, much slower. ...@@ -155,8 +155,9 @@ automatic code generation, but that way is much, much slower.
- `THEANO_UNITTEST_SEED`: - `THEANO_UNITTEST_SEED`:
An integer value specifying which seed should be used when An integer value specifying which seed should be used when
running unit tests. running unit tests. If this variable is not defined, the seed will default
Setting this value will make the unit tests deterministic. to 666. Users wishing for non-deterministic behaviour can set this
variable to 'random'.
Mac Mac
......
...@@ -4,7 +4,21 @@ import numpy ...@@ -4,7 +4,21 @@ import numpy
import os, sys import os, sys
def fetch_seed(pseed=None): def fetch_seed(pseed=None):
seed = os.getenv("THEANO_UNITTEST_SEED", pseed) """
Returns the seed to use for running the unit tests.
If an explicit seed is given, it will be used for seending numpy's rng.
If not, it will try to get a seed from the THEANO_UNITTEST_SEED variable.
If THEANO_UNITTEST_SEED is set to "random", it will seed the rng. with None,
which is equivalent to seeding with a random seed.
If THEANO_UNITTEST_SEED is not defined, it will use a default seed of 666.
Useful for seeding RandomState objects.
>>> rng = numpy.random.RandomState(unittest_tools.fetch_seed())
"""
seed = pseed or os.getenv("THEANO_UNITTEST_SEED", 666)
seed = None if seed=='random' else seed
try: try:
seed = int(seed) if seed else None seed = int(seed) if seed else None
except ValueError: except ValueError:
...@@ -15,6 +29,10 @@ def fetch_seed(pseed=None): ...@@ -15,6 +29,10 @@ def fetch_seed(pseed=None):
return seed return seed
def seed_rng(pseed=None): def seed_rng(pseed=None):
"""
Seeds numpy's random number generator with the value returned by fetch_seed.
Usage: unittest_tools.seed_rng()
"""
seed = fetch_seed(pseed) seed = fetch_seed(pseed)
if pseed and pseed!=seed: if pseed and pseed!=seed:
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论