CamDavidsonPilon / Probabilistic-Programming-and-Bayesian-Methods-for-Hackers

aka "Bayesian Methods for Hackers": An introduction to Bayesian methods + probabilistic programming with a computation/understanding-first, mathematics-second point of view. All in pure Python ;)
http://camdavidsonpilon.github.io/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers/
MIT License
26.67k stars 7.86k forks source link

Chapter1_Introduction use edward #349

Open cavities opened 7 years ago

cavities commented 7 years ago
import edward as ed
import tensorflow as tf
from edward.models import Exponential,Uniform,Poisson,Empirical
alpha_f = 1.0/count_data.mean()

alpha = tf.Variable(alpha_f, name="alpha", dtype=tf.float32)

# init 
lambda_1 = Exponential(alpha)
lambda_2 = Exponential(alpha)
tau = Uniform(low=0.0,high=float(n_count_data - 1))
idx = np.arange(n_count_data)
lambda_ = tf.where(tau>=idx,tf.ones(shape=[n_count_data,],dtype=tf.float32)*lambda_1,tf.ones(shape=[n_count_data,],dtype=tf.float32)*lambda_2)
z = Poisson(lambda_,value=tf.Variable(tf.ones(n_count_data)))

# model
T = 5000  # number of posterior samples
qlambda_1 =  Empirical(params=tf.Variable(tf.zeros([T,n_count_data,1])))
qlambda_2 =  Empirical(params=tf.Variable(tf.zeros([T,n_count_data,1])))
# qlambda_  =  Empirical(params=tf.Variable(tf.zeros([T,n_count_data,1])))

inference = ed.HMC({lambda_1:qlambda_1,lambda_2:qlambda_2},data={z:count_data})
inference.run()

has a type Error

i think the Poisson of edward is diff the Poisson in pymc3;

this error:

TypeError                                 Traceback (most recent call last)
<ipython-input-135-b332cfa2b61e> in <module>()
     20 
     21 
---> 22 inference = ed.HMC({lambda_1:qlambda_1,lambda_2:qlambda_2},data={z:count_data})
     23 inference.run()
     24 

c:\python35\lib\site-packages\edward\inferences\hmc.py in __init__(self, *args, **kwargs)
     48     >>> inference = ed.HMC({z: qz}, data)
     49     """
---> 50     super(HMC, self).__init__(*args, **kwargs)
     51 
     52   def initialize(self, step_size=0.25, n_steps=2, *args, **kwargs):

c:\python35\lib\site-packages\edward\inferences\monte_carlo.py in __init__(self, latent_vars, data)
     87                            "a scalar sample shape.")
     88 
---> 89     super(MonteCarlo, self).__init__(latent_vars, data)
     90 
     91   def initialize(self, *args, **kwargs):

c:\python35\lib\site-packages\edward\inferences\inference.py in __init__(self, latent_vars, data)
     70       data = {}
     71 
---> 72     check_latent_vars(latent_vars)
     73     self.latent_vars = latent_vars
     74 

c:\python35\lib\site-packages\edward\util\random_variables.py in check_latent_vars(latent_vars)
     74     elif not key.shape.is_compatible_with(value.shape):
     75       raise TypeError("Key-value pair in latent_vars does not have same "
---> 76                       "shape: {}, {}".format(key.shape, value.shape))
     77     elif key.dtype != value.dtype:
     78       raise TypeError("Key-value pair in latent_vars does not have same "

TypeError: Key-value pair in latent_vars does not have same shape: (), (74, 1)
petecog commented 6 years ago

could it be the def of tau? https://stackoverflow.com/questions/46942729/equivalent-of-theano-tensor-switch-in-tensorflow seems to suggest that if it's element wise (which I think it is in this case) then tf.cond is the equiv of theano.switch (== pm.math.switch (i think))