Skip to content

Commit

Permalink
Update numpy.random usage in tests
Browse files Browse the repository at this point in the history
- Use newer, NumPy-recommended `numpy.random` functions (e.g. replace `rand`
  `random`, `randn` with `standard_normal`, etc.)
- Seed some unseeded/inconsistently seeded tests
- Minor `pytest` usage updates (e.g. use parameterizations, `pytest.raises`, and
  `pytest.warns`)
- Use `np.array_equal` instead of `np.all(... == ...)`
  • Loading branch information
brandonwillard committed May 8, 2022
1 parent e40c1b2 commit 6cca25e
Show file tree
Hide file tree
Showing 38 changed files with 891 additions and 855 deletions.
31 changes: 16 additions & 15 deletions tests/compile/function/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,18 @@ def test_in_strict(self):
out = a + b

f = function([In(a, strict=False)], out)

# works, rand generates float64 by default
f(np.random.rand(8))
assert f(np.random.random((8,)).astype(np.float64)).dtype == np.float64

# works, casting is allowed
f(np.array([1, 2, 3, 4], dtype="int32"))
assert f(np.array([1, 2, 3, 4], dtype="int32")).dtype == np.float64

f = function([In(a, strict=True)], out)
try:

with pytest.raises(TypeError):
# fails, f expects float64
f(np.array([1, 2, 3, 4], dtype="int32"))
except TypeError:
pass

def test_explicit_shared_input(self):
# This is not a test of the In class per se, but the In class relies
Expand All @@ -94,17 +95,17 @@ def test_in_mutable(self):

# using mutable=True will let f change the value in aval
f = function([In(a, mutable=True)], a_out, mode="FAST_RUN")
aval = np.random.rand(10)
aval = np.random.random((10,))
aval2 = aval.copy()
assert np.all(f(aval) == (aval2 * 2))
assert not np.all(aval == aval2)
assert np.array_equal(f(aval), (aval2 * 2))
assert not np.array_equal(aval, aval2)

# using mutable=False should leave the input untouched
f = function([In(a, mutable=False)], a_out, mode="FAST_RUN")
aval = np.random.rand(10)
aval = np.random.random((10,))
aval2 = aval.copy()
assert np.all(f(aval) == (aval2 * 2))
assert np.all(aval == aval2)
assert np.array_equal(f(aval), (aval2 * 2))
assert np.array_equal(aval, aval2)

def test_in_update(self):
a = dscalar("a")
Expand Down Expand Up @@ -155,7 +156,7 @@ def test_in_allow_downcast_int(self):

# Both values are in range. Since they're not ndarrays (but lists),
# they will be converted, and their value checked.
assert np.all(f([3], [6], 1) == 10)
assert np.array_equal(f([3], [6], 1), [10])

# Values are in range, but a dtype too large has explicitly been given
# For performance reasons, no check of the data is explicitly performed
Expand All @@ -164,7 +165,7 @@ def test_in_allow_downcast_int(self):
f([3], np.array([6], dtype="int16"), 1)

# Value too big for a, silently ignored
assert np.all(f([2**20], np.ones(1, dtype="int8"), 1) == 2)
assert np.array_equal(f([2**20], np.ones(1, dtype="int8"), 1), [2])

# Value too big for b, raises TypeError
with pytest.raises(TypeError):
Expand All @@ -189,7 +190,7 @@ def test_in_allow_downcast_floatX(self):
)

# If the values can be accurately represented, everything is OK
assert np.all(f(0, 0, 0) == 0)
assert np.array_equal(f(0, 0, 0), 0)

# If allow_downcast is True, idem
assert np.allclose(f(0.1, 0, 0), 0.1)
Expand Down Expand Up @@ -221,7 +222,7 @@ def test_in_allow_downcast_vector_floatX(self):

# If the values can be accurately represented, everything is OK
z = [0]
assert np.all(f(z, z, z) == 0)
assert np.array_equal(f(z, z, z), [0])

# If allow_downcast is True, idem
assert np.allclose(f([0.1], z, z), 0.1)
Expand Down
16 changes: 8 additions & 8 deletions tests/compile/function/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ def test_copy_share_memory(self):

