提交 f1bf4253 authored 作者: amrithasuresh's avatar amrithasuresh

1. Updated numpy as np

2. Fixed indentation
上级 7e05e00e
...@@ -3,7 +3,7 @@ __docformat__ = "restructuredtext en" ...@@ -3,7 +3,7 @@ __docformat__ = "restructuredtext en"
import sys import sys
import unittest import unittest
import numpy import numpy as np
from theano.tensor import raw_random from theano.tensor import raw_random
from theano.tensor.shared_randomstreams import RandomStreams from theano.tensor.shared_randomstreams import RandomStreams
...@@ -27,12 +27,12 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -27,12 +27,12 @@ class T_SharedRandomStreams(unittest.TestCase):
g = function([], rv_n, no_default_updates=True) #Not updating rv_n.rng g = function([], rv_n, no_default_updates=True) #Not updating rv_n.rng
nearly_zeros = function([], rv_u + rv_u - 2 * rv_u) nearly_zeros = function([], rv_u + rv_u - 2 * rv_u)
assert numpy.all(f() != f()) assert np.all(f() != f())
assert numpy.all(g() == g()) assert np.all(g() == g())
assert numpy.all(abs(nearly_zeros()) < 1e-5) assert np.all(abs(nearly_zeros()) < 1e-5)
assert isinstance(rv_u.rng.get_value(borrow=True), assert isinstance(rv_u.rng.get_value(borrow=True),
numpy.random.RandomState) np.random.RandomState)
def test_basics(self): def test_basics(self):
random = RandomStreams(utt.fetch_seed()) random = RandomStreams(utt.fetch_seed())
...@@ -44,20 +44,20 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -44,20 +44,20 @@ class T_SharedRandomStreams(unittest.TestCase):
gn_val0 = gn() gn_val0 = gn()
rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30) rng_seed = np.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = numpy.random.RandomState(int(rng_seed)) # int() is for 32bit rng = np.random.RandomState(int(rng_seed)) # int() is for 32bit
# print fn_val0 # print fn_val0
numpy_val0 = rng.uniform(size=(2, 2)) numpy_val0 = rng.uniform(size=(2, 2))
numpy_val1 = rng.uniform(size=(2, 2)) numpy_val1 = rng.uniform(size=(2, 2))
# print numpy_val0 # print numpy_val0
assert numpy.allclose(fn_val0, numpy_val0) assert np.allclose(fn_val0, numpy_val0)
print(fn_val0) print(fn_val0)
print(numpy_val0) print(numpy_val0)
print(fn_val1) print(fn_val1)
print(numpy_val1) print(numpy_val1)
assert numpy.allclose(fn_val1, numpy_val1) assert np.allclose(fn_val1, numpy_val1)
def test_seed_fn(self): def test_seed_fn(self):
random = RandomStreams(234) random = RandomStreams(234)
...@@ -68,16 +68,16 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -68,16 +68,16 @@ class T_SharedRandomStreams(unittest.TestCase):
fn_val0 = fn() fn_val0 = fn()
fn_val1 = fn() fn_val1 = fn()
rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30) rng_seed = np.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = numpy.random.RandomState(int(rng_seed)) #int() is for 32bit rng = np.random.RandomState(int(rng_seed)) #int() is for 32bit
# print fn_val0 # print fn_val0
numpy_val0 = rng.uniform(size=(2, 2)) numpy_val0 = rng.uniform(size=(2, 2))
numpy_val1 = rng.uniform(size=(2, 2)) numpy_val1 = rng.uniform(size=(2, 2))
# print numpy_val0 # print numpy_val0
assert numpy.allclose(fn_val0, numpy_val0) assert np.allclose(fn_val0, numpy_val0)
assert numpy.allclose(fn_val1, numpy_val1) assert np.allclose(fn_val1, numpy_val1)
def test_getitem(self): def test_getitem(self):
...@@ -87,15 +87,15 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -87,15 +87,15 @@ class T_SharedRandomStreams(unittest.TestCase):
random.seed(utt.fetch_seed()) random.seed(utt.fetch_seed())
rng = numpy.random.RandomState() rng = np.random.RandomState()
rng.set_state(random[out.rng].get_state()) # tests getitem rng.set_state(random[out.rng].get_state()) # tests getitem
fn_val0 = fn() fn_val0 = fn()
fn_val1 = fn() fn_val1 = fn()
numpy_val0 = rng.uniform(size=(2, 2)) numpy_val0 = rng.uniform(size=(2, 2))
numpy_val1 = rng.uniform(size=(2, 2)) numpy_val1 = rng.uniform(size=(2, 2))
assert numpy.allclose(fn_val0, numpy_val0) assert np.allclose(fn_val0, numpy_val0)
assert numpy.allclose(fn_val1, numpy_val1) assert np.allclose(fn_val1, numpy_val1)
def test_setitem(self): def test_setitem(self):
...@@ -105,15 +105,15 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -105,15 +105,15 @@ class T_SharedRandomStreams(unittest.TestCase):
random.seed(888) random.seed(888)
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
random[out.rng] = numpy.random.RandomState(utt.fetch_seed()) random[out.rng] = np.random.RandomState(utt.fetch_seed())
fn_val0 = fn() fn_val0 = fn()
fn_val1 = fn() fn_val1 = fn()
numpy_val0 = rng.uniform(size=(2, 2)) numpy_val0 = rng.uniform(size=(2, 2))
numpy_val1 = rng.uniform(size=(2, 2)) numpy_val1 = rng.uniform(size=(2, 2))
assert numpy.allclose(fn_val0, numpy_val0) assert np.allclose(fn_val0, numpy_val0)
assert numpy.allclose(fn_val1, numpy_val1) assert np.allclose(fn_val1, numpy_val1)
def test_ndim(self): def test_ndim(self):
"""Test that the behaviour of 'ndim' optional parameter""" """Test that the behaviour of 'ndim' optional parameter"""
...@@ -130,7 +130,7 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -130,7 +130,7 @@ class T_SharedRandomStreams(unittest.TestCase):
val1 = fn() val1 = fn()
val2 = fn2() val2 = fn2()
assert numpy.all(val1 == val2) assert np.all(val1 == val2)
# ndim specified, inconsistent with shape, should raise ValueError # ndim specified, inconsistent with shape, should raise ValueError
random3 = RandomStreams(utt.fetch_seed()) random3 = RandomStreams(utt.fetch_seed())
...@@ -144,13 +144,13 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -144,13 +144,13 @@ class T_SharedRandomStreams(unittest.TestCase):
fn_val0 = fn() fn_val0 = fn()
fn_val1 = fn() fn_val1 = fn()
rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30) rng_seed = np.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = numpy.random.RandomState(int(rng_seed)) # int() is for 32bit rng = np.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))
numpy_val1 = rng.uniform(-1, 1, size=(2, 2)) numpy_val1 = rng.uniform(-1, 1, size=(2, 2))
assert numpy.allclose(fn_val0, numpy_val0) assert np.allclose(fn_val0, numpy_val0)
assert numpy.allclose(fn_val1, numpy_val1) assert np.allclose(fn_val1, numpy_val1)
def test_normal(self): def test_normal(self):
"""Test that RandomStreams.normal generates the same results as numpy""" """Test that RandomStreams.normal generates the same results as numpy"""
...@@ -161,13 +161,13 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -161,13 +161,13 @@ class T_SharedRandomStreams(unittest.TestCase):
fn_val0 = fn() fn_val0 = fn()
fn_val1 = fn() fn_val1 = fn()
rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30) rng_seed = np.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = numpy.random.RandomState(int(rng_seed)) # int() is for 32bit rng = np.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))
assert numpy.allclose(fn_val0, numpy_val0) assert np.allclose(fn_val0, numpy_val0)
assert numpy.allclose(fn_val1, numpy_val1) assert np.allclose(fn_val1, numpy_val1)
def test_random_integers(self): def test_random_integers(self):
# Test that RandomStreams.random_integers generates the same # Test that RandomStreams.random_integers generates the same
...@@ -180,14 +180,14 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -180,14 +180,14 @@ class T_SharedRandomStreams(unittest.TestCase):
fn_val0 = fn() fn_val0 = fn()
fn_val1 = fn() fn_val1 = fn()
rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30) rng_seed = np.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = numpy.random.RandomState(int(rng_seed)) # int() is for 32bit rng = np.random.RandomState(int(rng_seed)) # int() is for 32bit
numpy_val0 = rng.randint(-5, 6, size=(20, 20)) numpy_val0 = rng.randint(-5, 6, size=(20, 20))
numpy_val1 = rng.randint(-5, 6, size=(20, 20)) numpy_val1 = rng.randint(-5, 6, size=(20, 20))
assert numpy.all(fn_val0 == numpy_val0) assert np.all(fn_val0 == numpy_val0)
assert numpy.all(fn_val1 == numpy_val1) assert np.all(fn_val1 == numpy_val1)
def test_choice(self): def test_choice(self):
"""Test that RandomStreams.choice generates the same results as numpy""" """Test that RandomStreams.choice 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.
...@@ -196,30 +196,30 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -196,30 +196,30 @@ class T_SharedRandomStreams(unittest.TestCase):
fn_val0 = fn() fn_val0 = fn()
fn_val1 = fn() fn_val1 = fn()
rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30) rng_seed = np.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = numpy.random.RandomState(int(rng_seed)) # int() is for 32bit rng = np.random.RandomState(int(rng_seed)) # int() is for 32bit
numpy_val0 = rng.choice(10, (11, 8), True, None) numpy_val0 = rng.choice(10, (11, 8), True, None)
numpy_val1 = rng.choice(10, (11, 8), True, None) numpy_val1 = rng.choice(10, (11, 8), True, None)
assert numpy.all(fn_val0 == numpy_val0) assert np.all(fn_val0 == numpy_val0)
assert numpy.all(fn_val1 == numpy_val1) assert np.all(fn_val1 == numpy_val1)
def test_poisson(self): def test_poisson(self):
"""Test that RandomStreams.poisson generates the same results as numpy""" """Test that RandomStreams.poisson 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.
random = RandomStreams(utt.fetch_seed()) random = RandomStreams(utt.fetch_seed())
fn = function([], random.poisson(lam=5, size=(11, 8))) fn = function([], random.poisson(lam=5, size=(11, 8)))
fn_val0 = fn() fn_val0 = fn()
fn_val1 = fn() fn_val1 = fn()
rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30) rng_seed = np.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = numpy.random.RandomState(int(rng_seed)) # int() is for 32bit rng = np.random.RandomState(int(rng_seed)) # int() is for 32bit
numpy_val0 = rng.poisson(lam=5, size=(11, 8)) numpy_val0 = rng.poisson(lam=5, size=(11, 8))
numpy_val1 = rng.poisson(lam=5, size=(11, 8)) numpy_val1 = rng.poisson(lam=5, size=(11, 8))
assert numpy.all(fn_val0 == numpy_val0) assert np.all(fn_val0 == numpy_val0)
assert numpy.all(fn_val1 == numpy_val1) assert np.all(fn_val1 == numpy_val1)
def test_permutation(self): def test_permutation(self):
"""Test that RandomStreams.permutation generates the same results as numpy""" """Test that RandomStreams.permutation generates the same results as numpy"""
...@@ -230,15 +230,15 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -230,15 +230,15 @@ class T_SharedRandomStreams(unittest.TestCase):
fn_val0 = fn() fn_val0 = fn()
fn_val1 = fn() fn_val1 = fn()
rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30) rng_seed = np.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = numpy.random.RandomState(int(rng_seed)) # int() is for 32bit rng = np.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.
numpy_val0 = numpy.asarray([rng.permutation(10) for i in range(20)]) numpy_val0 = np.asarray([rng.permutation(10) for i in range(20)])
numpy_val1 = numpy.asarray([rng.permutation(10) for i in range(20)]) numpy_val1 = np.asarray([rng.permutation(10) for i in range(20)])
assert numpy.all(fn_val0 == numpy_val0) assert np.all(fn_val0 == numpy_val0)
assert numpy.all(fn_val1 == numpy_val1) assert np.all(fn_val1 == numpy_val1)
def test_multinomial(self): def test_multinomial(self):
"""Test that RandomStreams.multinomial generates the same results as numpy""" """Test that RandomStreams.multinomial generates the same results as numpy"""
...@@ -249,39 +249,39 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -249,39 +249,39 @@ class T_SharedRandomStreams(unittest.TestCase):
fn_val0 = fn() fn_val0 = fn()
fn_val1 = fn() fn_val1 = fn()
rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30) rng_seed = np.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = numpy.random.RandomState(int(rng_seed)) # int() is for 32bit rng = np.random.RandomState(int(rng_seed)) # int() is for 32bit
numpy_val0 = rng.multinomial(1, [0.1]*10, size=(4, 4)) numpy_val0 = rng.multinomial(1, [0.1]*10, size=(4, 4))
numpy_val1 = rng.multinomial(1, [0.1]*10, size=(4, 4)) numpy_val1 = rng.multinomial(1, [0.1]*10, size=(4, 4))
assert numpy.all(fn_val0 == numpy_val0) assert np.all(fn_val0 == numpy_val0)
assert numpy.all(fn_val1 == numpy_val1) assert np.all(fn_val1 == numpy_val1)
def test_shuffle_row_elements(self): def test_shuffle_row_elements(self):
"""Test that RandomStreams.shuffle_row_elements generates the right results""" """Test that RandomStreams.shuffle_row_elements generates the right results"""
# 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.
# On matrices, for each row, the elements of that row should be shuffled. # On matrices, for each row, the elements of that row should be shuffled.
# Note that this differs from numpy.random.shuffle, where all the elements # Note that this differs from np.random.shuffle, where all the elements
# of the matrix are shuffled. # of the matrix are shuffled.
random = RandomStreams(utt.fetch_seed()) random = RandomStreams(utt.fetch_seed())
m_input = tensor.dmatrix() m_input = tensor.dmatrix()
f = function([m_input], random.shuffle_row_elements(m_input), updates=random.updates()) f = function([m_input], random.shuffle_row_elements(m_input), updates=random.updates())
# Generate the elements to be shuffled # Generate the elements to be shuffled
val_rng = numpy.random.RandomState(utt.fetch_seed()+42) val_rng = np.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 = f(in_mval) fn_mval0 = f(in_mval)
fn_mval1 = f(in_mval) fn_mval1 = f(in_mval)
print(in_mval[0]) print(in_mval[0])
print(fn_mval0[0]) print(fn_mval0[0])
print(fn_mval1[0]) print(fn_mval1[0])
assert not numpy.all(in_mval == fn_mval0) assert not np.all(in_mval == fn_mval0)
assert not numpy.all(in_mval == fn_mval1) assert not np.all(in_mval == fn_mval1)
assert not numpy.all(fn_mval0 == fn_mval1) assert not np.all(fn_mval0 == fn_mval1)
rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30) rng_seed = np.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = numpy.random.RandomState(int(rng_seed)) rng = np.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()
for row in numpy_mval0: for row in numpy_mval0:
...@@ -289,10 +289,10 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -289,10 +289,10 @@ class T_SharedRandomStreams(unittest.TestCase):
for row in numpy_mval1: for row in numpy_mval1:
rng.shuffle(row) rng.shuffle(row)
assert numpy.all(numpy_mval0 == fn_mval0) assert np.all(numpy_mval0 == fn_mval0)
assert numpy.all(numpy_mval1 == fn_mval1) assert np.all(numpy_mval1 == fn_mval1)
# On vectors, the behaviour is the same as numpy.random.shuffle, # On vectors, the behaviour is the same as np.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.
random1 = RandomStreams(utt.fetch_seed()) random1 = RandomStreams(utt.fetch_seed())
v_input = tensor.dvector() v_input = tensor.dvector()
...@@ -301,12 +301,12 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -301,12 +301,12 @@ class T_SharedRandomStreams(unittest.TestCase):
in_vval = val_rng.uniform(-3, 3, size=(12,)) in_vval = val_rng.uniform(-3, 3, size=(12,))
fn_vval = f1(in_vval) fn_vval = f1(in_vval)
numpy_vval = in_vval.copy() numpy_vval = in_vval.copy()
vrng = numpy.random.RandomState(int(rng_seed)) vrng = np.random.RandomState(int(rng_seed))
vrng.shuffle(numpy_vval) vrng.shuffle(numpy_vval)
print(in_vval) print(in_vval)
print(fn_vval) print(fn_vval)
print(numpy_vval) print(numpy_vval)
assert numpy.all(numpy_vval == fn_vval) assert np.all(numpy_vval == fn_vval)
# Trying to shuffle a vector with function that should shuffle # Trying to shuffle a vector with function that should shuffle
# matrices, or vice versa, raises a TypeError # matrices, or vice versa, raises a TypeError
...@@ -320,10 +320,10 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -320,10 +320,10 @@ class T_SharedRandomStreams(unittest.TestCase):
fn_a = function([], out_a) fn_a = function([], out_a)
fn_a_val0 = fn_a() fn_a_val0 = fn_a()
fn_a_val1 = fn_a() fn_a_val1 = fn_a()
assert not numpy.all(fn_a_val0 == fn_a_val1) assert not np.all(fn_a_val0 == fn_a_val1)
nearly_zeros = function([], out_a + out_a - 2 * out_a) nearly_zeros = function([], out_a + out_a - 2 * out_a)
assert numpy.all(abs(nearly_zeros()) < 1e-5) assert np.all(abs(nearly_zeros()) < 1e-5)
# Explicit updates #1 # Explicit updates #1
random_b = RandomStreams(utt.fetch_seed()) random_b = RandomStreams(utt.fetch_seed())
...@@ -331,8 +331,8 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -331,8 +331,8 @@ class T_SharedRandomStreams(unittest.TestCase):
fn_b = function([], out_b, updates=random_b.updates()) fn_b = function([], out_b, updates=random_b.updates())
fn_b_val0 = fn_b() fn_b_val0 = fn_b()
fn_b_val1 = fn_b() fn_b_val1 = fn_b()
assert numpy.all(fn_b_val0 == fn_a_val0) assert np.all(fn_b_val0 == fn_a_val0)
assert numpy.all(fn_b_val1 == fn_a_val1) assert np.all(fn_b_val1 == fn_a_val1)
# Explicit updates #2 # Explicit updates #2
random_c = RandomStreams(utt.fetch_seed()) random_c = RandomStreams(utt.fetch_seed())
...@@ -340,8 +340,8 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -340,8 +340,8 @@ class T_SharedRandomStreams(unittest.TestCase):
fn_c = function([], out_c, updates=[out_c.update]) fn_c = function([], out_c, updates=[out_c.update])
fn_c_val0 = fn_c() fn_c_val0 = fn_c()
fn_c_val1 = fn_c() fn_c_val1 = fn_c()
assert numpy.all(fn_c_val0 == fn_a_val0) assert np.all(fn_c_val0 == fn_a_val0)
assert numpy.all(fn_c_val1 == fn_a_val1) assert np.all(fn_c_val1 == fn_a_val1)
# No updates at all # No updates at all
random_d = RandomStreams(utt.fetch_seed()) random_d = RandomStreams(utt.fetch_seed())
...@@ -349,8 +349,8 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -349,8 +349,8 @@ class T_SharedRandomStreams(unittest.TestCase):
fn_d = function([], out_d, no_default_updates=True) fn_d = function([], out_d, no_default_updates=True)
fn_d_val0 = fn_d() fn_d_val0 = fn_d()
fn_d_val1 = fn_d() fn_d_val1 = fn_d()
assert numpy.all(fn_d_val0 == fn_a_val0) assert np.all(fn_d_val0 == fn_a_val0)
assert numpy.all(fn_d_val1 == fn_d_val0) assert np.all(fn_d_val1 == fn_d_val0)
# No updates for out # No updates for out
random_e = RandomStreams(utt.fetch_seed()) random_e = RandomStreams(utt.fetch_seed())
...@@ -358,8 +358,8 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -358,8 +358,8 @@ class T_SharedRandomStreams(unittest.TestCase):
fn_e = function([], out_e, no_default_updates=[out_e.rng]) fn_e = function([], out_e, no_default_updates=[out_e.rng])
fn_e_val0 = fn_e() fn_e_val0 = fn_e()
fn_e_val1 = fn_e() fn_e_val1 = fn_e()
assert numpy.all(fn_e_val0 == fn_a_val0) assert np.all(fn_e_val0 == fn_a_val0)
assert numpy.all(fn_e_val1 == fn_e_val0) assert np.all(fn_e_val1 == fn_e_val0)
def test_symbolic_shape(self): def test_symbolic_shape(self):
random = RandomStreams(utt.fetch_seed()) random = RandomStreams(utt.fetch_seed())
...@@ -407,21 +407,21 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -407,21 +407,21 @@ class T_SharedRandomStreams(unittest.TestCase):
g = function([], random.multinomial()) g = function([], random.multinomial())
# seed_rng is generator for generating *seeds* for RandomStates # seed_rng is generator for generating *seeds* for RandomStates
seed_rng = numpy.random.RandomState(utt.fetch_seed()) seed_rng = np.random.RandomState(utt.fetch_seed())
uniform_rng = numpy.random.RandomState(int(seed_rng.randint(2**30))) uniform_rng = np.random.RandomState(int(seed_rng.randint(2**30)))
multinomial_rng = numpy.random.RandomState(int(seed_rng.randint(2**30))) multinomial_rng = np.random.RandomState(int(seed_rng.randint(2**30)))
val0 = f() val0 = f()
val1 = f() val1 = f()
numpy_val0 = uniform_rng.uniform() numpy_val0 = uniform_rng.uniform()
numpy_val1 = uniform_rng.uniform() numpy_val1 = uniform_rng.uniform()
assert numpy.allclose(val0, numpy_val0) assert np.allclose(val0, numpy_val0)
assert numpy.allclose(val1, numpy_val1) assert np.allclose(val1, numpy_val1)
for i in range(10): # every test has 50% chance of passing even with non-matching random states for i in range(10): # every test has 50% chance of passing even with non-matching random states
val2 = g() val2 = g()
numpy_val2 = multinomial_rng.multinomial(n=1, pvals=[.5, .5]) numpy_val2 = multinomial_rng.multinomial(n=1, pvals=[.5, .5])
assert numpy.all(val2 == numpy_val2) assert np.all(val2 == numpy_val2)
def test_vector_arguments(self): def test_vector_arguments(self):
random = RandomStreams(utt.fetch_seed()) random = RandomStreams(utt.fetch_seed())
...@@ -430,27 +430,27 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -430,27 +430,27 @@ class T_SharedRandomStreams(unittest.TestCase):
assert out.ndim == 1 assert out.ndim == 1
f = function([low], out) f = function([low], out)
seed_gen = numpy.random.RandomState(utt.fetch_seed()) seed_gen = np.random.RandomState(utt.fetch_seed())
numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30))) numpy_rng = np.random.RandomState(int(seed_gen.randint(2**30)))
val0 = f([-5, .5, 0, 1]) val0 = f([-5, .5, 0, 1])
val1 = f([.9]) val1 = f([.9])
numpy_val0 = numpy_rng.uniform(low=[-5, .5, 0, 1], high=1) numpy_val0 = numpy_rng.uniform(low=[-5, .5, 0, 1], high=1)
numpy_val1 = numpy_rng.uniform(low=[.9], high=1) numpy_val1 = numpy_rng.uniform(low=[.9], high=1)
assert numpy.all(val0 == numpy_val0) assert np.all(val0 == numpy_val0)
assert numpy.all(val1 == numpy_val1) assert np.all(val1 == numpy_val1)
high = tensor.vector() high = tensor.vector()
outb = random.uniform(low=low, high=high) outb = random.uniform(low=low, high=high)
assert outb.ndim == 1 assert outb.ndim == 1
fb = function([low, high], outb) fb = function([low, high], outb)
numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30))) numpy_rng = np.random.RandomState(int(seed_gen.randint(2**30)))
val0b = fb([-4., -2], [-1, 0]) val0b = fb([-4., -2], [-1, 0])
val1b = fb([-4.], [-1]) val1b = fb([-4.], [-1])
numpy_val0b = numpy_rng.uniform(low=[-4., -2], high=[-1, 0]) numpy_val0b = numpy_rng.uniform(low=[-4., -2], high=[-1, 0])
numpy_val1b = numpy_rng.uniform(low=[-4.], high=[-1]) numpy_val1b = numpy_rng.uniform(low=[-4.], high=[-1])
assert numpy.all(val0b == numpy_val0b) assert np.all(val0b == numpy_val0b)
assert numpy.all(val1b == numpy_val1b) assert np.all(val1b == numpy_val1b)
self.assertRaises(ValueError, fb, [-4., -2], [-1, 0, 1]) self.assertRaises(ValueError, fb, [-4., -2], [-1, 0, 1])
# TODO: do we want that? # TODO: do we want that?
#self.assertRaises(ValueError, fb, [-4., -2], [-1]) #self.assertRaises(ValueError, fb, [-4., -2], [-1])
...@@ -459,13 +459,13 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -459,13 +459,13 @@ class T_SharedRandomStreams(unittest.TestCase):
outc = random.uniform(low=low, high=high, size=size, ndim=1) outc = random.uniform(low=low, high=high, size=size, ndim=1)
fc = function([low, high, size], outc) fc = function([low, high, size], outc)
numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30))) numpy_rng = np.random.RandomState(int(seed_gen.randint(2**30)))
val0c = fc([-4., -2], [-1, 0], [2]) val0c = fc([-4., -2], [-1, 0], [2])
val1c = fc([-4.], [-1], [1]) val1c = fc([-4.], [-1], [1])
numpy_val0c = numpy_rng.uniform(low=[-4., -2], high=[-1, 0]) numpy_val0c = numpy_rng.uniform(low=[-4., -2], high=[-1, 0])
numpy_val1c = numpy_rng.uniform(low=[-4.], high=[-1]) numpy_val1c = numpy_rng.uniform(low=[-4.], high=[-1])
assert numpy.all(val0c == numpy_val0c) assert np.all(val0c == numpy_val0c)
assert numpy.all(val1c == numpy_val1c) assert np.all(val1c == numpy_val1c)
self.assertRaises(ValueError, fc, [-4., -2], [-1, 0], [1]) self.assertRaises(ValueError, fc, [-4., -2], [-1, 0], [1])
self.assertRaises(ValueError, fc, [-4., -2], [-1, 0], [1, 2]) self.assertRaises(ValueError, fc, [-4., -2], [-1, 0], [1, 2])
self.assertRaises(ValueError, fc, [-4., -2], [-1, 0], [2, 1]) self.assertRaises(ValueError, fc, [-4., -2], [-1, 0], [2, 1])
...@@ -481,8 +481,8 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -481,8 +481,8 @@ class T_SharedRandomStreams(unittest.TestCase):
assert out.ndim == 2 assert out.ndim == 2
f = function([low, high], out) f = function([low, high], out)
rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30) rng_seed = np.random.RandomState(utt.fetch_seed()).randint(2**30)
numpy_rng = numpy.random.RandomState(int(rng_seed)) numpy_rng = np.random.RandomState(int(rng_seed))
val0 = f([-5, .5, 0, 1], [[1.]]) val0 = f([-5, .5, 0, 1], [[1.]])
val1 = f([.9], [[1.], [1.1], [1.5]]) val1 = f([.9], [[1.], [1.1], [1.5]])
val2 = f([-5, .5, 0, 1], [[1.], [1.1], [1.5]]) val2 = f([-5, .5, 0, 1], [[1.], [1.1], [1.5]])
...@@ -491,9 +491,9 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -491,9 +491,9 @@ class T_SharedRandomStreams(unittest.TestCase):
numpy_val1 = numpy_rng.uniform(low=[.9], high=[[1.], [1.1], [1.5]]) numpy_val1 = numpy_rng.uniform(low=[.9], high=[[1.], [1.1], [1.5]])
numpy_val2 = numpy_rng.uniform(low=[-5, .5, 0, 1], high=[[1.], [1.1], [1.5]]) numpy_val2 = numpy_rng.uniform(low=[-5, .5, 0, 1], high=[[1.], [1.1], [1.5]])
assert numpy.all(val0 == numpy_val0) assert np.all(val0 == numpy_val0)
assert numpy.all(val1 == numpy_val1) assert np.all(val1 == numpy_val1)
assert numpy.all(val2 == numpy_val2) assert np.all(val2 == numpy_val2)
def test_uniform_vector(self): def test_uniform_vector(self):
random = RandomStreams(utt.fetch_seed()) random = RandomStreams(utt.fetch_seed())
...@@ -505,29 +505,29 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -505,29 +505,29 @@ class T_SharedRandomStreams(unittest.TestCase):
low_val = [.1, .2, .3] low_val = [.1, .2, .3]
high_val = [1.1, 2.2, 3.3] high_val = [1.1, 2.2, 3.3]
seed_gen = numpy.random.RandomState(utt.fetch_seed()) seed_gen = np.random.RandomState(utt.fetch_seed())
numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30))) numpy_rng = np.random.RandomState(int(seed_gen.randint(2**30)))
# Arguments of size (3,) # Arguments of size (3,)
val0 = f(low_val, high_val) val0 = f(low_val, high_val)
numpy_val0 = numpy_rng.uniform(low=low_val, high=high_val) numpy_val0 = numpy_rng.uniform(low=low_val, high=high_val)
print('THEANO', val0) print('THEANO', val0)
print('NUMPY', numpy_val0) print('NUMPY', numpy_val0)
assert numpy.all(val0 == numpy_val0) assert np.all(val0 == numpy_val0)
# arguments of size (2,) # arguments of size (2,)
val1 = f(low_val[:-1], high_val[:-1]) val1 = f(low_val[:-1], high_val[:-1])
numpy_val1 = numpy_rng.uniform(low=low_val[:-1], high=high_val[:-1]) numpy_val1 = numpy_rng.uniform(low=low_val[:-1], high=high_val[:-1])
print('THEANO', val1) print('THEANO', val1)
print('NUMPY', numpy_val1) print('NUMPY', numpy_val1)
assert numpy.all(val1 == numpy_val1) assert np.all(val1 == numpy_val1)
# Specifying the size explicitly # Specifying the size explicitly
g = function([low, high], random.uniform(low=low, high=high, size=(3,))) g = function([low, high], random.uniform(low=low, high=high, size=(3,)))
val2 = g(low_val, high_val) val2 = g(low_val, high_val)
numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30))) numpy_rng = np.random.RandomState(int(seed_gen.randint(2**30)))
numpy_val2 = numpy_rng.uniform(low=low_val, high=high_val, size=(3,)) numpy_val2 = numpy_rng.uniform(low=low_val, high=high_val, size=(3,))
assert numpy.all(val2 == numpy_val2) assert np.all(val2 == numpy_val2)
self.assertRaises(ValueError, g, low_val[:-1], high_val[:-1]) self.assertRaises(ValueError, g, low_val[:-1], high_val[:-1])
def test_binomial_vector(self): def test_binomial_vector(self):
...@@ -539,26 +539,26 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -539,26 +539,26 @@ class T_SharedRandomStreams(unittest.TestCase):
f = function([n, prob], out) f = function([n, prob], out)
n_val = [1, 2, 3] n_val = [1, 2, 3]
prob_val = numpy.asarray([.1, .2, .3], dtype=config.floatX) prob_val = np.asarray([.1, .2, .3], dtype=config.floatX)
seed_gen = numpy.random.RandomState(utt.fetch_seed()) seed_gen = np.random.RandomState(utt.fetch_seed())
numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30))) numpy_rng = np.random.RandomState(int(seed_gen.randint(2**30)))
# Arguments of size (3,) # Arguments of size (3,)
val0 = f(n_val, prob_val) val0 = f(n_val, prob_val)
numpy_val0 = numpy_rng.binomial(n=n_val, p=prob_val) numpy_val0 = numpy_rng.binomial(n=n_val, p=prob_val)
assert numpy.all(val0 == numpy_val0) assert np.all(val0 == numpy_val0)
# arguments of size (2,) # arguments of size (2,)
val1 = f(n_val[:-1], prob_val[:-1]) val1 = f(n_val[:-1], prob_val[:-1])
numpy_val1 = numpy_rng.binomial(n=n_val[:-1], p=prob_val[:-1]) numpy_val1 = numpy_rng.binomial(n=n_val[:-1], p=prob_val[:-1])
assert numpy.all(val1 == numpy_val1) assert np.all(val1 == numpy_val1)
# Specifying the size explicitly # Specifying the size explicitly
g = function([n, prob], random.binomial(n=n, p=prob, size=(3,))) g = function([n, prob], random.binomial(n=n, p=prob, size=(3,)))
val2 = g(n_val, prob_val) val2 = g(n_val, prob_val)
numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30))) numpy_rng = np.random.RandomState(int(seed_gen.randint(2**30)))
numpy_val2 = numpy_rng.binomial(n=n_val, p=prob_val, size=(3,)) numpy_val2 = numpy_rng.binomial(n=n_val, p=prob_val, size=(3,))
assert numpy.all(val2 == numpy_val2) assert np.all(val2 == numpy_val2)
self.assertRaises(ValueError, g, n_val[:-1], prob_val[:-1]) self.assertRaises(ValueError, g, n_val[:-1], prob_val[:-1])
def test_normal_vector(self): def test_normal_vector(self):
...@@ -571,25 +571,25 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -571,25 +571,25 @@ class T_SharedRandomStreams(unittest.TestCase):
avg_val = [1, 2, 3] avg_val = [1, 2, 3]
std_val = [.1, .2, .3] std_val = [.1, .2, .3]
seed_gen = numpy.random.RandomState(utt.fetch_seed()) seed_gen = np.random.RandomState(utt.fetch_seed())
numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30))) numpy_rng = np.random.RandomState(int(seed_gen.randint(2**30)))
# Arguments of size (3,) # Arguments of size (3,)
val0 = f(avg_val, std_val) val0 = f(avg_val, std_val)
numpy_val0 = numpy_rng.normal(loc=avg_val, scale=std_val) numpy_val0 = numpy_rng.normal(loc=avg_val, scale=std_val)
assert numpy.allclose(val0, numpy_val0) assert np.allclose(val0, numpy_val0)
# arguments of size (2,) # arguments of size (2,)
val1 = f(avg_val[:-1], std_val[:-1]) val1 = f(avg_val[:-1], std_val[:-1])
numpy_val1 = numpy_rng.normal(loc=avg_val[:-1], scale=std_val[:-1]) numpy_val1 = numpy_rng.normal(loc=avg_val[:-1], scale=std_val[:-1])
assert numpy.allclose(val1, numpy_val1) assert np.allclose(val1, numpy_val1)
# Specifying the size explicitly # Specifying the size explicitly
g = function([avg, std], random.normal(avg=avg, std=std, size=(3,))) g = function([avg, std], random.normal(avg=avg, std=std, size=(3,)))
val2 = g(avg_val, std_val) val2 = g(avg_val, std_val)
numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30))) numpy_rng = np.random.RandomState(int(seed_gen.randint(2**30)))
numpy_val2 = numpy_rng.normal(loc=avg_val, scale=std_val, size=(3,)) numpy_val2 = numpy_rng.normal(loc=avg_val, scale=std_val, size=(3,))
assert numpy.allclose(val2, numpy_val2) assert np.allclose(val2, numpy_val2)
self.assertRaises(ValueError, g, avg_val[:-1], std_val[:-1]) self.assertRaises(ValueError, g, avg_val[:-1], std_val[:-1])
def test_random_integers_vector(self): def test_random_integers_vector(self):
...@@ -602,28 +602,28 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -602,28 +602,28 @@ class T_SharedRandomStreams(unittest.TestCase):
low_val = [100, 200, 300] low_val = [100, 200, 300]
high_val = [110, 220, 330] high_val = [110, 220, 330]
seed_gen = numpy.random.RandomState(utt.fetch_seed()) seed_gen = np.random.RandomState(utt.fetch_seed())
numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30))) numpy_rng = np.random.RandomState(int(seed_gen.randint(2**30)))
# Arguments of size (3,) # Arguments of size (3,)
val0 = f(low_val, high_val) val0 = f(low_val, high_val)
numpy_val0 = numpy.asarray([numpy_rng.randint(low=lv, high=hv+1) numpy_val0 = np.asarray([numpy_rng.randint(low=lv, high=hv+1)
for lv, hv in zip(low_val, high_val)]) for lv, hv in zip(low_val, high_val)])
assert numpy.all(val0 == numpy_val0) assert np.all(val0 == numpy_val0)
# arguments of size (2,) # arguments of size (2,)
val1 = f(low_val[:-1], high_val[:-1]) val1 = f(low_val[:-1], high_val[:-1])
numpy_val1 = numpy.asarray([numpy_rng.randint(low=lv, high=hv+1) numpy_val1 = np.asarray([numpy_rng.randint(low=lv, high=hv+1)
for lv, hv in zip(low_val[:-1], high_val[:-1])]) for lv, hv in zip(low_val[:-1], high_val[:-1])])
assert numpy.all(val1 == numpy_val1) assert np.all(val1 == numpy_val1)
# Specifying the size explicitly # Specifying the size explicitly
g = function([low, high], random.random_integers(low=low, high=high, size=(3,))) g = function([low, high], random.random_integers(low=low, high=high, size=(3,)))
val2 = g(low_val, high_val) val2 = g(low_val, high_val)
numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30))) numpy_rng = np.random.RandomState(int(seed_gen.randint(2**30)))
numpy_val2 = numpy.asarray([numpy_rng.randint(low=lv, high=hv+1) numpy_val2 = np.asarray([numpy_rng.randint(low=lv, high=hv+1)
for lv, hv in zip(low_val, high_val)]) for lv, hv in zip(low_val, high_val)])
assert numpy.all(val2 == numpy_val2) assert np.all(val2 == numpy_val2)
self.assertRaises(ValueError, g, low_val[:-1], high_val[:-1]) self.assertRaises(ValueError, g, low_val[:-1], high_val[:-1])
# Vectorized permutation don't make sense: the only parameter, n, # Vectorized permutation don't make sense: the only parameter, n,
...@@ -639,29 +639,29 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -639,29 +639,29 @@ class T_SharedRandomStreams(unittest.TestCase):
n_val = [1, 2, 3] n_val = [1, 2, 3]
pvals_val = [[.1, .9], [.2, .8], [.3, .7]] pvals_val = [[.1, .9], [.2, .8], [.3, .7]]
pvals_val = numpy.asarray(pvals_val, dtype=config.floatX) pvals_val = np.asarray(pvals_val, dtype=config.floatX)
seed_gen = numpy.random.RandomState(utt.fetch_seed()) seed_gen = np.random.RandomState(utt.fetch_seed())
numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30))) numpy_rng = np.random.RandomState(int(seed_gen.randint(2**30)))
# Arguments of size (3,) # Arguments of size (3,)
val0 = f(n_val, pvals_val) val0 = f(n_val, pvals_val)
numpy_val0 = numpy.asarray([numpy_rng.multinomial(n=nv, pvals=pv) numpy_val0 = np.asarray([numpy_rng.multinomial(n=nv, pvals=pv)
for nv, pv in zip(n_val, pvals_val)]) for nv, pv in zip(n_val, pvals_val)])
assert numpy.all(val0 == numpy_val0) assert np.all(val0 == numpy_val0)
# arguments of size (2,) # arguments of size (2,)
val1 = f(n_val[:-1], pvals_val[:-1]) val1 = f(n_val[:-1], pvals_val[:-1])
numpy_val1 = numpy.asarray([numpy_rng.multinomial(n=nv, pvals=pv) numpy_val1 = np.asarray([numpy_rng.multinomial(n=nv, pvals=pv)
for nv, pv in zip(n_val[:-1], pvals_val[:-1])]) for nv, pv in zip(n_val[:-1], pvals_val[:-1])])
assert numpy.all(val1 == numpy_val1) assert np.all(val1 == numpy_val1)
# Specifying the size explicitly # Specifying the size explicitly
g = function([n, pvals], random.multinomial(n=n, pvals=pvals, size=(3,))) g = function([n, pvals], random.multinomial(n=n, pvals=pvals, size=(3,)))
val2 = g(n_val, pvals_val) val2 = g(n_val, pvals_val)
numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30))) numpy_rng = np.random.RandomState(int(seed_gen.randint(2**30)))
numpy_val2 = numpy.asarray([numpy_rng.multinomial(n=nv, pvals=pv) numpy_val2 = np.asarray([numpy_rng.multinomial(n=nv, pvals=pv)
for nv, pv in zip(n_val, pvals_val)]) for nv, pv in zip(n_val, pvals_val)])
assert numpy.all(val2 == numpy_val2) assert np.all(val2 == numpy_val2)
self.assertRaises(ValueError, g, n_val[:-1], pvals_val[:-1]) self.assertRaises(ValueError, g, n_val[:-1], pvals_val[:-1])
def test_dtype(self): def test_dtype(self):
...@@ -677,7 +677,7 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -677,7 +677,7 @@ class T_SharedRandomStreams(unittest.TestCase):
val1 = f(255, 257) val1 = f(255, 257)
assert val1.dtype == 'int8' assert val1.dtype == 'int8'
assert numpy.all(abs(val1) <= 1) assert np.all(abs(val1) <= 1)
def test_default_dtype(self): def test_default_dtype(self):
random = RandomStreams(utt.fetch_seed()) random = RandomStreams(utt.fetch_seed())
...@@ -704,11 +704,11 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -704,11 +704,11 @@ class T_SharedRandomStreams(unittest.TestCase):
outf = random.uniform(low=lowf, high=highf, size=(42,)) outf = random.uniform(low=lowf, high=highf, size=(42,))
assert outf.dtype == config.floatX assert outf.dtype == config.floatX
ff = function([lowf, highf], outf) ff = function([lowf, highf], outf)
valf = ff(numpy.float32(-0.1), numpy.float32(0.3)) valf = ff(np.float32(-0.1), np.float32(0.3))
assert valf.dtype == config.floatX assert valf.dtype == config.floatX
def test_shared_constructor_borrow(self): def test_shared_constructor_borrow(self):
rng = numpy.random.RandomState(123) rng = np.random.RandomState(123)
s_rng_default = shared(rng) s_rng_default = shared(rng)
s_rng_True = shared(rng, borrow=True) s_rng_True = shared(rng, borrow=True)
s_rng_False = shared(rng, borrow=False) s_rng_False = shared(rng, borrow=False)
...@@ -728,7 +728,7 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -728,7 +728,7 @@ class T_SharedRandomStreams(unittest.TestCase):
def test_get_value_borrow(self): def test_get_value_borrow(self):
rng = numpy.random.RandomState(123) rng = np.random.RandomState(123)
s_rng = shared(rng) s_rng = shared(rng)
r_ = s_rng.container.storage[0] r_ = s_rng.container.storage[0]
...@@ -745,7 +745,7 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -745,7 +745,7 @@ class T_SharedRandomStreams(unittest.TestCase):
assert r_.rand() == r_F.rand() assert r_.rand() == r_F.rand()
def test_get_value_internal_type(self): def test_get_value_internal_type(self):
rng = numpy.random.RandomState(123) rng = np.random.RandomState(123)
s_rng = shared(rng) s_rng = shared(rng)
# there is no special behaviour required of return_internal_type # there is no special behaviour required of return_internal_type
...@@ -765,11 +765,11 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -765,11 +765,11 @@ class T_SharedRandomStreams(unittest.TestCase):
assert r_.rand() == r_F.rand() assert r_.rand() == r_F.rand()
def test_set_value_borrow(self): def test_set_value_borrow(self):
rng = numpy.random.RandomState(123) rng = np.random.RandomState(123)
s_rng = shared(rng) s_rng = shared(rng)
new_rng = numpy.random.RandomState(234234) new_rng = np.random.RandomState(234234)
# Test the borrow contract is respected: # Test the borrow contract is respected:
# assigning with borrow=False makes a copy # assigning with borrow=False makes a copy
...@@ -778,7 +778,7 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -778,7 +778,7 @@ class T_SharedRandomStreams(unittest.TestCase):
assert new_rng.randn() == s_rng.container.storage[0].randn() assert new_rng.randn() == s_rng.container.storage[0].randn()
# Test that the current implementation is actually borrowing when it can. # Test that the current implementation is actually borrowing when it can.
rr = numpy.random.RandomState(33) rr = np.random.RandomState(33)
s_rng.set_value(rr, borrow=True) s_rng.set_value(rr, borrow=True)
assert rr is s_rng.container.storage[0] assert rr is s_rng.container.storage[0]
...@@ -811,7 +811,7 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -811,7 +811,7 @@ class T_SharedRandomStreams(unittest.TestCase):
for (su1, su2) in zip(g1.rng.state_updates, g2.rng.state_updates): for (su1, su2) in zip(g1.rng.state_updates, g2.rng.state_updates):
su2[0].set_value(su1[0].get_value()) su2[0].set_value(su1[0].get_value())
numpy.testing.assert_array_almost_equal(f1(), f2(), decimal=6) np.testing.assert_array_almost_equal(f1(), f2(), decimal=6)
if __name__ == '__main__': if __name__ == '__main__':
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论