コード例 #1
0
    def test_multinomial_vector(self):
        random = RandomStreams(utt.fetch_seed())
        n = tensor.lvector()
        pvals = tensor.matrix()
        out = random.multinomial(n=n, pvals=pvals)
        assert out.ndim == 2
        f = function([n, pvals], out)

        n_val = [1, 2, 3]
        pvals_val = [[0.1, 0.9], [0.2, 0.8], [0.3, 0.7]]
        pvals_val = np.asarray(pvals_val, dtype=config.floatX)
        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(n_val, pvals_val)
        numpy_val0 = np.asarray([
            numpy_rng.multinomial(n=nv, pvals=pv)
            for nv, pv in zip(n_val, pvals_val)
        ])
        assert np.all(val0 == numpy_val0)

        # arguments of size (2,)
        val1 = f(n_val[:-1], pvals_val[:-1])
        numpy_val1 = np.asarray([
            numpy_rng.multinomial(n=nv, pvals=pv)
            for nv, pv in zip(n_val[:-1], pvals_val[:-1])
        ])
        assert np.all(val1 == numpy_val1)

        # Specifying the size explicitly
        g = function([n, pvals],
                     random.multinomial(n=n, pvals=pvals, size=(3, )))
        val2 = g(n_val, pvals_val)
        numpy_rng = np.random.RandomState(int(seed_gen.randint(2**30)))
        numpy_val2 = np.asarray([
            numpy_rng.multinomial(n=nv, pvals=pv)
            for nv, pv in zip(n_val, pvals_val)
        ])
        assert np.all(val2 == numpy_val2)
        with pytest.raises(ValueError):
            g(n_val[:-1], pvals_val[:-1])
コード例 #2
0
    def test_multinomial(self):
        # Test that RandomStreams.multinomial generates the same results as numpy
        # Check over two calls to see if the random state is correctly updated.
        random = RandomStreams(utt.fetch_seed())
        fn = function([],
                      random.multinomial((4, 4), 1, [0.1] * 10),
                      updates=random.updates())

        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.multinomial(1, [0.1] * 10, size=(4, 4))
        numpy_val1 = rng.multinomial(1, [0.1] * 10, size=(4, 4))

        assert np.all(fn_val0 == numpy_val0)
        assert np.all(fn_val1 == numpy_val1)
コード例 #3
0
    def test_default_shape(self):
        random = RandomStreams(utt.fetch_seed())
        f = function([], random.uniform())
        g = function([], random.multinomial())

        # seed_rng is generator for generating *seeds* for RandomStates
        seed_rng = np.random.RandomState(utt.fetch_seed())
        uniform_rng = np.random.RandomState(int(seed_rng.randint(2**30)))
        multinomial_rng = np.random.RandomState(int(seed_rng.randint(2**30)))

        val0 = f()
        val1 = f()
        numpy_val0 = uniform_rng.uniform()
        numpy_val1 = uniform_rng.uniform()
        assert np.allclose(val0, numpy_val0)
        assert np.allclose(val1, numpy_val1)

        for i in range(
                10
        ):  # every test has 50% chance of passing even with non-matching random states
            val2 = g()
            numpy_val2 = multinomial_rng.multinomial(n=1, pvals=[0.5, 0.5])
            assert np.all(val2 == numpy_val2)