Beispiel #1
0
def DM_sampler(rstream, alpha, draw_shape=None, ndim=None, dtype=None):
    shape = infer_shape(rstream.dirichlet(alpha, draw_shape=draw_shape))
    rstate = rstream.new_shared_rstate()
    op = DM(tensor.TensorType(broadcastable=(False,) * tensor.get_vector_length(shape), dtype=theano.config.floatX))
    rs, out = op(rstate, alpha)
    rstream.add_default_update(out, rstate, rs)
    return out
Beispiel #2
0
def DM_sampler(rstream, alpha, draw_shape=None, ndim=None, dtype=None):
    shape = infer_shape(rstream.dirichlet(alpha, draw_shape=draw_shape))
    rstate = rstream.new_shared_rstate()
    op = DM(
        tensor.TensorType(broadcastable=(False, ) *
                          tensor.get_vector_length(shape),
                          dtype=theano.config.floatX))
    rs, out = op(rstate, alpha)
    rstream.add_default_update(out, rstate, rs)
    return out
Beispiel #3
0
def normal_proposal(rstream, node, sample, kw):
    # TODO: how do we determine the variance?
    return rstream.normal(sample, 0.1, draw_shape=infer_shape(node.outputs[1]))
Beispiel #4
0
def hybridmc_sample(s_rng, outputs, observations = {}):
    # TODO: should there be a size variable here?
    # TODO: implement lag and burn-in
    # TODO: implement size
    """
    Return a dictionary mapping random variables to their sample values.
    """

    all_vars = ancestors(list(outputs) + list(observations.keys()))
    
    for o in observations:
        assert o in all_vars
        if not is_raw_rv(o):
            raise TypeError(o)

    RVs = [v for v in all_vars if is_raw_rv(v)]

    free_RVs = [v for v in RVs if v not in observations]
    
    free_RVs_state = [theano.shared(numpy.ones(shape=infer_shape(v)), broadcastable=tuple(numpy.asarray(infer_shape(v))==1)) for v in free_RVs]
    free_RVs_prop = [s_rng.normal(0, 1, draw_shape=infer_shape(v)) for v in free_RVs]
    
    log_likelihood = theano.shared(numpy.array(float('-inf')))
    
    U = s_rng.uniform(low=0, high=1.0)
    
    epsilon = numpy.sqrt(2*0.03)
    def mcmc(ll, *frvs):
        full_observations = dict(observations)
        full_observations.update(dict([(rv, s) for rv, s in zip(free_RVs, frvs)]))
        
        loglik = -full_log_likelihood(full_observations)

        proposals = free_RVs_prop
        H = tensor.add(*[tensor.sum(tensor.sqr(p)) for p in proposals])/2. + loglik

# -- this should be an inner loop
        g = []
        g.append(tensor.grad(loglik, frvs))
        
        proposals = [(p - epsilon*gg[0]/2.) for p, gg in zip(proposals, g)]

        rvsp = [(rvs + epsilon*rvp) for rvs,rvp in zip(frvs, proposals)]
        
        full_observations = dict(observations)
        full_observations.update(dict([(rv, s) for rv, s in zip(free_RVs, rvsp)]))
        new_loglik = -full_log_likelihood(full_observations)
        
        gnew = []
        gnew.append(tensor.grad(new_loglik, rvsp))
        proposals = [(p - epsilon*gn[0]/2.) for p, gn in zip(proposals, gnew)]
# --
        
        Hnew = tensor.add(*[tensor.sum(tensor.sqr(p)) for p in proposals])/2. + new_loglik

        dH = Hnew - H
        accept = tensor.or_(dH < 0., U < tensor.exp(-dH))

        return [tensor.switch(accept, -new_loglik, ll)] + \
            [tensor.switch(accept, p, f) for p, f in zip(rvsp, frvs)], \
            {}, theano.scan_module.until(accept)

    samples, updates = theano.scan(mcmc, outputs_info = [log_likelihood] + free_RVs_state, n_steps = 10000000)
    
    updates[log_likelihood] = samples[0][-1]
    updates.update(dict([(f, s[-1]) for f, s in zip(free_RVs_state, samples[1:])]))
    
    return [free_RVs_state[free_RVs.index(out)] for out in outputs], log_likelihood, updates
