Example #1
0
    def sampler(nr_samples, burnin=100, lag=100):
        data = [[] for o in outputs]
        for i in range(nr_samples * lag + burnin):
            accept = False
            while not accept:
                index = numpy.random.randint(len(free_RVs))

                accept = rr[index]()
                if accept and i > burnin and (i - burnin) % lag == 0:
                    for d, o in zip(data, outputs):
                        # TODO: this can be optimized
                        if is_raw_rv(o):
                            d.append(
                                free_RVs_state[free_RVs.index(o)].get_value())
                        else:
                            full_observations = dict(observations)
                            full_observations.update(
                                dict([
                                    (rv, s)
                                    for rv, s in zip(free_RVs, free_RVs_state)
                                ]))
                            d.append(
                                evaluate(evaluate_with_assignments(
                                    o, full_observations),
                                         givens=givens))
        data = [numpy.asarray(d).squeeze() for d in data]

        return data
Example #2
0
    def sampler(nr_samples, burnin = 100, lag = 100):
        data = [[] for o in outputs]
        for i in range(nr_samples*lag+burnin):        
            accept = False
            while not accept:
                index = numpy.random.randint(len(free_RVs))

                accept = rr[index]()            
                if accept and i > burnin and (i-burnin) % lag == 0:
                    for d, o in zip(data, outputs):
                        # TODO: this can be optimized
                        if is_raw_rv(o):
                            d.append(free_RVs_state[free_RVs.index(o)].get_value())
                        else:
                            full_observations = dict(observations)
                            full_observations.update(dict([(rv, s) for rv, s in zip(free_RVs, free_RVs_state)]))
                            d.append(evaluate(evaluate_with_assignments(o, full_observations), givens=givens))
        data = [numpy.asarray(d).squeeze() for d in data]
        
        return data
 def test(self):
     print evaluate(self.sample_words([1,0,0,0,0]))
Example #4
0
 def test(self):
     print evaluate(self.sample_words([1, 0, 0, 0, 0]))
import numpy, pylab
import theano
from theano import tensor
from rstreams import RandomStreams
import distributions
from sample import mh2_sample
from for_theano import evaluate
from rv import full_log_likelihood

s_rng = RandomStreams(23424)

fair_prior = 0.999

coin_weight = tensor.switch(
    s_rng.binomial(1, fair_prior) > 0.5, 0.5,
    s_rng.dirichlet([1, 1])[0])

make_coin = lambda p, size: s_rng.binomial(1, p, draw_shape=(size, ))
coin = lambda size: make_coin(coin_weight, size)

for size in [1, 3, 6, 10, 20, 30, 50, 70, 100]:
    data = evaluate(make_coin(0.9, size))

    sampler = mh2_sample(s_rng, [coin_weight], {coin(size): data})

    print "nr of examples", size, ", estimated probability", sampler(
        nr_samples=400, burnin=20000, lag=10)[0].mean()
import numpy, pylab
import theano
from theano import tensor
from rstreams import RandomStreams
import distributions
from sample import mh2_sample
from for_theano import evaluate
from rv import full_log_likelihood

s_rng = RandomStreams(23424)

fair_prior = 0.999

coin_weight = tensor.switch(s_rng.binomial(1, fair_prior) > 0.5, 0.5, s_rng.dirichlet([1, 1])[0])

make_coin = lambda p, size: s_rng.binomial(1, p, draw_shape=(size,))    
coin = lambda size: make_coin(coin_weight, size)
            
for size in [1, 3, 6, 10, 20, 30, 50, 70, 100]:
    data = evaluate(make_coin(0.9, size))
            
    sampler = mh2_sample(s_rng, [coin_weight], {coin(size) : data})            
    
    print "nr of examples", size, ", estimated probability", sampler(nr_samples=400, burnin=20000, lag=10)[0].mean()