Example #1
0
def test_RandomStream():
    srng = RandomStream(seed=123)
    out = srng.normal() - srng.normal()

    fn = function([], out, mode=jax_mode)
    jax_res_1 = fn()
    jax_res_2 = fn()

    assert np.array_equal(jax_res_1, jax_res_2)
Example #2
0
    def test_tutorial(self):
        srng = RandomStream(seed=234)
        rv_u = srng.uniform(0, 1, size=(2, 2))
        rv_n = srng.normal(0, 1, size=(2, 2))

        f = function([], rv_u)
        # Disabling `default_updates` means that we have to pass
        # `srng.state_updates` to `function` manually, if we want the shared
        # state to change
        g = function([], rv_n, no_default_updates=True)
        nearly_zeros = function([], rv_u + rv_u - 2 * rv_u)

        assert np.all(f() != f())
        assert np.all(g() == g())
        assert np.all(abs(nearly_zeros()) < 1e-5)
Example #3
0
    def test_seed(self):
        init_seed = 234
        random = RandomStream(init_seed)

        ref_state = np.random.RandomState(init_seed).get_state()
        random_state = random.gen_seedgen.get_state()
        assert random.default_instance_seed == init_seed
        assert np.array_equal(random_state[1], ref_state[1])
        assert random_state[0] == ref_state[0]
        assert random_state[2:] == ref_state[2:]

        new_seed = 43298
        random.seed(new_seed)

        ref_state = np.random.RandomState(new_seed).get_state()
        random_state = random.gen_seedgen.get_state()
        assert np.array_equal(random_state[1], ref_state[1])
        assert random_state[0] == ref_state[0]
        assert random_state[2:] == ref_state[2:]

        random.seed()
        ref_state = np.random.RandomState(init_seed).get_state()
        random_state = random.gen_seedgen.get_state()
        assert random.default_instance_seed == init_seed
        assert np.array_equal(random_state[1], ref_state[1])
        assert random_state[0] == ref_state[0]
        assert random_state[2:] == ref_state[2:]

        # Reset the seed
        random.seed(new_seed)

        # Check state updates
        _ = random.normal()

        # Now, change the seed when there are state updates
        random.seed(new_seed)

        rng = np.random.RandomState(new_seed)
        update_seed = rng.randint(2**30)
        ref_state = np.random.RandomState(update_seed).get_state()
        random_state = random.state_updates[0][0].get_value(
            borrow=True).get_state()
        assert np.array_equal(random_state[1], ref_state[1])
        assert random_state[0] == ref_state[0]
        assert random_state[2:] == ref_state[2:]
Example #4
0
    def test_seed(self, rng_ctor):
        init_seed = 234
        random = RandomStream(init_seed, rng_ctor=rng_ctor)

        assert random.default_instance_seed == init_seed

        new_seed = 43298
        random.seed(new_seed)

        rng_seed = np.random.SeedSequence(new_seed)
        assert random.gen_seedgen.entropy == rng_seed.entropy

        random.seed()

        rng_seed = np.random.SeedSequence(init_seed)
        assert random.gen_seedgen.entropy == rng_seed.entropy

        # Reset the seed
        random.seed(new_seed)

        # Check state updates
        _ = random.normal()

        # Now, change the seed when there are state updates
        random.seed(new_seed)

        update_seed = np.random.SeedSequence(new_seed)
        (update_seed, ) = update_seed.spawn(1)
        ref_rng = random.rng_ctor(update_seed)
        state_rng = random.state_updates[0][0].get_value(borrow=True)

        if hasattr(state_rng, "get_state"):
            ref_state = ref_rng.get_state()
            random_state = state_rng.get_state()
            assert np.array_equal(random_state[1], ref_state[1])
            assert random_state[0] == ref_state[0]
            assert random_state[2:] == ref_state[2:]
        else:
            ref_state = ref_rng.__getstate__()
            random_state = state_rng.__getstate__()
            assert random_state["bit_generator"] == ref_state["bit_generator"]
            assert random_state["state"] == ref_state["state"]
Example #5
0
def test_normal0():
    steps = 50
    std = 2.0
    if (config.mode in ("DEBUG_MODE", "DebugMode", "FAST_COMPILE")
            or config.mode == "Mode" and config.linker in ["py"]):
        sample_size = (25, 30)
        default_rtol = 0.02
    else:
        sample_size = (999, 50)
        default_rtol = 0.01
    sample_size_odd = (sample_size[0], sample_size[1] - 1)
    x = matrix()

    test_cases = [
        (sample_size, sample_size, [], [], -5.0, default_rtol, default_rtol),
        (
            x.shape,
            sample_size,
            [x],
            [np.zeros(sample_size, dtype=config.floatX)],
            -5.0,
            default_rtol,
            default_rtol,
        ),
        # test odd value
        (
            x.shape,
            sample_size_odd,
            [x],
            [np.zeros(sample_size_odd, dtype=config.floatX)],
            -5.0,
            default_rtol,
            default_rtol,
        ),
        (
            sample_size,
            sample_size,
            [],
            [],
            np.arange(np.prod(sample_size),
                      dtype="float32").reshape(sample_size),
            10.0 * std / np.sqrt(steps),
            default_rtol,
        ),
        # test empty size (scalar)
        ((), (), [], [], -5.0, default_rtol, 0.02),
        # test with few samples at the same time
        ((1, ), (1, ), [], [], -5.0, default_rtol, 0.02),
        ((3, ), (3, ), [], [], -5.0, default_rtol, 0.02),
    ]

    for size, const_size, var_input, input, avg, rtol, std_tol in test_cases:
        R = MRG_RandomStream(234)
        # Note: we specify `nstreams` to avoid a warning.
        n = R.normal(
            size=size,
            avg=avg,
            std=std,
            nstreams=rng_mrg.guess_n_streams(size, warn=False),
        )
        f = function(var_input, n)
        f(*input)

        # Increase the number of steps if size implies only a few samples
        if np.prod(const_size) < 10:
            steps_ = steps * 50
        else:
            steps_ = steps
        check_basics(
            f,
            steps_,
            const_size,
            target_avg=avg,
            target_std=std,
            prefix="mrg ",
            allow_01=True,
            inputs=input,
            mean_rtol=rtol,
            std_tol=std_tol,
        )

        sys.stdout.flush()

        RR = RandomStream(235)

        nn = RR.normal(avg, std, size=size)
        ff = function(var_input, nn)

        check_basics(
            ff,
            steps_,
            const_size,
            target_avg=avg,
            target_std=std,
            prefix="numpy ",
            allow_01=True,
            inputs=input,
            mean_rtol=rtol,
        )