def test_swap_SharedVariable(self):
i = iscalar()
x_list = shared(value=np.random.rand(10).astype(config.floatX))
x_list = shared(value=np.random.random((10,)).astype(config.floatX))

x = scalar("x")
# SharedVariable for tests, one of them has update
Expand Down Expand Up @@ -419,11 +419,11 @@ def test_swap_SharedVariable_with_given(self):
# A special testcase for logistic_sgd.py in Deep Learning Tutorial
# This test assert that SharedVariable in different function have same storage

train_x = shared(value=np.random.rand(10, 10).astype(config.floatX))
test_x = shared(value=np.random.rand(10, 10).astype(config.floatX))
train_x = shared(value=np.random.random((10, 10)).astype(config.floatX))
test_x = shared(value=np.random.random((10, 10)).astype(config.floatX))

train_y = shared(value=np.random.rand(10, 1).astype(config.floatX))
test_y = shared(value=np.random.rand(10, 1).astype(config.floatX))
train_y = shared(value=np.random.random((10, 1)).astype(config.floatX))
test_y = shared(value=np.random.random((10, 1)).astype(config.floatX))

i = iscalar("index")
x = vector("x")
Expand Down Expand Up @@ -604,7 +604,7 @@ def test_borrow_input(self):
# when borrow=True is implemented.

a = dmatrix()
aval = np.random.rand(3, 3)
aval = np.random.random((3, 3))

# when borrow=False, test that a destroy map cannot alias output to input
f = function([In(a, borrow=False)], Out(a + 1, borrow=True))
Expand Down Expand Up @@ -699,7 +699,7 @@ def test_default_values(self):
assert funct(first=1) == x

def test_check_for_aliased_inputs(self):
b = np.random.rand(5, 4)
b = np.random.random((5, 4))
s1 = shared(b)
s2 = shared(b)
x1 = vector()
Expand Down Expand Up @@ -1053,7 +1053,7 @@ def pers_save(obj):
def pers_load(id):
return saves[id]

b = np.random.rand(5, 4)
b = np.random.random((5, 4))

x = matrix()
y = shared(b)
Expand Down
28 changes: 14 additions & 14 deletions tests/compile/test_builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def test_grad_grad(self, cls_ofg):
)
def test_shared(self, cls_ofg):
x, y, z = matrices("xyz")
s = shared(np.random.rand(2, 2).astype(config.floatX))
s = shared(np.random.random((2, 2)).astype(config.floatX))
e = x + y * z + s
op = cls_ofg([x, y, z], [e])
# (1+3*5=array of 16) - (3+1*5=array of 8)
Expand All @@ -144,7 +144,7 @@ def test_shared(self, cls_ofg):
)
def test_shared_grad(self, cls_ofg):
x, y, z = matrices("xyz")
s = shared(np.random.rand(2, 2).astype(config.floatX))
s = shared(np.random.random((2, 2)).astype(config.floatX))
e = x + y * z + s
op = cls_ofg([x, y, z], [e])
f = op(x, y, z)
Expand Down Expand Up @@ -184,8 +184,8 @@ def go(inps, gs):
zz = at_sum(op(xx, yy))
dx, dy = grad(zz, [xx, yy])
fn = function([xx, yy], [dx, dy])
xv = np.random.rand(16).astype(config.floatX)
yv = np.random.rand(16).astype(config.floatX)
xv = np.random.random((16,)).astype(config.floatX)
yv = np.random.random((16,)).astype(config.floatX)
dxv, dyv = fn(xv, yv)
np.testing.assert_array_almost_equal(yv * 2, dxv, 4)
np.testing.assert_array_almost_equal(xv * 1.5, dyv, 4)
Expand All @@ -210,9 +210,9 @@ def go2(inps, gs):
zz = at_sum(op_linear(xx, ww, bb))
dx, dw, db = grad(zz, [xx, ww, bb])
fn = function([xx, ww, bb], [dx, dw, db])
xv = np.random.rand(16).astype(config.floatX)
wv = np.random.rand(16).astype(config.floatX)
bv = np.random.rand(16).astype(config.floatX)
xv = np.random.random((16,)).astype(config.floatX)
wv = np.random.random((16,)).astype(config.floatX)
bv = np.random.random((16,)).astype(config.floatX)
dxv, dwv, dbv = fn(xv, wv, bv)
np.testing.assert_array_almost_equal(wv * 2, dxv, 4)
np.testing.assert_array_almost_equal(xv * 1.5, dwv, 4)
Expand Down Expand Up @@ -262,7 +262,7 @@ def lop_ov(inps, outs, grads):
gyy2 = grad(yy2, xx)
fn = function([xx], [gyy1, gyy2])

