コード例 #1
0
def test_multinomial():
    R = RandomStreams(234)
    n = R.multinomial(5, numpy.ones(5, ) / 5, draw_shape=(2, ))

    f = theano.function([], n)

    assert f().shape == (2, 5)
コード例 #2
0
def test_dirichlet():
    R = RandomStreams(234)
    n = R.dirichlet(alpha=numpy.ones(10, ), draw_shape=(5, ))

    f = theano.function([], n)

    assert f().shape == (5, 10)
コード例 #3
0
 def setUp(self):
     s_rng = self.s_rng = RandomStreams(23424)
     p = 0.5
     self.A = s_rng.binomial(1, p)
     self.B = s_rng.binomial(1, p)
     self.C = s_rng.binomial(1, p)
     self.D = self.A + self.B + self.C
     self.condition = tensor.ge(self.D, 2)
コード例 #4
0
    def setUp(self):
        s_rng = self.s_rng = RandomStreams(23424)

        self.fair_prior = 0.999
        self.fair_coin = s_rng.binomial(1, self.fair_prior)

        make_coin = lambda x: s_rng.binomial((4, ), 1, x)
        self.coin = make_coin(tensor.switch(self.fair_coin > 0.5, 0.5, 0.95))

        self.data = tensor.as_tensor_variable([[1, 1, 1, 1]])
コード例 #5
0
    def setUp(self):
        R = RandomStreams(234)
        weights = tensor.dvector()
        mus = tensor.dvector()
        sigmas = tensor.dvector()
        draw_shape = tensor.ivector()
        xsca = R.GMM1(weights, mus, sigmas, draw_shape=draw_shape, ndim=0)
        xvec = R.GMM1(weights, mus, sigmas, draw_shape=draw_shape, ndim=1)
        xmat = R.GMM1(weights, mus, sigmas, draw_shape=draw_shape, ndim=2)

        self.__dict__.update(locals())
        del self.self
コード例 #6
0
def test_normal_nonscalar():
    s_rng = RandomStreams(234)
    n = s_rng.normal()

    data = numpy.asarray([1, 2, 3, 4, 5])
    p_data = rv.lpdf(n, data)

    f = theano.function([], [p_data])

    pvals = f()
    targets = numpy.log(numpy.exp(-0.5 * (data**2)) / numpy.sqrt(2 * numpy.pi))

    assert numpy.allclose(pvals, targets), (pvals, targets)
コード例 #7
0
def test_dag_condition_bottom():
    """
    Test test of conditioning an upper node on a lower one
    """
    with RandomStreams(234) as _:
        mu = normal(10, .1)
        x = normal(mu, sigma=1)

    post_mu = rv.condition([mu], {x: -7})
    theano.printing.debugprint(post_mu)

    f = theano.function([], post_mu)
    f()
コード例 #8
0
    def setUp(self):
        s_rng = self.s_rng = RandomStreams(23424)

        self.p = tensor.scalar()
        self.m1 = tensor.scalar()
        self.m2 = tensor.scalar()
        self.v = tensor.scalar()

        self.C = s_rng.binomial(1, p)
        self.m = tensor.switch(self.C, self.m1, self.m2)
        self.D = s_rng.normal(self.m, self.v)

        self.D_data = tensor.as_tensor_variable([1, 1.2, 3, 3.4])
コード例 #9
0
    def setUp(self):
        s_rng = self.s_rng = RandomStreams(23424)
        a = 0.0
        b = 1.0
        c = 1.5
        d = 2.0

        self.M = s_rng.normal(a, b)
        self.V = s_rng.normal(c, d)
        self.V_ = abs(self.V) + .1
        self.X = s_rng.normal((4, ), self.M, self.V_)

        self.X_data = tensor.as_tensor_variable([1, 2, 3, 2.4])
コード例 #10
0
def test_uniform_w_params():
    s_rng = RandomStreams(234)
    u = s_rng.uniform(low=-0.999, high=9.001)

    p0 = rv.lpdf(u, 0)
    p1 = rv.lpdf(u, 2)
    p05 = rv.lpdf(u, -1.5)
    pn1 = rv.lpdf(u, 10)

    f = theano.function([], [p0, p1, p05, pn1])

    pvals = f()
    targets = numpy.log(numpy.asarray([.1, .1, 0, 0]))
    assert numpy.allclose(pvals, targets), (pvals, targets)
コード例 #11
0
def test_dag_condition_top():
    """
    Easy test of conditioning
    """
    with RandomStreams(234) as _:
        mu = normal(10, .1)
        x = normal(mu, sigma=1)

    post_x = rv.condition([x], {mu: -7})
    theano.printing.debugprint(post_x)

    f = theano.function([], post_x)
    r = [f() for i in range(10)]
    assert numpy.allclose(numpy.mean(r), -7.4722755432)
コード例 #12
0
def test_uniform_simple():
    s_rng = RandomStreams(234)
    u = s_rng.uniform()

    p0 = rv.lpdf(u, 0)
    p1 = rv.lpdf(u, 1)
    p05 = rv.lpdf(u, 0.5)
    pn1 = rv.lpdf(u, -1)

    f = theano.function([], [p0, p1, p05, pn1])

    pvals = f()
    targets = numpy.log(numpy.asarray([1.0, 1.0, 1.0, 0.0]))

    assert numpy.allclose(pvals, targets), (pvals, targets)
