提交 b90ec1d2 authored 作者: Pascal Lamblin's avatar Pascal Lamblin

Use utt.fetch_seed() instead of a fixed seed in test_randomstreams

上级 d5701303
...@@ -6,16 +6,19 @@ import numpy ...@@ -6,16 +6,19 @@ import numpy
from theano.tensor.randomstreams import RandomStreams, raw_random from theano.tensor.randomstreams import RandomStreams, raw_random
from theano.compile import Module, Method, Member from theano.compile import Module, Method, Member
from theano.tests import unittest_tools from theano.tests import unittest_tools as utt
from theano import tensor from theano import tensor
from theano import compile, gof from theano import compile, gof
class T_RandomStreams(unittest.TestCase): class T_RandomStreams(unittest.TestCase):
def setUp(self):
utt.seed_rng()
def test_basics(self): def test_basics(self):
m = Module() m = Module()
m.random = RandomStreams(234) m.random = RandomStreams(utt.fetch_seed())
m.fn = Method([], m.random.uniform((2,2))) m.fn = Method([], m.random.uniform((2,2)))
m.gn = Method([], m.random.normal((2,2))) m.gn = Method([], m.random.normal((2,2)))
made = m.make() made = m.make()
...@@ -26,7 +29,7 @@ class T_RandomStreams(unittest.TestCase): ...@@ -26,7 +29,7 @@ class T_RandomStreams(unittest.TestCase):
gn_val0 = made.gn() gn_val0 = made.gn()
rng_seed = numpy.random.RandomState(234).randint(2**30) rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = numpy.random.RandomState(int(rng_seed)) #int() is for 32bit rng = numpy.random.RandomState(int(rng_seed)) #int() is for 32bit
#print fn_val0 #print fn_val0
...@@ -42,12 +45,12 @@ class T_RandomStreams(unittest.TestCase): ...@@ -42,12 +45,12 @@ class T_RandomStreams(unittest.TestCase):
m.random = RandomStreams(234) m.random = RandomStreams(234)
m.fn = Method([], m.random.uniform((2,2))) m.fn = Method([], m.random.uniform((2,2)))
made = m.make() made = m.make()
made.random.initialize(seed=888) made.random.initialize(seed=utt.fetch_seed())
fn_val0 = made.fn() fn_val0 = made.fn()
fn_val1 = made.fn() fn_val1 = made.fn()
rng_seed = numpy.random.RandomState(888).randint(2**30) rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = numpy.random.RandomState(int(rng_seed)) #int() is for 32bit rng = numpy.random.RandomState(int(rng_seed)) #int() is for 32bit
#print fn_val0 #print fn_val0
...@@ -65,12 +68,12 @@ class T_RandomStreams(unittest.TestCase): ...@@ -65,12 +68,12 @@ class T_RandomStreams(unittest.TestCase):
made = m.make() made = m.make()
made.random.initialize(seed=789) made.random.initialize(seed=789)
made.random.seed(888) made.random.seed(utt.fetch_seed())
fn_val0 = made.fn() fn_val0 = made.fn()
fn_val1 = made.fn() fn_val1 = made.fn()
rng_seed = numpy.random.RandomState(888).randint(2**30) rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = numpy.random.RandomState(int(rng_seed)) #int() is for 32bit rng = numpy.random.RandomState(int(rng_seed)) #int() is for 32bit
#print fn_val0 #print fn_val0
...@@ -90,7 +93,7 @@ class T_RandomStreams(unittest.TestCase): ...@@ -90,7 +93,7 @@ class T_RandomStreams(unittest.TestCase):
made = m.make() made = m.make()
made.random.initialize(seed=789) made.random.initialize(seed=789)
made.random.seed(888) made.random.seed(utt.fetch_seed())
rng = numpy.random.RandomState() rng = numpy.random.RandomState()
rng.set_state(made.random[out.rng].get_state()) rng.set_state(made.random[out.rng].get_state())
...@@ -115,7 +118,7 @@ class T_RandomStreams(unittest.TestCase): ...@@ -115,7 +118,7 @@ class T_RandomStreams(unittest.TestCase):
made.random.seed(888) made.random.seed(888)
# then replace the rng of the stream we care about via setitem # then replace the rng of the stream we care about via setitem
realseed = 823874 realseed = utt.fetch_seed()
rng = numpy.random.RandomState(realseed) rng = numpy.random.RandomState(realseed)
made.random[out.rng] = numpy.random.RandomState(realseed) made.random[out.rng] = numpy.random.RandomState(realseed)
...@@ -131,7 +134,7 @@ class T_RandomStreams(unittest.TestCase): ...@@ -131,7 +134,7 @@ class T_RandomStreams(unittest.TestCase):
def test_multiple(self): def test_multiple(self):
M = Module() M = Module()
M.random = RandomStreams(234) M.random = RandomStreams(utt.fetch_seed())
out = M.random.uniform((2,2)) out = M.random.uniform((2,2))
M.m2 = Module() M.m2 = Module()
M.m2.random = M.random M.m2.random = M.random
...@@ -176,7 +179,7 @@ class T_RandomStreams(unittest.TestCase): ...@@ -176,7 +179,7 @@ class T_RandomStreams(unittest.TestCase):
"""Test that RandomStreams.uniform generates the same results as numpy""" """Test that RandomStreams.uniform generates the same results as numpy"""
# Check over two calls to see if the random state is correctly updated. # Check over two calls to see if the random state is correctly updated.
m = Module() m = Module()
m.random = RandomStreams(234) m.random = RandomStreams(utt.fetch_seed())
m.fn = Method([], m.random.uniform((2,2), -1, 1)) m.fn = Method([], m.random.uniform((2,2), -1, 1))
made = m.make() made = m.make()
...@@ -186,7 +189,7 @@ class T_RandomStreams(unittest.TestCase): ...@@ -186,7 +189,7 @@ class T_RandomStreams(unittest.TestCase):
print fn_val0 print fn_val0
print fn_val1 print fn_val1
rng_seed = numpy.random.RandomState(234).randint(2**30) rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = numpy.random.RandomState(int(rng_seed)) #int() is for 32bit rng = numpy.random.RandomState(int(rng_seed)) #int() is for 32bit
numpy_val0 = rng.uniform(-1, 1, size=(2,2)) numpy_val0 = rng.uniform(-1, 1, size=(2,2))
...@@ -201,7 +204,7 @@ class T_RandomStreams(unittest.TestCase): ...@@ -201,7 +204,7 @@ class T_RandomStreams(unittest.TestCase):
"""Test that RandomStreams.normal generates the same results as numpy""" """Test that RandomStreams.normal generates the same results as numpy"""
# Check over two calls to see if the random state is correctly updated. # Check over two calls to see if the random state is correctly updated.
m = Module() m = Module()
m.random = RandomStreams(234) m.random = RandomStreams(utt.fetch_seed())
m.fn = Method([], m.random.normal((2,2), -1, 2)) m.fn = Method([], m.random.normal((2,2), -1, 2))
made = m.make() made = m.make()
...@@ -209,7 +212,7 @@ class T_RandomStreams(unittest.TestCase): ...@@ -209,7 +212,7 @@ class T_RandomStreams(unittest.TestCase):
fn_val0 = made.fn() fn_val0 = made.fn()
fn_val1 = made.fn() fn_val1 = made.fn()
rng_seed = numpy.random.RandomState(234).randint(2**30) rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = numpy.random.RandomState(int(rng_seed)) #int() is for 32bit rng = numpy.random.RandomState(int(rng_seed)) #int() is for 32bit
numpy_val0 = rng.normal(-1, 2, size=(2,2)) numpy_val0 = rng.normal(-1, 2, size=(2,2))
numpy_val1 = rng.normal(-1, 2, size=(2,2)) numpy_val1 = rng.normal(-1, 2, size=(2,2))
...@@ -221,7 +224,7 @@ class T_RandomStreams(unittest.TestCase): ...@@ -221,7 +224,7 @@ class T_RandomStreams(unittest.TestCase):
"""Test that RandomStreams.random_integers generates the same results as numpy""" """Test that RandomStreams.random_integers generates the same results as numpy"""
# Check over two calls to see if the random state is correctly updated. # Check over two calls to see if the random state is correctly updated.
m = Module() m = Module()
m.random = RandomStreams(234) m.random = RandomStreams(utt.fetch_seed())
m.fn = Method([], m.random.random_integers((20,20), -5, 5)) m.fn = Method([], m.random.random_integers((20,20), -5, 5))
made = m.make() made = m.make()
...@@ -229,7 +232,7 @@ class T_RandomStreams(unittest.TestCase): ...@@ -229,7 +232,7 @@ class T_RandomStreams(unittest.TestCase):
fn_val0 = made.fn() fn_val0 = made.fn()
fn_val1 = made.fn() fn_val1 = made.fn()
rng_seed = numpy.random.RandomState(234).randint(2**30) rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = numpy.random.RandomState(int(rng_seed)) #int() is for 32bit rng = numpy.random.RandomState(int(rng_seed)) #int() is for 32bit
numpy_val0 = rng.random_integers(-5, 5, size=(20,20)) numpy_val0 = rng.random_integers(-5, 5, size=(20,20))
numpy_val1 = rng.random_integers(-5, 5, size=(20,20)) numpy_val1 = rng.random_integers(-5, 5, size=(20,20))
...@@ -238,10 +241,10 @@ class T_RandomStreams(unittest.TestCase): ...@@ -238,10 +241,10 @@ class T_RandomStreams(unittest.TestCase):
assert numpy.all(fn_val1 == numpy_val1) assert numpy.all(fn_val1 == numpy_val1)
def test_permutation(self): def test_permutation(self):
"""Test that RandomStreams.uniform generates the same results as numpy""" """Test that RandomStreams.permutation generates the same results as numpy"""
# Check over two calls to see if the random state is correctly updated. # Check over two calls to see if the random state is correctly updated.
m = Module() m = Module()
m.random = RandomStreams(234) m.random = RandomStreams(utt.fetch_seed())
m.fn = Method([], m.random.permutation((20,), 10)) m.fn = Method([], m.random.permutation((20,), 10))
made = m.make() made = m.make()
...@@ -249,7 +252,7 @@ class T_RandomStreams(unittest.TestCase): ...@@ -249,7 +252,7 @@ class T_RandomStreams(unittest.TestCase):
fn_val0 = made.fn() fn_val0 = made.fn()
fn_val1 = made.fn() fn_val1 = made.fn()
rng_seed = numpy.random.RandomState(234).randint(2**30) rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = numpy.random.RandomState(int(rng_seed)) #int() is for 32bit rng = numpy.random.RandomState(int(rng_seed)) #int() is for 32bit
# rng.permutation outputs one vector at a time, so we iterate. # rng.permutation outputs one vector at a time, so we iterate.
...@@ -263,7 +266,7 @@ class T_RandomStreams(unittest.TestCase): ...@@ -263,7 +266,7 @@ class T_RandomStreams(unittest.TestCase):
"""Test that RandomStreams.multinomial generates the same results as numpy""" """Test that RandomStreams.multinomial generates the same results as numpy"""
# Check over two calls to see if the random state is correctly updated. # Check over two calls to see if the random state is correctly updated.
m = Module() m = Module()
m.random = RandomStreams(234) m.random = RandomStreams(utt.fetch_seed())
m.fn = Method([], m.random.multinomial((20,20), 1, [0.1]*10)) m.fn = Method([], m.random.multinomial((20,20), 1, [0.1]*10))
made = m.make() made = m.make()
...@@ -271,7 +274,7 @@ class T_RandomStreams(unittest.TestCase): ...@@ -271,7 +274,7 @@ class T_RandomStreams(unittest.TestCase):
fn_val0 = made.fn() fn_val0 = made.fn()
fn_val1 = made.fn() fn_val1 = made.fn()
rng_seed = numpy.random.RandomState(234).randint(2**30) rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = numpy.random.RandomState(int(rng_seed)) #int() is for 32bit rng = numpy.random.RandomState(int(rng_seed)) #int() is for 32bit
numpy_val0 = rng.multinomial(1, [0.1]*10, size=(20,20)) numpy_val0 = rng.multinomial(1, [0.1]*10, size=(20,20))
numpy_val1 = rng.multinomial(1, [0.1]*10, size=(20,20)) numpy_val1 = rng.multinomial(1, [0.1]*10, size=(20,20))
...@@ -287,13 +290,14 @@ class T_RandomStreams(unittest.TestCase): ...@@ -287,13 +290,14 @@ class T_RandomStreams(unittest.TestCase):
# Note that this differs from numpy.random.shuffle, where all the # Note that this differs from numpy.random.shuffle, where all the
# elements of the matrix are shuffled. # elements of the matrix are shuffled.
mm = Module() mm = Module()
mm.random = RandomStreams(234) mm.random = RandomStreams(utt.fetch_seed())
m_input = tensor.dmatrix() m_input = tensor.dmatrix()
mm.f = Method([m_input], mm.random.shuffle_row_elements(m_input)) mm.f = Method([m_input], mm.random.shuffle_row_elements(m_input))
mmade = mm.make() mmade = mm.make()
mmade.random.initialize() mmade.random.initialize()
val_rng = numpy.random.RandomState(unittest_tools.fetch_seed()) # Generate the elements to be shuffled
val_rng = numpy.random.RandomState(utt.fetch_seed()+42)
in_mval = val_rng.uniform(-2, 2, size=(20,5)) in_mval = val_rng.uniform(-2, 2, size=(20,5))
fn_mval0 = mmade.f(in_mval) fn_mval0 = mmade.f(in_mval)
fn_mval1 = mmade.f(in_mval) fn_mval1 = mmade.f(in_mval)
...@@ -304,7 +308,7 @@ class T_RandomStreams(unittest.TestCase): ...@@ -304,7 +308,7 @@ class T_RandomStreams(unittest.TestCase):
assert not numpy.all(in_mval == fn_mval1) assert not numpy.all(in_mval == fn_mval1)
assert not numpy.all(fn_mval0 == fn_mval1) assert not numpy.all(fn_mval0 == fn_mval1)
rng_seed = numpy.random.RandomState(234).randint(2**30) rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = numpy.random.RandomState(int(rng_seed)) rng = numpy.random.RandomState(int(rng_seed))
numpy_mval0 = in_mval.copy() numpy_mval0 = in_mval.copy()
numpy_mval1 = in_mval.copy() numpy_mval1 = in_mval.copy()
...@@ -319,7 +323,7 @@ class T_RandomStreams(unittest.TestCase): ...@@ -319,7 +323,7 @@ class T_RandomStreams(unittest.TestCase):
# On vectors, the behaviour is the same as numpy.random.shuffle, # On vectors, the behaviour is the same as numpy.random.shuffle,
# except that it does not work in place, but returns a shuffled vector. # except that it does not work in place, but returns a shuffled vector.
vm = Module() vm = Module()
vm.random = RandomStreams(234) vm.random = RandomStreams(utt.fetch_seed())
v_input = tensor.dvector() v_input = tensor.dvector()
vm.f = Method([v_input], vm.random.shuffle_row_elements(v_input)) vm.f = Method([v_input], vm.random.shuffle_row_elements(v_input))
vmade = vm.make() vmade = vm.make()
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论