Delosari
Delosari

Reputation: 693

Declaring theano variables for pymc3

I am having issues replicating a pymc2 code using pymc3.

I believe it is due to the fact pymc3 is using the theano type variables which are not compatible with the numpy operations I am using. So I am using the @theano.decorator:

I have this function:

with pymc3.Model() as model:

    z_stars         = pymc3.Uniform('z_star',    self.z_min_ssp_limit, self.z_max_ssp_limit)
    Av_stars        = pymc3.Uniform('Av_star',   0.0, 5.00)
    sigma_stars     = pymc3.Uniform('sigma_star',0.0, 5.0)

    #Fit observational wavelength
    ssp_fit_output = self.ssp_fit_theano(z_stars, Av_stars, sigma_stars, 
                                         self.obj_data['obs_wave_resam'], 
                                         self.obj_data['obs_flux_norm_masked'], 
                                         self.obj_data['basesWave_resam'], 
                                         self.obj_data['bases_flux_norm'], 
                                         self.obj_data['int_mask'], 
                                         self.obj_data['normFlux_obs'])

    #Define likelihood
    like = pymc.Normal('ChiSq', mu=ssp_fit_output, 
                       sd=self.obj_data['obs_fluxEr_norm'], 
                       observed=self.obj_data['obs_fluxEr_norm'])

    #Run the sampler
    trace = pymc3.sample(iterations, step=step, start=start_conditions, trace=db)

where:

@theano.compile.ops.as_op(itypes=[t.dscalar,t.dscalar,t.dscalar,t.dvector,
                                  t.dvector,t.dvector,t.dvector,t.dvector,t.dscalar],
                          otypes=[t.dvector])
def ssp_fit_theano(self, input_z, input_sigma, input_Av, obs_wave, obs_flux_masked, 
                   rest_wave, bases_flux, int_mask, obsFlux_mean):
   ...
   ...

The first three variables are scalars (from the pymc3 uniform distribution). The remaining variables are numpy arrays and the last one is a float. However, I am getting this "'numpy.ndarray' object has no attribute 'type'" error:

  File "/home/user/anaconda/lib/python2.7/site-packages/theano/gof/op.py", line 615, in __call__
    node = self.make_node(*inputs, **kwargs)
  File "/home/user/anaconda/lib/python2.7/site-packages/theano/gof/op.py", line 963, in make_node
    if not all(inp.type == it for inp, it in zip(inputs, self.itypes)):
  File "/home/user/anaconda/lib/python2.7/site-packages/theano/gof/op.py", line 963, in <genexpr>
    if not all(inp.type == it for inp, it in zip(inputs, self.itypes)):
AttributeError: 'numpy.ndarray' object has no attribute 'type'

Please any advice in the right direction will be most welcomed.

Upvotes: 3

Views: 686

Answers (1)

St&#233;phane
St&#233;phane

Reputation: 1344

I had a bunch of time-wasting-stops when I went from pymc2 to pymc3. The problem, I think, is that the doc is quite bad. I suspect they neglect the doc as far as the code is still evolving. 3 comments/advises:

Upvotes: 3

Related Questions