def test_random_integers_vector(self): random = RandomStreams(utt.fetch_seed()) low = tensor.lvector() high = tensor.lvector() out = random.random_integers(low=low, high=high) assert out.ndim == 1 f = function([low, high], out) low_val = [100, 200, 300] high_val = [110, 220, 330] seed_gen = np.random.RandomState(utt.fetch_seed()) numpy_rng = np.random.RandomState(int(seed_gen.randint(2**30))) # Arguments of size (3,) val0 = f(low_val, high_val) numpy_val0 = np.asarray([ numpy_rng.randint(low=lv, high=hv + 1) for lv, hv in zip(low_val, high_val) ]) assert np.all(val0 == numpy_val0) # arguments of size (2,) val1 = f(low_val[:-1], high_val[:-1]) numpy_val1 = np.asarray([ numpy_rng.randint(low=lv, high=hv + 1) for lv, hv in zip(low_val[:-1], high_val[:-1]) ]) assert np.all(val1 == numpy_val1) # Specifying the size explicitly g = function([low, high], random.random_integers(low=low, high=high, size=(3, ))) val2 = g(low_val, high_val) numpy_rng = np.random.RandomState(int(seed_gen.randint(2**30))) numpy_val2 = np.asarray([ numpy_rng.randint(low=lv, high=hv + 1) for lv, hv in zip(low_val, high_val) ]) assert np.all(val2 == numpy_val2) with pytest.raises(ValueError): g(low_val[:-1], high_val[:-1])
def test_dtype(self): random = RandomStreams(utt.fetch_seed()) low = tensor.lscalar() high = tensor.lscalar() out = random.random_integers(low=low, high=high, size=(20, ), dtype="int8") assert out.dtype == "int8" f = function([low, high], out) val0 = f(0, 9) assert val0.dtype == "int8" val1 = f(255, 257) assert val1.dtype == "int8" assert np.all(abs(val1) <= 1)
def test_random_integers(self): # Test that RandomStreams.random_integers generates the same # results as numpy. We use randint() for numpy since # random_integers() is deprecated. # Check over two calls to see if the random state is correctly updated. random = RandomStreams(utt.fetch_seed()) fn = function([], random.random_integers((20, 20), -5, 5)) fn_val0 = fn() fn_val1 = fn() rng_seed = np.random.RandomState(utt.fetch_seed()).randint(2**30) rng = np.random.RandomState(int(rng_seed)) # int() is for 32bit numpy_val0 = rng.randint(-5, 6, size=(20, 20)) numpy_val1 = rng.randint(-5, 6, size=(20, 20)) assert np.all(fn_val0 == numpy_val0) assert np.all(fn_val1 == numpy_val1)