def poly_expansion(x, order):
    x = x.T
    result, updates = theano.scan(fn=lambda prior_result, x: prior_result * x,
                                  outputs_info=tensor.ones_like(x),
                                  non_sequences=x,
                                  n_steps=order)

    return tensor.concatenate([
        tensor.ones([x.shape[1], 1]),
        tensor.reshape(result.T, (x.shape[1], x.shape[0] * order))
    ],
                              axis=1)


# Define priors to be inverse gamma distributions
alpha = 1 / s_rng.gamma(1., 2.)
beta = 1 / s_rng.gamma(1., .1)

# Order of the model
# TODO: this currently has to be fixed, would be nice if this could also be a RV!
m = 7  #s_rng.random_integers(1, 10)
w = s_rng.normal(0, beta, draw_shape=(m + 1, ))

# Input variable used for training
x = tensor.matrix('x')
# Input variable used for testing
xn = tensor.matrix('xn')

# Actual linear model
y = lambda x_in: tensor.dot(poly_expansion(x_in, m), w)
from rv import full_log_likelihood
from for_theano import evaluate

s_rng = RandomStreams(3424)

def poly_expansion(x, order):
	x = x.T
	result, updates = theano.scan(fn=lambda prior_result, x: prior_result * x,
			outputs_info=tensor.ones_like(x),
			non_sequences=x,
			n_steps=order)
			
	return tensor.concatenate([tensor.ones([x.shape[1],1]), tensor.reshape(result.T, (x.shape[1], x.shape[0]*order))], axis=1)

# Define priors to be inverse gamma distributions
alpha = 1/s_rng.gamma(1., 2.)
beta = 1/s_rng.gamma(1., .1)

# Order of the model
# TODO: this currently has to be fixed, would be nice if this could also be a RV!
m = 7 #s_rng.random_integers(1, 10)
w = s_rng.normal(0, beta, draw_shape=(m+1,))

# Input variable used for training
x = tensor.matrix('x')
# Input variable used for testing
xn = tensor.matrix('xn')

# Actual linear model
y = lambda x_in: tensor.dot(poly_expansion(x_in, m), w)
import numpy, pylab
import theano
from rstreams import RandomStreams
import distributions
from sample import mh2_sample
from for_theano import memoized

s_rng = RandomStreams(23424)

phi = s_rng.dirichlet(numpy.asarray([1, 1, 1, 1, 1]))
alpha = s_rng.gamma(2., 2.)        
prototype = phi*alpha

bag_prototype =  memoized(lambda bag: s_rng.dirichlet(prototype))
draw_marbles = lambda bag, nr: s_rng.multinomial(1, bag_prototype(bag), draw_shape=(nr,))

marbles_bag_1 = numpy.asarray([[1,1,1,1,1,1],
                               [0,0,0,0,0,0],
                               [0,0,0,0,0,0],
                               [0,0,0,0,0,0],
                               [0,0,0,0,0,0]], dtype=theano.config.floatX).T                                
marbles_bag_2 = numpy.asarray([[0,0,0,0,0,0],
                               [1,1,1,1,1,1],
                               [0,0,0,0,0,0],
                               [0,0,0,0,0,0],
                               [0,0,0,0,0,0]], dtype=theano.config.floatX).T 
marbles_bag_3 = numpy.asarray([[0,0,0,0,0,0],
                               [0,0,0,0,0,0],
                               [0,0,0,0,0,0],
                               [1,1,1,1,1,1],
                               [0,0,0,0,0,0]], dtype=theano.config.floatX).T 
예제 #4
0
            
sampler = mh2_sample(s_rng, [draw_marbles(4,1)], givens)            

samples = sampler(200, 100, 100)
data = samples[0]

# Show histogram
pylab.subplot(211)
pylab.bar(range(5), data.sum(axis=0))
pylab.title("Flat model")



# Define hierarchical model
phi = s_rng.dirichlet(numpy.asarray([1, 1, 1, 1, 1]))
alpha = s_rng.gamma(2., 2.)        
prototype = phi*alpha

bag_prototype =  memoized(lambda bag: s_rng.dirichlet(prototype))
draw_marbles = lambda bag, nr: s_rng.multinomial(1, bag_prototype(bag), draw_shape=(nr,))

# Generate samples from the model
givens = {draw_marbles(1,6): marbles_bag_1,
            draw_marbles(2,6): marbles_bag_2,
            draw_marbles(3,6): marbles_bag_3,
            draw_marbles(4,1): marbles_bag_4}
            
sampler = mh2_sample(s_rng, [draw_marbles(4,1)], givens)            

samples = sampler(200, 100, 100)
data = samples[0]