Example #1
0
    def test_multinomial(self):
        # Test that raw_random.multinomial generates the same results as numpy.
        # Check over two calls to see if the random state is correctly updated.
        rng_R = random_state_type()
        post_r, out = multinomial(rng_R, (7, 3), 6, [0.2] * 5)

        f = compile.function(
            [
                compile.In(
                    rng_R,
                    value=np.random.RandomState(utt.fetch_seed()),
                    update=post_r,
                    mutable=True,
                )
            ],
            [out],
            accept_inplace=True,
        )

        numpy_rng = np.random.RandomState(utt.fetch_seed())
        (val0, ) = f()
        (val1, ) = f()
        numpy_val0 = numpy_rng.multinomial(6, [0.2] * 5, (7, 3))
        numpy_val1 = numpy_rng.multinomial(6, [0.2] * 5, (7, 3))
        assert np.all(val0 == numpy_val0)
        assert np.all(val1 == numpy_val1)

        assert val0.shape == (7, 3, 5)
        assert val1.shape == (7, 3, 5)
Example #2
0
    def test_multinomial_vector(self):
        rng_R = random_state_type()
        n = tensor.lvector()
        pvals = tensor.matrix()
        post_r, out = multinomial(rng_R, n=n, pvals=pvals)
        assert out.ndim == 2
        f = compile.function([rng_R, n, pvals], [post_r, out],
                             accept_inplace=True)

        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)
        rng = np.random.RandomState(utt.fetch_seed())
        numpy_rng = np.random.RandomState(utt.fetch_seed())

        # Arguments of size (3,)
        rng0, val0 = f(rng, 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,)
        rng1, val1 = f(rng0, 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 = compile.function(
            [rng_R, n, pvals],
            multinomial(rng_R, n=n, pvals=pvals, size=(3, )),
            accept_inplace=True,
        )
        rng2, val2 = g(rng1, n_val, pvals_val)
        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(rng2, n_val[:-1], pvals_val[:-1])
Example #3
0
    def test_multinomial_tensor3_b(self):
        # Test the examples given in the multinomial documentation regarding
        # tensor3 objects
        rng_R = random_state_type()
        n = 9
        pvals = tensor.dtensor3()
        post_r, out = multinomial(rng_R, n=n, pvals=pvals, size=(10, 1, -1))
        assert out.ndim == 4
        assert out.broadcastable == (False, True, False, False)

        f = function([rng_R, pvals], [post_r, out], accept_inplace=True)

        rng = np.random.RandomState(utt.fetch_seed())

        pvals_val = np.asarray([[[0.1, 0.9], [0.2, 0.8], [0.3, 0.7]]])
        assert pvals_val.shape == (1, 3, 2)

        out_rng, draw = f(rng, pvals_val)
        assert draw.shape == (10, 1, 3, 2)
        assert np.allclose(draw.sum(axis=3), 9)
Example #4
0
    def test_default_shape(self):
        rng_R = random_state_type()
        post_r, out = uniform(rng_R)
        f = compile.function([rng_R], [post_r, out], accept_inplace=True)

        rng_state0 = np.random.RandomState(utt.fetch_seed())
        numpy_rng = np.random.RandomState(utt.fetch_seed())
        post0, val0 = f(rng_state0)
        post1, val1 = f(post0)
        numpy_val0 = np.asarray(numpy_rng.uniform(), dtype=theano.config.floatX)
        numpy_val1 = np.asarray(numpy_rng.uniform(), dtype=theano.config.floatX)

        assert np.all(val0 == numpy_val0)
        assert np.all(val1 == numpy_val1)

        post_r, out = multinomial(rng_R)
        g = compile.function([rng_R], [post_r, out], accept_inplace=True)
        post2, val2 = g(post1)
        numpy_val2 = np.asarray(
            numpy_rng.multinomial(n=1, pvals=[0.5, 0.5]), dtype=theano.config.floatX
        )

        assert np.all(val2 == numpy_val2)