提交 1ff4b9d3 authored 作者: kc611's avatar kc611 提交者: Brandon T. Willard

Refactor tests.tensor to use NumPy Generator

上级 8044a412
......@@ -37,7 +37,7 @@ def flip(kern, kshp):
return flip
global_rng = np.random.RandomState(3423489)
global_rng = np.random.default_rng(3423489)
dmatrix4 = TensorType("float64", (False, False, False, False))
......@@ -64,7 +64,7 @@ def exec_multilayer_conv_nnet_old(
img = dmatrix()
# build actual input images
imgval = global_rng.rand(bsize, imshp[0], imshp[1], imshp[2])
imgval = global_rng.random((bsize, imshp[0], imshp[1], imshp[2]))
a = dmatrix()
kerns = [a for i in nkerns]
......@@ -82,7 +82,7 @@ def exec_multilayer_conv_nnet_old(
print(conv_mode, ss, n_layer, kshp, nkern)
# actual values
w = global_rng.random_sample(np.r_[nkern, imshp[0], kshp])
w = global_rng.random((np.r_[nkern, imshp[0], kshp]))
w_flip = flip(w, kshp).reshape(w.shape)
# manual implementation
......@@ -193,7 +193,7 @@ def exec_multilayer_conv_nnet(
img = dmatrix()
# build actual input images
imgval = global_rng.rand(bsize, imshp[0], imshp[1], imshp[2])
imgval = global_rng.random((bsize, imshp[0], imshp[1], imshp[2]))
a = dmatrix()
kerns = [a for i in nkerns]
......@@ -211,7 +211,7 @@ def exec_multilayer_conv_nnet(
print(conv_mode, ss, n_layer, kshp, nkern)
# actual values
w = global_rng.random_sample(np.r_[nkern, imshp[0], kshp])
w = global_rng.random((np.r_[nkern, imshp[0], kshp]))
w_flip = flip(w, kshp).reshape(w.shape)
outshp = np.hstack(
......
......@@ -1903,7 +1903,7 @@ class TestConv2dTranspose:
)
class TestConv2dGrads:
def setup_method(self):
self.random_stream = np.random.RandomState(utt.fetch_seed())
self.random_stream = np.random.default_rng(utt.fetch_seed())
self.inputs_shapes = [(8, 1, 12, 12), (1, 1, 5, 5), (1, 1, 5, 6), (1, 1, 6, 6)]
self.filters_shapes = [(5, 1, 2, 2), (1, 1, 3, 3)]
......@@ -1928,12 +1928,12 @@ class TestConv2dGrads:
for bm in self.border_modes:
for ss in self.subsamples:
for ff in self.filter_flip:
input_val = self.random_stream.random_sample(in_shape).astype(
input_val = self.random_stream.random(in_shape).astype(
config.floatX
)
filter_val = self.random_stream.random(fltr_shape).astype(
config.floatX
)
filter_val = self.random_stream.random_sample(
fltr_shape
).astype(config.floatX)
out_grad_shape = (
aesara.tensor.nnet.abstract_conv.get_conv_output_shape(
image_shape=in_shape,
......@@ -1942,9 +1942,9 @@ class TestConv2dGrads:
subsample=ss,
)
)
out_grad_val = self.random_stream.random_sample(
out_grad_shape
).astype(config.floatX)
out_grad_val = self.random_stream.random(out_grad_shape).astype(
config.floatX
)
conv_out = aesara.tensor.nnet.conv2d(
self.x,
filters=self.w,
......@@ -1994,12 +1994,12 @@ class TestConv2dGrads:
for bm in self.border_modes:
for ss in self.subsamples:
for ff in self.filter_flip:
input_val = self.random_stream.random_sample(in_shape).astype(
input_val = self.random_stream.random(in_shape).astype(
config.floatX
)
filter_val = self.random_stream.random(fltr_shape).astype(
config.floatX
)
filter_val = self.random_stream.random_sample(
fltr_shape
).astype(config.floatX)
out_grad_shape = (
aesara.tensor.nnet.abstract_conv.get_conv_output_shape(
image_shape=in_shape,
......@@ -2008,9 +2008,9 @@ class TestConv2dGrads:
subsample=ss,
)
)
out_grad_val = self.random_stream.random_sample(
out_grad_shape
).astype(config.floatX)
out_grad_val = self.random_stream.random(out_grad_shape).astype(
config.floatX
)
conv_out = aesara.tensor.nnet.conv2d(
self.x,
filters=self.w,
......
......@@ -28,12 +28,12 @@ def test_BNComposite():
n = (x - M) / V
return n * G + B
np.random.seed(1234)
X = 1 + np.random.random([10, 20]).astype("float32")
B = 1 + np.random.random([20]).astype("float32")
G = 1 + np.random.random([20]).astype("float32")
M = 1 + np.random.random([20]).astype("float32")
V = 1 + np.random.random([20]).astype("float32")
rng = np.random.default_rng(1234)
X = 1 + rng.random([10, 20]).astype("float32")
B = 1 + rng.random([20]).astype("float32")
G = 1 + rng.random([20]).astype("float32")
M = 1 + rng.random([20]).astype("float32")
V = 1 + rng.random([20]).astype("float32")
x = matrix("x")
b = vector("b")
......@@ -41,11 +41,11 @@ def test_BNComposite():
m = vector("m")
v = vector("v")
x.tag.test_value = np.random.rand(2, 2).astype(aesara.config.floatX)
b.tag.test_value = np.random.rand(2).astype(aesara.config.floatX)
g.tag.test_value = np.random.rand(2).astype(aesara.config.floatX)
m.tag.test_value = np.random.rand(2).astype(aesara.config.floatX)
v.tag.test_value = np.random.rand(2).astype(aesara.config.floatX)
x.tag.test_value = rng.random((2, 2)).astype(aesara.config.floatX)
b.tag.test_value = rng.random((2)).astype(aesara.config.floatX)
g.tag.test_value = rng.random((2)).astype(aesara.config.floatX)
m.tag.test_value = rng.random((2)).astype(aesara.config.floatX)
v.tag.test_value = rng.random((2)).astype(aesara.config.floatX)
bn_ref_op = bn_ref(x, g, b, m, v)
f_ref = aesara.function([x, b, g, m, v], [bn_ref_op])
......@@ -62,12 +62,12 @@ def test_batch_normalization():
n = (x - M) / V
return n * G + B
np.random.seed(1234)
X = 1 + np.random.random([10, 20]).astype("float32")
B = 1 + np.random.random([20]).astype("float32")
G = 1 + np.random.random([20]).astype("float32")
M = 1 + np.random.random([20]).astype("float32")
V = 1 + np.random.random([20]).astype("float32")
rng = np.random.default_rng(1234)
X = 1 + rng.random([10, 20]).astype("float32")
B = 1 + rng.random([20]).astype("float32")
G = 1 + rng.random([20]).astype("float32")
M = 1 + rng.random([20]).astype("float32")
V = 1 + rng.random([20]).astype("float32")
x = matrix("x")
b = vector("b")
......@@ -124,12 +124,12 @@ def test_bn_feature_maps():
n = (x - M) / V
return n * G + B
np.random.seed(1234)
X = 1 + np.random.random([2, 3, 4, 4]).astype("float32")
B = 1 + np.random.random([3]).astype("float32")
G = 1 + np.random.random([3]).astype("float32")
M = 1 + np.random.random([3]).astype("float32")
V = 1 + np.random.random([3]).astype("float32")
rng = np.random.default_rng(1234)
X = 1 + rng.random([2, 3, 4, 4]).astype("float32")
B = 1 + rng.random([3]).astype("float32")
G = 1 + rng.random([3]).astype("float32")
M = 1 + rng.random([3]).astype("float32")
V = 1 + rng.random([3]).astype("float32")
x = tensor4("x")
b = vector("b")
......@@ -175,7 +175,6 @@ def test_bn_feature_maps():
@pytest.mark.slow
def test_batch_normalization_train():
utt.seed_rng()
for axes in ("per-activation", "spatial", (1, 2, 3, 4)):
for vartype in (tensor5, tensor3, vector):
......@@ -320,19 +319,18 @@ def test_batch_normalization_train():
param_shape = tuple(
1 if d in axes2 else s for d, s in enumerate(data_shape)
)
X = 4 + 3 * np.random.randn(*data_shape).astype(aesara.config.floatX)
Dy = -1 + 2 * np.random.randn(*data_shape).astype(aesara.config.floatX)
Scale = np.random.randn(*param_shape).astype(aesara.config.floatX)
Bias = np.random.randn(*param_shape).astype(aesara.config.floatX)
Running_mean = np.random.randn(*param_shape).astype(
aesara.config.floatX
)
Running_var = np.random.randn(*param_shape).astype(aesara.config.floatX)
Dx = 4 + 3 * np.random.randn(*data_shape).astype(aesara.config.floatX)
Dscale = -1 + 2 * np.random.randn(*param_shape).astype(
aesara.config.floatX
)
Dbias = np.random.randn(*param_shape).astype(aesara.config.floatX)
rng = np.random.default_rng(1234)
X = 4 + 3 * rng.random(data_shape).astype(aesara.config.floatX)
Dy = -1 + 2 * rng.random(data_shape).astype(aesara.config.floatX)
Scale = rng.random(param_shape).astype(aesara.config.floatX)
Bias = rng.random(param_shape).astype(aesara.config.floatX)
Running_mean = rng.random(param_shape).astype(aesara.config.floatX)
Running_var = rng.random(param_shape).astype(aesara.config.floatX)
Dx = 4 + 3 * rng.random(data_shape).astype(aesara.config.floatX)
Dscale = -1 + 2 * rng.random(param_shape).astype(aesara.config.floatX)
Dbias = rng.random(param_shape).astype(aesara.config.floatX)
outputs = f(
X, Scale, Bias, Running_mean, Running_var, Dy, Dx, Dscale, Dbias
......@@ -361,7 +359,6 @@ def test_batch_normalization_train():
@pytest.mark.slow
def test_batch_normalization_train_grad_grad():
utt.seed_rng()
for axes in ("per-activation", "spatial", (1, 2, 3, 4)):
for vartype in (tensor5, tensor4, tensor3, matrix, vector):
......@@ -407,12 +404,13 @@ def test_batch_normalization_train_grad_grad():
param_shape = tuple(
1 if d in axes else s for d, s in enumerate(data_shape)
)
rng = np.random.default_rng(1234)
# force float64 for sufficient numerical stability
x_val = 4 + 3 * np.random.randn(*data_shape).astype("float64")
dy_val = -1 + 2 * np.random.randn(*data_shape).astype("float64")
scale_val = np.random.randn(*param_shape).astype("float64")
x_mean_val = np.random.randn(*param_shape).astype("float64")
x_invstd_val = np.random.randn(*param_shape).astype("float64")
x_val = 4 + 3 * rng.random(data_shape).astype("float64")
dy_val = -1 + 2 * rng.random(data_shape).astype("float64")
scale_val = rng.random(param_shape).astype("float64")
x_mean_val = rng.random(param_shape).astype("float64")
x_invstd_val = rng.random(param_shape).astype("float64")
utt.verify_grad(
bn_grad_wrt_inputs_f,
......@@ -432,7 +430,6 @@ def test_batch_normalization_train_grad_grad():
def test_batch_normalization_train_without_running_averages():
# compile and run batch_normalization_train without running averages
utt.seed_rng()
x, scale, bias, dy = (
tensor4("x"),
......@@ -466,10 +463,11 @@ def test_batch_normalization_train_without_running_averages():
]
)
# run
X = 4 + 3 * np.random.randn(*data_shape).astype(aesara.config.floatX)
Dy = -1 + 2 * np.random.randn(*data_shape).astype(aesara.config.floatX)
Scale = np.random.randn(*param_shape).astype(aesara.config.floatX)
Bias = np.random.randn(*param_shape).astype(aesara.config.floatX)
rng = np.random.default_rng(1234)
X = 4 + 3 * rng.random(data_shape).astype(aesara.config.floatX)
Dy = -1 + 2 * rng.random(data_shape).astype(aesara.config.floatX)
Scale = rng.random(param_shape).astype(aesara.config.floatX)
Bias = rng.random(param_shape).astype(aesara.config.floatX)
f(X, Scale, Bias, Dy)
......@@ -565,7 +563,7 @@ def test_batch_normalization_train_broadcast():
assert len(nodes) == 1
assert isinstance(nodes[0].op, aesara.compile.DeepCopyOp)
inputs = [
np.asarray(np.random.rand(*((4,) * n)), x.dtype)
np.asarray(np.random.random(((4,) * n)), x.dtype)
for n in [
x.ndim,
scale.ndim,
......@@ -641,12 +639,13 @@ def test_batch_normalization_test():
param_shape = tuple(
1 if d in axes2 else s for d, s in enumerate(data_shape)
)
X = 4 + 3 * np.random.randn(*data_shape).astype(aesara.config.floatX)
Dy = -1 + 2 * np.random.randn(*data_shape).astype(aesara.config.floatX)
Scale = np.random.randn(*param_shape).astype(aesara.config.floatX)
Bias = np.random.randn(*param_shape).astype(aesara.config.floatX)
Mean = np.random.randn(*param_shape).astype(aesara.config.floatX)
Var = np.random.rand(*param_shape).astype(aesara.config.floatX)
rng = np.random.default_rng(1234)
X = 4 + 3 * rng.random(data_shape).astype(aesara.config.floatX)
Dy = -1 + 2 * rng.random(data_shape).astype(aesara.config.floatX)
Scale = rng.random(param_shape).astype(aesara.config.floatX)
Bias = rng.random(param_shape).astype(aesara.config.floatX)
Mean = rng.random(param_shape).astype(aesara.config.floatX)
Var = rng.random(param_shape).astype(aesara.config.floatX)
outputs = f(X, Scale, Bias, Mean, Var, Dy)
# compare outputs
utt.assert_allclose(outputs[0], outputs[1]) # out
......
......@@ -20,7 +20,7 @@ from aesara.tensor.type import fmatrix, ftensor3, ftensor4, imatrix
class TestBlockSparseGemvAndOuter(utt.InferShapeTester):
def setup_method(self):
utt.seed_rng()
mode = None
if aesara.config.mode == "FAST_COMPILE":
mode = "FAST_RUN"
......
......@@ -437,7 +437,7 @@ def test_dirichlet_samples():
assert res.shape == (2, 3)
assert all(np.all(r[i] > np.delete(r, [i])) for r in res)
rng_state = np.random.RandomState(np.random.MT19937(np.random.SeedSequence(1234)))
rng_state = np.random.Generator(np.random.MT19937(np.random.SeedSequence(1234)))
alphas = np.array([[1000, 1, 1], [1, 1000, 1], [1, 1, 1000]], dtype=config.floatX)
......@@ -661,7 +661,7 @@ def test_multinomial_samples():
)
rng_state = shared(
np.random.RandomState(np.random.MT19937(np.random.SeedSequence(1234)))
np.random.Generator(np.random.MT19937(np.random.SeedSequence(1234)))
)
test_M = np.array([10, 20], dtype="int64")
......@@ -678,7 +678,7 @@ def test_multinomial_samples():
def test_categorical_samples():
rng_state = np.random.RandomState(np.random.MT19937(np.random.SeedSequence(1234)))
rng_state = np.random.Generator(np.random.MT19937(np.random.SeedSequence(1234)))
assert categorical.rng_fn(rng_state, np.array([1.0 / 3.0] * 3), size=10).shape == (
10,
......
......@@ -129,7 +129,7 @@ def test_inplace_optimization():
],
)
def test_local_rv_size_lift(dist_op, dist_params, size):
rng = shared(np.random.RandomState(1233532), borrow=False)
rng = shared(np.random.default_rng(1233532), borrow=False)
new_out, f_inputs, dist_st, f_opt = apply_local_opt_to_rv(
local_rv_size_lift,
......@@ -284,7 +284,7 @@ def test_local_rv_size_lift(dist_op, dist_params, size):
@config.change_flags(compute_test_value_opt="raise", compute_test_value="raise")
def test_DimShuffle_lift(ds_order, lifted, dist_op, dist_params, size, rtol):
rng = shared(np.random.RandomState(1233532), borrow=False)
rng = shared(np.random.default_rng(1233532), borrow=False)
new_out, f_inputs, dist_st, f_opt = apply_local_opt_to_rv(
local_dimshuffle_rv_lift,
......@@ -400,7 +400,7 @@ def test_DimShuffle_lift(ds_order, lifted, dist_op, dist_params, size, rtol):
def test_Subtensor_lift(indices, lifted, dist_op, dist_params, size):
from aesara.tensor.subtensor import as_index_constant
rng = shared(np.random.RandomState(1233532), borrow=False)
rng = shared(np.random.default_rng(1233532), borrow=False)
indices_aet = ()
for i in indices:
......@@ -445,7 +445,7 @@ def test_Subtensor_lift(indices, lifted, dist_op, dist_params, size):
def test_Subtensor_lift_restrictions():
rng = shared(np.random.RandomState(1233532), borrow=False)
rng = shared(np.random.default_rng(1233532), borrow=False)
std = vector("std")
std.tag.test_value = np.array([1e-5, 2e-5, 3e-5], dtype=config.floatX)
......@@ -477,7 +477,7 @@ def test_Subtensor_lift_restrictions():
def test_Dimshuffle_lift_restrictions():
rng = shared(np.random.RandomState(1233532), borrow=False)
rng = shared(np.random.default_rng(1233532), borrow=False)
x = normal(aet.arange(2).reshape((2,)), 100, size=(2, 2, 2), rng=rng)
y = x.dimshuffle(1, 0, 2)
......
......@@ -12,9 +12,6 @@ _ = pytest.importorskip("scipy.signal")
class TestSignalConv2D:
def setup_method(self):
utt.seed_rng()
def validate(self, image_shape, filter_shape, out_dim, verify_grad=True):
image_dim = len(image_shape)
......
差异被折叠。
差异被折叠。
......@@ -202,7 +202,7 @@ class TestCGemv(OptimizationTestMixin):
def t_gemv1(self, m_shp):
""" test vector2 + dot(matrix, vector1) """
rng = np.random.RandomState(unittest_tools.fetch_seed())
rng = np.random.default_rng(unittest_tools.fetch_seed())
v1 = aesara.shared(np.array(rng.uniform(size=(m_shp[1],)), dtype="float32"))
v2_orig = np.array(rng.uniform(size=(m_shp[0],)), dtype="float32")
v2 = aesara.shared(v2_orig)
......@@ -278,9 +278,9 @@ class TestCGemv(OptimizationTestMixin):
f = aesara.function(
[x, y, z], [aet.dot(y, x), aet.dot(z, x)], mode=mode_blas_opt
)
vx = np.random.rand(3, 3)
vy = np.random.rand(3)
vz = np.random.rand(3)
vx = np.random.random((3, 3))
vy = np.random.random((3))
vz = np.random.random((3))
out = f(vx, vy, vz)
assert np.allclose(out[0], np.dot(vy, vx))
assert np.allclose(out[1], np.dot(vz, vx))
......@@ -317,9 +317,6 @@ class TestCGemvNoFlags:
N = 5
slice_step = 3
def setup_method(self):
unittest_tools.seed_rng()
def get_function(self, dtype, transpose_A=False, slice_tensors=False):
alpha = scalar(dtype=dtype)
beta = scalar(dtype=dtype)
......
......@@ -12,20 +12,25 @@ from tests import unittest_tools as utt
class TestRealImag:
def test_basic(self):
x = zvector()
rng = np.random.RandomState(23)
xval = np.asarray(list(np.complex(rng.randn(), rng.randn()) for i in range(10)))
rng = np.random.default_rng(23)
xval = np.asarray(
list(
np.complex(rng.standard_normal(), rng.standard_normal())
for i in range(10)
)
)
assert np.all(xval.real == aesara.function([x], real(x))(xval))
assert np.all(xval.imag == aesara.function([x], imag(x))(xval))
def test_on_real_input(self):
x = dvector()
rng = np.random.RandomState(23)
xval = rng.randn(10)
rng = np.random.default_rng(23)
xval = rng.standard_normal((10))
np.all(0 == aesara.function([x], imag(x))(xval))
np.all(xval == aesara.function([x], real(x))(xval))
x = imatrix()
xval = np.asarray(rng.randn(3, 3) * 100, dtype="int32")
xval = np.asarray(rng.standard_normal((3, 3)) * 100, dtype="int32")
np.all(0 == aesara.function([x], imag(x))(xval))
np.all(xval == aesara.function([x], real(x))(xval))
......@@ -35,7 +40,7 @@ class TestRealImag:
cast(x, "int32")
def test_complex(self):
rng = np.random.RandomState(2333)
rng = np.random.default_rng(2333)
m = fmatrix()
c = complex(m[0], m[1])
assert c.type == cvector
......@@ -44,7 +49,7 @@ class TestRealImag:
assert i.type == fvector
f = aesara.function([m], [r, i])
mval = np.asarray(rng.randn(2, 5), dtype="float32")
mval = np.asarray(rng.standard_normal((2, 5)), dtype="float32")
rval, ival = f(mval)
assert np.all(rval == mval[0]), (rval, mval[0])
assert np.all(ival == mval[1]), (ival, mval[1])
......@@ -55,8 +60,8 @@ class TestRealImag:
c = complex(m[0], m[1])
return 0.5 * real(c) + 0.9 * imag(c)
rng = np.random.RandomState(9333)
mval = np.asarray(rng.randn(2, 5))
rng = np.random.default_rng(9333)
mval = np.asarray(rng.standard_normal((2, 5)))
utt.verify_grad(f, [mval])
@pytest.mark.skip(reason="Complex grads not enabled, see #178")
......@@ -65,8 +70,8 @@ class TestRealImag:
ac = complex(a[0], a[1])
return abs((ac) ** 2).sum()
rng = np.random.RandomState(9333)
aval = np.asarray(rng.randn(2, 5))
rng = np.random.default_rng(9333)
aval = np.asarray(rng.standard_normal((2, 5)))
try:
utt.verify_grad(f, [aval])
except GradientError as e:
......@@ -80,8 +85,8 @@ class TestRealImag:
ac = complex(a[0], a[1])
return abs(ac).sum()
rng = np.random.RandomState(9333)
aval = np.asarray(rng.randn(2, 5))
rng = np.random.default_rng(9333)
aval = np.asarray(rng.standard_normal((2, 5)))
try:
utt.verify_grad(f, [aval])
except GradientError as e:
......@@ -95,9 +100,9 @@ class TestRealImag:
ac = complex(a[0], a[1])
return abs((ac * b) ** 2).sum()
rng = np.random.RandomState(9333)
aval = np.asarray(rng.randn(2, 5))
bval = rng.randn(5)
rng = np.random.default_rng(9333)
aval = np.asarray(rng.standard_normal((2, 5)))
bval = rng.standard_normal((5))
try:
utt.verify_grad(f, [aval, bval])
except GradientError as e:
......@@ -111,8 +116,8 @@ class TestRealImag:
c = complex_from_polar(abs(m[0]), m[1])
return 0.5 * real(c) + 0.9 * imag(c)
rng = np.random.RandomState(9333)
mval = np.asarray(rng.randn(2, 5))
rng = np.random.default_rng(9333)
mval = np.asarray(rng.standard_normal((2, 5)))
utt.verify_grad(f, [mval])
@pytest.mark.skip(reason="Complex grads not enabled, see #178")
......@@ -121,6 +126,6 @@ class TestRealImag:
c = complex(m[0], m[1])
return 0.5 * abs(c)
rng = np.random.RandomState(9333)
mval = np.asarray(rng.randn(2, 5))
rng = np.random.default_rng(9333)
mval = np.asarray(rng.standard_normal((2, 5)))
utt.verify_grad(f, [mval])
......@@ -135,13 +135,10 @@ class TestBroadcast:
linkers = [PerformLinker, CLinker]
def rand_val(self, shp):
return np.asarray(np.random.rand(*shp), dtype=aesara.config.floatX)
return np.asarray(np.random.random(shp), dtype=aesara.config.floatX)
def rand_cval(self, shp):
return np.asarray(np.random.rand(*shp), dtype=aesara.config.floatX)
def setup_method(self):
unittest_tools.seed_rng()
return np.asarray(np.random.random(shp), dtype=aesara.config.floatX)
def with_linker(self, linker, op, type, rand_val):
for xsh, ysh in [
......@@ -356,7 +353,7 @@ class TestCAReduce(unittest_tools.InferShapeTester):
tosum = list(range(len(xsh)))
f = aesara.function([x], e, mode=mode)
xv = np.asarray(np.random.rand(*xsh))
xv = np.asarray(np.random.random(xsh))
if dtype not in discrete_dtypes:
xv = np.asarray(xv, dtype=dtype)
......@@ -561,7 +558,7 @@ class TestCAReduce(unittest_tools.InferShapeTester):
x = pre_scalar_op(x)
if tosum is None:
tosum = list(range(len(xsh)))
xv = np.asarray(np.random.rand(*xsh), dtype=dtype)
xv = np.asarray(np.random.random(xsh), dtype=dtype)
d = {}
if pre_scalar_op is not None:
xv = x.eval({x.owner.inputs[0]: xv})
......@@ -578,7 +575,7 @@ class TestCAReduce(unittest_tools.InferShapeTester):
class TestBitOpReduceGrad:
def setup_method(self):
self.rng = np.random.RandomState(unittest_tools.fetch_seed())
self.rng = np.random.default_rng(unittest_tools.fetch_seed())
def test_all_grad(self):
x = bmatrix("x")
......
......@@ -69,7 +69,7 @@ from tests import unittest_tools as utt
def test_cpu_contiguous():
a = fmatrix("a")
i = iscalar("i")
a_val = np.asarray(np.random.rand(4, 5), dtype="float32")
a_val = np.asarray(np.random.random((4, 5)), dtype="float32")
f = aesara.function([a, i], cpu_contiguous(a.reshape((5, 4))[::i]))
topo = f.maker.fgraph.toposort()
assert any([isinstance(node.op, CpuContiguous) for node in topo])
......@@ -78,7 +78,7 @@ def test_cpu_contiguous():
assert f(a_val, 3).flags["C_CONTIGUOUS"]
# Test the grad:
utt.verify_grad(cpu_contiguous, [np.random.rand(5, 7, 2)])
utt.verify_grad(cpu_contiguous, [np.random.random((5, 7, 2))])
class TestSearchsortedOp(utt.InferShapeTester):
......@@ -89,9 +89,9 @@ class TestSearchsortedOp(utt.InferShapeTester):
self.x = vector("x")
self.v = tensor3("v")
self.a = 30 * np.random.random(50).astype(config.floatX)
self.b = 30 * np.random.random((8, 10, 5)).astype(config.floatX)
self.rng = np.random.default_rng(utt.fetch_seed())
self.a = 30 * self.rng.random(50).astype(config.floatX)
self.b = 30 * self.rng.random((8, 10, 5)).astype(config.floatX)
self.idx_sorted = np.argsort(self.a).astype("int32")
def test_searchsortedOp_on_sorted_input(self):
......@@ -185,7 +185,7 @@ class TestSearchsortedOp(utt.InferShapeTester):
)
def test_grad(self):
utt.verify_grad(self.op, [self.a[self.idx_sorted], self.b])
utt.verify_grad(self.op, [self.a[self.idx_sorted], self.b], rng=self.rng)
class TestCumOp(utt.InferShapeTester):
......@@ -591,7 +591,7 @@ class TestBartlett(utt.InferShapeTester):
class TestFillDiagonal(utt.InferShapeTester):
rng = np.random.RandomState(43)
rng = np.random.default_rng(43)
def setup_method(self):
super().setup_method()
......@@ -603,19 +603,19 @@ class TestFillDiagonal(utt.InferShapeTester):
y = scalar()
f = function([x, y], fill_diagonal(x, y))
for shp in [(8, 8), (5, 8), (8, 5)]:
a = np.random.rand(*shp).astype(config.floatX)
val = np.cast[config.floatX](np.random.rand())
a = np.random.random(shp).astype(config.floatX)
val = np.cast[config.floatX](np.random.random())
out = f(a, val)
# We can't use np.fill_diagonal as it is bugged.
assert np.allclose(np.diag(out), val)
assert (out == val).sum() == min(a.shape)
# test for 3dtt
a = np.random.rand(3, 3, 3).astype(config.floatX)
a = np.random.random((3, 3, 3)).astype(config.floatX)
x = tensor3()
y = scalar()
f = function([x, y], fill_diagonal(x, y))
val = np.cast[config.floatX](np.random.rand() + 10)
val = np.cast[config.floatX](np.random.random() + 10)
out = f(a, val)
# We can't use np.fill_diagonal as it is bugged.
assert out[0, 0, 0] == val
......@@ -627,13 +627,13 @@ class TestFillDiagonal(utt.InferShapeTester):
def test_gradient(self):
utt.verify_grad(
fill_diagonal,
[np.random.rand(5, 8), np.random.rand()],
[np.random.random((5, 8)), np.random.random()],
n_tests=1,
rng=TestFillDiagonal.rng,
)
utt.verify_grad(
fill_diagonal,
[np.random.rand(8, 5), np.random.rand()],
[np.random.random((8, 5)), np.random.random()],
n_tests=1,
rng=TestFillDiagonal.rng,
)
......@@ -645,14 +645,14 @@ class TestFillDiagonal(utt.InferShapeTester):
self._compile_and_check(
[x, y],
[self.op(x, y)],
[np.random.rand(8, 5), np.random.rand()],
[np.random.random((8, 5)), np.random.random()],
self.op_class,
)
self._compile_and_check(
[z, y],
[self.op(z, y)],
# must be square when nd>2
[np.random.rand(8, 8, 8), np.random.rand()],
[np.random.random((8, 8, 8)), np.random.random()],
self.op_class,
warn=False,
)
......@@ -660,7 +660,7 @@ class TestFillDiagonal(utt.InferShapeTester):
class TestFillDiagonalOffset(utt.InferShapeTester):
rng = np.random.RandomState(43)
rng = np.random.default_rng(43)
def setup_method(self):
super().setup_method()
......@@ -675,8 +675,8 @@ class TestFillDiagonalOffset(utt.InferShapeTester):
f = function([x, y, z], fill_diagonal_offset(x, y, z))
for test_offset in (-5, -4, -1, 0, 1, 4, 5):
for shp in [(8, 8), (5, 8), (8, 5), (5, 5)]:
a = np.random.rand(*shp).astype(config.floatX)
val = np.cast[config.floatX](np.random.rand())
a = np.random.random(shp).astype(config.floatX)
val = np.cast[config.floatX](np.random.random())
out = f(a, val, test_offset)
# We can't use np.fill_diagonal as it is bugged.
assert np.allclose(np.diag(out, test_offset), val)
......@@ -697,19 +697,19 @@ class TestFillDiagonalOffset(utt.InferShapeTester):
utt.verify_grad(
fill_diagonal_with_fix_offset,
[np.random.rand(5, 8), np.random.rand()],
[np.random.random((5, 8)), np.random.random()],
n_tests=1,
rng=TestFillDiagonalOffset.rng,
)
utt.verify_grad(
fill_diagonal_with_fix_offset,
[np.random.rand(8, 5), np.random.rand()],
[np.random.random((8, 5)), np.random.random()],
n_tests=1,
rng=TestFillDiagonalOffset.rng,
)
utt.verify_grad(
fill_diagonal_with_fix_offset,
[np.random.rand(5, 5), np.random.rand()],
[np.random.random((5, 5)), np.random.random()],
n_tests=1,
rng=TestFillDiagonalOffset.rng,
)
......@@ -722,13 +722,13 @@ class TestFillDiagonalOffset(utt.InferShapeTester):
self._compile_and_check(
[x, y, z],
[self.op(x, y, z)],
[np.random.rand(8, 5), np.random.rand(), test_offset],
[np.random.random((8, 5)), np.random.random(), test_offset],
self.op_class,
)
self._compile_and_check(
[x, y, z],
[self.op(x, y, z)],
[np.random.rand(5, 8), np.random.rand(), test_offset],
[np.random.random((5, 8)), np.random.random(), test_offset],
self.op_class,
)
......@@ -1096,7 +1096,7 @@ def test_broadcast_shape():
class TestBroadcastTo(utt.InferShapeTester):
rng = np.random.RandomState(43)
rng = np.random.default_rng(43)
def setup_method(self):
super().setup_method()
......@@ -1134,7 +1134,7 @@ class TestBroadcastTo(utt.InferShapeTester):
def test_gradient(self, fn, input_dims):
utt.verify_grad(
fn,
[np.random.rand(*input_dims).astype(config.floatX)],
[np.random.random(input_dims).astype(config.floatX)],
n_tests=1,
rng=self.rng,
)
......@@ -1147,7 +1147,7 @@ class TestBroadcastTo(utt.InferShapeTester):
self._compile_and_check(
[a] + shape,
[out],
[np.random.rand(2, 1, 3).astype(config.floatX), 2, 1, 3],
[np.random.random((2, 1, 3)).astype(config.floatX), 2, 1, 3],
self.op_class,
)
......@@ -1156,7 +1156,7 @@ class TestBroadcastTo(utt.InferShapeTester):
self._compile_and_check(
[a] + shape,
[self.op(a, shape)],
[np.random.rand(2, 1, 3).astype(config.floatX), 6, 2, 5, 3],
[np.random.random((2, 1, 3)).astype(config.floatX), 6, 2, 5, 3],
self.op_class,
)
......
......@@ -9,7 +9,7 @@ from tests import unittest_tools as utt
class TestFourier(utt.InferShapeTester):
rng = np.random.RandomState(43)
rng = np.random.default_rng(43)
def setup_method(self):
super().setup_method()
......@@ -19,13 +19,13 @@ class TestFourier(utt.InferShapeTester):
def test_perform(self):
a = dmatrix()
f = aesara.function([a], self.op(a, n=10, axis=0))
a = np.random.rand(8, 6)
a = np.random.random((8, 6))
assert np.allclose(f(a), np.fft.fft(a, 10, 0))
def test_infer_shape(self):
a = dvector()
self._compile_and_check(
[a], [self.op(a, 16, 0)], [np.random.rand(12)], self.op_class
[a], [self.op(a, 16, 0)], [np.random.random((12))], self.op_class
)
a = dmatrix()
for var in [
......@@ -34,7 +34,9 @@ class TestFourier(utt.InferShapeTester):
self.op(a, 16, None),
self.op(a, None, None),
]:
self._compile_and_check([a], [var], [np.random.rand(12, 4)], self.op_class)
self._compile_and_check(
[a], [var], [np.random.random((12, 4))], self.op_class
)
b = iscalar()
for var in [self.op(a, 16, b), self.op(a, None, b)]:
self._compile_and_check(
......@@ -56,10 +58,10 @@ class TestFourier(utt.InferShapeTester):
return self.op(a, 4, 0)
pts = [
np.random.rand(5, 2, 4, 3),
np.random.rand(2, 3, 4),
np.random.rand(2, 5),
np.random.rand(5),
np.random.random((5, 2, 4, 3)),
np.random.random((2, 3, 4)),
np.random.random((2, 5)),
np.random.random((5)),
]
for fft_test in [fft_test1, fft_test2, fft_test3, fft_test4]:
for pt in pts:
......
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论