コード例 #13
0
def test_normal_simple():
    s_rng = RandomStreams(23)
    n = s_rng.normal()

    p0 = rv.lpdf(n, 0)
    p1 = rv.lpdf(n, 1)
    pn1 = rv.lpdf(n, -1)

    f = theano.function([], [p0, p1, pn1])

    pvals = f()
    targets = numpy.asarray([
        numpy.log(1.0 / numpy.sqrt(2 * numpy.pi)),
        numpy.log(numpy.exp(-0.5) / numpy.sqrt(2 * numpy.pi)),
        numpy.log(numpy.exp(-0.5) / numpy.sqrt(2 * numpy.pi)),
    ])

    assert numpy.allclose(pvals, targets), (pvals, targets)
コード例 #14
0
def test_normal_w_params():
    s_rng = RandomStreams(23)
    n = s_rng.normal(mu=2, sigma=3)

    p0 = rv.lpdf(n, 0)
    p1 = rv.lpdf(n, 2)
    pn1 = rv.lpdf(n, -1)

    f = theano.function([], [p0, p1, pn1])

    pvals = f()
    targets = numpy.asarray([
        numpy.log(
            numpy.exp(-0.5 * ((2.0 / 3.0)**2)) /
            numpy.sqrt(2 * numpy.pi * 9.0)),
        numpy.log(numpy.exp(0) / numpy.sqrt(2 * numpy.pi * 9)),
        numpy.log(
            numpy.exp(-0.5 * ((3.0 / 3.0)**2)) /
            numpy.sqrt(2 * numpy.pi * 9.0)),
    ])

    assert numpy.allclose(pvals, targets), (pvals, targets)
コード例 #15
0
    def setUp(self):
        s_rng = self.s_rng = RandomStreams(23424)

        self.nr_states = 5
        self.nr_obs = 3

        self.observation_model = memoized(
            lambda state: s_rng.dirichlet([1] * self.nr_obs))
        self.transition_model = memoized(
            lambda state: s_rng.dirichlet([1] * self.nr_states))

        self.transition = lambda state: s_rng.multinomial(
            1, self.tranisition_model(state))
        self.observation = lambda state: s_rng.multinomial(
            1, self.observation_model(state))

        def transition(obs, state):
            return [self.observation(state),
                    self.transition(state)
                    ], {}, until(state == numpy.asarray([0, 0, 0, 0, 1]))

        [self.sampled_words, self.sampled_states], updates = scan([],
                                                                  [obs, state])
コード例 #16
0
 def setUp(self):
     s_rng = self.s_rng = RandomStreams(23424)
     self.weights = tensor.dvector()
     self.mus = tensor.dvector()
     self.sigmas = tensor.dvector()
コード例 #17
0
    def setUp(self):
        s_rng = self.s_rng = RandomStreams(23424)

        self.repetitions = 100
        self.coin_weight = s_rng.uniform(low=0, high=1)
        self.coin = s_rng.binomial((self.repetitions, ), 1, self.coin_weight)
コード例 #18
0
 def setUp(self):
     self.obs = tensor.as_tensor_variable(
         numpy.asarray([0.0, 1.01, 0.7, 0.65, 0.3]))
     self.rstream = RandomStreams(234)
     self.n = self.rstream.normal()
     self.u = self.rstream.uniform()
コード例 #19
0
import numpy
import theano
from theano import tensor
from rstreams import RandomStreams
import distributions
from sample import hybridmc_sample
from rv import full_log_likelihood

from max_lik import likelihood_gradient 

s_rng = RandomStreams(3424)

# Weight prior:
w = s_rng.normal(0, 2, draw_shape=(3,))

# Linear model:
x = tensor.matrix('x')
y = tensor.nnet.sigmoid(tensor.dot(x, w))

# Bernouilli observation model:
t = s_rng.binomial(p=y, draw_shape=(4,))

# Some data:
X_data = numpy.asarray([[-1.5, -0.4, 1.3, 2.2], [-1.1, -2.2, 1.3, 0], [1., 1., 1., 1.]], dtype=theano.config.floatX).T 
Y_data = numpy.asarray([1., 1., 0., 0.], dtype=theano.config.floatX)

# Compute gradient updates:
observations = dict([(t, Y_data)])
params, updates, log_likelihood = likelihood_gradient(observations)

# Compile training function and assign input data as givens:
コード例 #20
0
import numpy
import theano
from theano import tensor
from rstreams import RandomStreams
import distributions
from sample import mh2_sample, mh_sample
from for_theano import memoized, evaluate

s_rng = RandomStreams(123)

nr_words = 4
nr_topics = 2
alpha = 0.8
beta = 1.

# Topic distribution per document
doc_mixture = memoized(
    lambda doc_id: s_rng.dirichlet([alpha / nr_topics] * nr_topics))

# Word distribution per topic
topic_mixture = memoized(
    lambda top_id: s_rng.dirichlet([beta / nr_words] * nr_words))

# For each word in the document, draw a topic according to multinomial with document specific prior
# TODO, see comment below: topics = memoized(lambda doc_id, nr: s_rng.multinomial(1, doc_mixture[doc_id], draw_shape=(nr,)))
topics = memoized(lambda doc_id, nr: s_rng.binomial(
    1, doc_mixture(doc_id)[0], draw_shape=(nr, )))

# Draw words for a specific topic
word_topic = lambda top_id: s_rng.multinomial(1, topic_mixture(top_id))