3

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.

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
Delosari
  • 677
  • 2
  • 17
  • 29
  • Just based on the name of the variables, shouldn't the call to `ssp_fit_theano` be `self.ssp_fit_theano(z_stars, sigma_stars, Av_stars, ...)`? (The second and third arguments appear to be out of place compared to `ssp_fit_theano`'s call signature). – unutbu Sep 29 '17 at 00:51

1 Answers1

3

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:

Stéphane
  • 1,389
  • 3
  • 13
  • 34
  • Thank you very much for your reply (and your understanding). I have been learning that the issue is much complex than this. Using pymc2 I had always managed to combine its variables with the 3rd party python libraries without issues. However, it is not so straight forward now. They must have their reasons, of course, but yes the documentation should have more examples. – Delosari Nov 14 '17 at 18:48