def test_bad_size(self): R = MRG_RandomStreams(234) for size in [ (0, 100), (-1, 100), (1, 0), ]: with pytest.raises(ValueError): R.uniform(size) with pytest.raises(ValueError): R.binomial(size) with pytest.raises(ValueError): R.multinomial(size, 1, []) with pytest.raises(ValueError): R.normal(size) with pytest.raises(ValueError): R.truncated_normal(size)
def test_target_parameter(): srng = MRG_RandomStreams() pvals = np.array([[0.98, 0.01, 0.01], [0.01, 0.49, 0.50]]) def basic_target_parameter_test(x): f = aesara.function([], x) assert isinstance(f(), np.ndarray) basic_target_parameter_test(srng.uniform((3, 2), target="cpu")) basic_target_parameter_test(srng.normal((3, 2), target="cpu")) basic_target_parameter_test(srng.truncated_normal((3, 2), target="cpu")) basic_target_parameter_test(srng.binomial((3, 2), target="cpu")) basic_target_parameter_test( srng.multinomial(pvals=pvals.astype("float32"), target="cpu")) basic_target_parameter_test( srng.choice(p=pvals.astype("float32"), replace=False, target="cpu")) basic_target_parameter_test( srng.multinomial_wo_replacement(pvals=pvals.astype("float32"), target="cpu"))
def check_binomial(mean, size, const_size, var_input, input, steps, rtol): R = MRG_RandomStreams(234) u = R.binomial(size=size, p=mean) f = aesara.function(var_input, u) f(*input) # Increase the number of steps if sizes implies only a few samples if np.prod(const_size) < 10: steps_ = steps * 100 else: steps_ = steps check_basics( f, steps_, const_size, prefix="mrg cpu", inputs=input, allow_01=True, target_avg=mean, mean_rtol=rtol, ) RR = aesara.tensor.shared_randomstreams.RandomStreams(234) uu = RR.binomial(size=size, p=mean) ff = aesara.function(var_input, uu) # It's not our problem if numpy generates 0 or 1 check_basics( ff, steps_, const_size, prefix="numpy", allow_01=True, inputs=input, target_avg=mean, mean_rtol=rtol, )
def test_undefined_grad(): srng = MRG_RandomStreams(seed=1234) # checking uniform distribution low = tensor.scalar() out = srng.uniform((), low=low) with pytest.raises(aesara.gradient.NullTypeGradError): aesara.grad(out, low) high = tensor.scalar() out = srng.uniform((), low=0, high=high) with pytest.raises(aesara.gradient.NullTypeGradError): aesara.grad(out, high) out = srng.uniform((), low=low, high=high) with pytest.raises(aesara.gradient.NullTypeGradError): aesara.grad(out, (low, high)) # checking binomial distribution prob = tensor.scalar() out = srng.binomial((), p=prob) with pytest.raises(aesara.gradient.NullTypeGradError): aesara.grad(out, prob) # checking multinomial distribution prob1 = tensor.scalar() prob2 = tensor.scalar() p = [aesara.tensor.as_tensor_variable([prob1, 0.5, 0.25])] out = srng.multinomial(size=None, pvals=p, n=4)[0] with pytest.raises(aesara.gradient.NullTypeGradError): aesara.grad(aesara.tensor.sum(out), prob1) p = [aesara.tensor.as_tensor_variable([prob1, prob2])] out = srng.multinomial(size=None, pvals=p, n=4)[0] with pytest.raises(aesara.gradient.NullTypeGradError): aesara.grad(aesara.tensor.sum(out), (prob1, prob2)) # checking choice p = [aesara.tensor.as_tensor_variable([prob1, prob2, 0.1, 0.2])] out = srng.choice(a=None, size=1, p=p, replace=False)[0] with pytest.raises(aesara.gradient.NullTypeGradError): aesara.grad(out[0], (prob1, prob2)) p = [aesara.tensor.as_tensor_variable([prob1, prob2])] out = srng.choice(a=None, size=1, p=p, replace=False)[0] with pytest.raises(aesara.gradient.NullTypeGradError): aesara.grad(out[0], (prob1, prob2)) p = [aesara.tensor.as_tensor_variable([prob1, 0.2, 0.3])] out = srng.choice(a=None, size=1, p=p, replace=False)[0] with pytest.raises(aesara.gradient.NullTypeGradError): aesara.grad(out[0], prob1) # checking normal distribution avg = tensor.scalar() out = srng.normal((), avg=avg) with pytest.raises(aesara.gradient.NullTypeGradError): aesara.grad(out, avg) std = tensor.scalar() out = srng.normal((), avg=0, std=std) with pytest.raises(aesara.gradient.NullTypeGradError): aesara.grad(out, std) out = srng.normal((), avg=avg, std=std) with pytest.raises(aesara.gradient.NullTypeGradError): aesara.grad(out, (avg, std)) # checking truncated normal distribution avg = tensor.scalar() out = srng.truncated_normal((), avg=avg) with pytest.raises(aesara.gradient.NullTypeGradError): aesara.grad(out, avg) std = tensor.scalar() out = srng.truncated_normal((), avg=0, std=std) with pytest.raises(aesara.gradient.NullTypeGradError): aesara.grad(out, std) out = srng.truncated_normal((), avg=avg, std=std) with pytest.raises(aesara.gradient.NullTypeGradError): aesara.grad(out, (avg, std))