xval = np.random.rand(32).astype(config.floatX)
xval = np.random.random((32,)).astype(config.floatX)
y1val, y2val = fn(xval)
np.testing.assert_array_almost_equal(y1val, y2val, 4)

Expand All @@ -280,9 +280,9 @@ def test_rop(self, cls_ofg):
du = vector()
dv = Rop(y, x, du)
fn = function([x, W, du], dv)
xval = np.random.rand(16).astype(config.floatX)
Wval = np.random.rand(16, 16).astype(config.floatX)
duval = np.random.rand(16).astype(config.floatX)
xval = np.random.random((16,)).astype(config.floatX)
Wval = np.random.random((16, 16)).astype(config.floatX)
duval = np.random.random((16,)).astype(config.floatX)
dvval = np.dot(duval, Wval)
dvval2 = fn(xval, Wval, duval)
np.testing.assert_array_almost_equal(dvval2, dvval, 4)
Expand Down Expand Up @@ -310,7 +310,7 @@ def ro(inps, epts):
zz = op_mul(xx, yy)
dw = Rop(zz, [xx, yy], [du, dv])
fn = function([xx, yy, du, dv], dw)
vals = np.random.rand(4, 32).astype(config.floatX)
vals = np.random.random((4, 32)).astype(config.floatX)
dwval = fn(*vals)
np.testing.assert_array_almost_equal(
dwval, vals[0] * vals[3] * 1.5 + vals[1] * vals[2] * 2.0, 4
Expand Down Expand Up @@ -363,8 +363,8 @@ def test_nested(self, cls_ofg):
xx2, yy2 = op_ift(*op_ft(xx, yy))
fn = function([xx, yy], [xx2, yy2])

xv = np.random.rand(16).astype(config.floatX)
yv = np.random.rand(16).astype(config.floatX)
xv = np.random.random((16,)).astype(config.floatX)
yv = np.random.random((16,)).astype(config.floatX)
xv2, yv2 = fn(xv, yv)
np.testing.assert_array_almost_equal(xv, xv2, 4)
np.testing.assert_array_almost_equal(yv, yv2, 4)
Expand Down
23 changes: 16 additions & 7 deletions tests/compile/test_nanguardmode.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,19 @@ def test_NanGuardMode():
# Tests if NanGuardMode is working by feeding in numpy.inf and numpy.nans
# intentionally. A working implementation should be able to capture all
# the abnormalties.
rng = np.random.default_rng(2482)
x = matrix()
w = shared(np.random.randn(5, 7).astype(config.floatX))
w = shared(rng.standard_normal((5, 7)).astype(config.floatX))
y = dot(x, w)

fun = function([x], y, mode=NanGuardMode(nan_is_error=True, inf_is_error=True))
a = np.random.randn(3, 5).astype(config.floatX)
infa = np.tile((np.asarray(100.0) ** 1000000).astype(config.floatX), (3, 5))
a = rng.standard_normal((3, 5)).astype(config.floatX)

with pytest.warns(RuntimeWarning):
infa = np.tile((np.asarray(100.0) ** 1000000).astype(config.floatX), (3, 5))

nana = np.tile(np.asarray(np.nan).astype(config.floatX), (3, 5))

biga = np.tile(np.asarray(1e20).astype(config.floatX), (3, 5))

fun(a) # normal values
Expand All @@ -38,17 +43,21 @@ def test_NanGuardMode():
_logger.propagate = False
with pytest.raises(AssertionError):
fun(infa) # INFs
with pytest.raises(AssertionError):
with pytest.raises(AssertionError), pytest.warns(RuntimeWarning):
fun(nana) # NANs
with pytest.raises(AssertionError):
fun(biga) # big values
finally:
_logger.propagate = True

# slices
a = np.random.randn(3, 4, 5).astype(config.floatX)
infa = np.tile((np.asarray(100.0) ** 1000000).astype(config.floatX), (3, 4, 5))
a = rng.standard_normal((3, 4, 5)).astype(config.floatX)

with pytest.warns(RuntimeWarning):
infa = np.tile((np.asarray(100.0) ** 1000000).astype(config.floatX), (3, 4, 5))

nana = np.tile(np.asarray(np.nan).astype(config.floatX), (3, 4, 5))

biga = np.tile(np.asarray(1e20).astype(config.floatX), (3, 4, 5))

x = tensor3()
Expand All @@ -59,7 +68,7 @@ def test_NanGuardMode():
_logger.propagate = False
with pytest.raises(AssertionError):
fun(infa) # INFs
with pytest.raises(AssertionError):
with pytest.raises(AssertionError), pytest.warns(RuntimeWarning):
fun(nana) # NANs
with pytest.raises(AssertionError):
fun(biga) # big values
Expand Down
13 changes: 6 additions & 7 deletions tests/compile/test_shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ def test_ctors(self):
# test tensor constructor
b = shared(np.zeros((5, 5), dtype="int32"))
assert b.type == TensorType("int32", shape=[False, False])
b = shared(np.random.rand(4, 5))
b = shared(np.random.random((4, 5)))
assert b.type == TensorType("float64", shape=[False, False])
b = shared(np.random.rand(5, 1, 2))
b = shared(np.random.random((5, 1, 2)))
assert b.type == TensorType("float64", shape=[False, False, False])

assert shared([]).type == generic
Expand Down Expand Up @@ -178,7 +178,7 @@ def f(var, val):

b = shared(np.zeros((5, 5), dtype="float32"))
with pytest.raises(TypeError):
f(b, np.random.rand(5, 5))
f(b, np.random.random((5, 5)))

def test_tensor_strict(self):
def f(var, val):
Expand Down Expand Up @@ -228,7 +228,7 @@ def f(var, val):

b = shared(np.zeros((5, 5), dtype="float32"))
with pytest.raises(TypeError):
f(b, np.random.rand(5, 5))
f(b, np.random.random((5, 5)))

def test_scalar_floatX(self):

Expand Down Expand Up @@ -285,7 +285,7 @@ def f(var, val):

b = shared(np.zeros((5, 5), dtype="float32"))
with pytest.raises(TypeError):
f(b, np.random.rand(5, 5))
f(b, np.random.random((5, 5)))

def test_tensor_floatX(self):
def f(var, val):
Expand Down Expand Up @@ -338,9 +338,8 @@ def f(var, val):

b = shared(np.zeros((5, 5), dtype="float32"))
with pytest.raises(TypeError):
f(b, np.random.rand(5, 5))
f(b, np.random.random((5, 5)))

def test_err_symbolic_variable(self):
with pytest.raises(TypeError):
shared(aesara.tensor.ones((2, 3)))
shared(np.ones((2, 4)))
2 changes: 1 addition & 1 deletion tests/graph/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ def test_tensorvariable(self):
Variable.__count__ = count(autoname_id)
r1 = TensorType(dtype="int32", shape=())("myvar")
r2 = TensorVariable(TensorType(dtype="int32", shape=()))
r3 = shared(np.random.randn(3, 4))
r3 = shared(np.random.standard_normal((3, 4)))
assert r1.auto_name == "auto_" + str(autoname_id)
assert r2.auto_name == "auto_" + str(autoname_id + 1)
assert r3.auto_name == "auto_" + str(autoname_id + 2)
Expand Down

0 comments on commit 6cca25e

Please sign in to comment.