Beispiel #5
0
def hybridmc_sample(s_rng, outputs, observations={}):
    # TODO: should there be a size variable here?
    # TODO: implement lag and burn-in
    # TODO: implement size
    """
    Return a dictionary mapping random variables to their sample values.
    """

    all_vars = ancestors(list(outputs) + list(observations.keys()))

    for o in observations:
        assert o in all_vars
        if not is_raw_rv(o):
            raise TypeError(o)

    RVs = [v for v in all_vars if is_raw_rv(v)]

    free_RVs = [v for v in RVs if v not in observations]

    free_RVs_state = [
        theano.shared(numpy.ones(shape=infer_shape(v)),
                      broadcastable=tuple(numpy.asarray(infer_shape(v)) == 1))
        for v in free_RVs
    ]
    free_RVs_prop = [
        s_rng.normal(0, 1, draw_shape=infer_shape(v)) for v in free_RVs
    ]

    log_likelihood = theano.shared(numpy.array(float('-inf')))

    U = s_rng.uniform(low=0, high=1.0)

    epsilon = numpy.sqrt(2 * 0.03)

    def mcmc(ll, *frvs):
        full_observations = dict(observations)
        full_observations.update(
            dict([(rv, s) for rv, s in zip(free_RVs, frvs)]))

        loglik = -full_log_likelihood(full_observations)

        proposals = free_RVs_prop
        H = tensor.add(*[tensor.sum(tensor.sqr(p))
                         for p in proposals]) / 2. + loglik

        # -- this should be an inner loop
        g = []
        g.append(tensor.grad(loglik, frvs))

        proposals = [(p - epsilon * gg[0] / 2.) for p, gg in zip(proposals, g)]

        rvsp = [(rvs + epsilon * rvp) for rvs, rvp in zip(frvs, proposals)]

        full_observations = dict(observations)
        full_observations.update(
            dict([(rv, s) for rv, s in zip(free_RVs, rvsp)]))
        new_loglik = -full_log_likelihood(full_observations)

        gnew = []
        gnew.append(tensor.grad(new_loglik, rvsp))
        proposals = [(p - epsilon * gn[0] / 2.)
                     for p, gn in zip(proposals, gnew)]
        # --

        Hnew = tensor.add(*[tensor.sum(tensor.sqr(p))
                            for p in proposals]) / 2. + new_loglik

        dH = Hnew - H
        accept = tensor.or_(dH < 0., U < tensor.exp(-dH))

        return [tensor.switch(accept, -new_loglik, ll)] + \
            [tensor.switch(accept, p, f) for p, f in zip(rvsp, frvs)], \
            {}, theano.scan_module.until(accept)

    samples, updates = theano.scan(mcmc,
                                   outputs_info=[log_likelihood] +
                                   free_RVs_state,
                                   n_steps=10000000)

    updates[log_likelihood] = samples[0][-1]
    updates.update(
        dict([(f, s[-1]) for f, s in zip(free_RVs_state, samples[1:])]))

    return [free_RVs_state[free_RVs.index(out)]
            for out in outputs], log_likelihood, updates
Beispiel #6
0
def test_infer_shape_const():
    shp = infer_shape(tensor.alloc(0, 5, 6, 7))
    print shp
    assert  shp == (5, 6, 7)
Beispiel #7
0
def test_shape_vector_rv_dirichlet_rstreams():
    R = rstreams.RandomStreams(234)
    n = R.dirichlet(alpha=numpy.ones(10,), draw_shape=(10,))
    assert infer_shape(n) == (10,), infer_shape(n)
Beispiel #8
0
def test_shape_vector_rv_rstreams():
    R = rstreams.RandomStreams(234)
    n = R.normal(mu=numpy.zeros(10,), sigma=numpy.ones(10,), draw_shape=(10,))
    assert infer_shape(n) == (10,)
Beispiel #9
0
def test_shape_scalar_rv_w_size_rstreams():
    R = rstreams.RandomStreams(234)
    n = R.normal(mu=0, sigma=1.0, draw_shape=(40,20))
    
    assert infer_shape(n) == (40, 20)
Beispiel #10
0
def test_shape_scalar_rv_w_size():
    R = tensor.shared_randomstreams.RandomStreams(234)
    n = R.normal(avg=0, std=1.0, size=(40,20))
    assert infer_shape(n) == (40, 20)
Beispiel #11
0
def test_shape_rv():
    R = tensor.shared_randomstreams.RandomStreams(234)
    n = R.normal(avg=0, std=1.0)
    assert infer_shape(n) == ()
Beispiel #12
0
def test_shape_infer_shape():
    sv = theano.shared(numpy.asarray([2,3,5]))
    assert infer_shape(sv.shape) == (1,)
Beispiel #13
0
def test_infer_shape_shared_var():
    sv = theano.shared(numpy.asarray([2,3,5]))
    assert infer_shape(sv) == (3,)
    assert infer_shape(sv * 2 + 75) == (3,)
Beispiel #14
0
def normal_proposal(rstream, node, sample, kw):
    # TODO: how do we determine the variance?
    return rstream.normal(sample, 0.1, draw_shape=infer_shape(node.outputs[1]))