Skip to content

Error in variable count when assigning sample method #2413

@ctm22396

Description

@ctm22396

I came across this error after a fresh install of python 3.5 and the latest versions of theano and pymc3. When I assign a step method to a variable, the resulting call to NUTS() as the remaining variables get auto-assigned generates the following error :

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-7-8f98cb09c8eb> in <module>()
      2     x = pm.Normal('x')
      3     y = pm.Normal('y')
----> 4     pm.sample(draws=10,tune=10, step=[pm.Metropolis(vars=[x])])
      5
~\Anaconda3\lib\site-packages\pymc3\sampling.py in sample(draws, step, init, n_init, start, trace, chain, njobs, tune, nuts_kwargs, step_kwargs, progressbar, model, random_seed, live_plot, discard_tuned_samples, live_plot_kwargs, **kwargs)
    247             start = start_
    248     else:
--> 249         step = assign_step_methods(model, step, step_kwargs=step_kwargs)
    250
    251     if njobs is None:

~\Anaconda3\lib\site-packages\pymc3\sampling.py in assign_step_methods(model, step, methods, step_kwargs)
     90         args = step_kwargs.get(step_class.name, {})
     91         used_keys.add(step_class.name)
---> 92         step = step_class(vars=vars, **args)
     93         steps.append(step)
     94

~\Anaconda3\lib\site-packages\pymc3\step_methods\hmc\nuts.py in __init__(self, vars, Emax, target_accept, gamma, k, t0, adapt_step_size, max_treedepth, on_error, **kwargs)
    147         `pm.sample` to the desired number of tuning steps.
    148         """
--> 149         super(NUTS, self).__init__(vars, use_single_leapfrog=True, **kwargs)
    150
    151         self.Emax = Emax

~\Anaconda3\lib\site-packages\pymc3\step_methods\hmc\base_hmc.py in __init__(self, vars, scaling, step_scale, is_cov, model, blocked, use_single_leapfrog, potential, integrator, **theano_kwargs)
     62
     63         self.H, self.compute_energy, self.compute_velocity, self.leapfrog, self.dlogp = get_theano_hamiltonian_functions(
---> 64             vars, shared, model.logpt, self.potential, use_single_leapfrog, integrator, **theano_kwargs)
     65
     66         super(BaseHMC, self).__init__(vars, shared, blocked=blocked)

~\Anaconda3\lib\site-packages\pymc3\step_methods\hmc\trajectory.py in get_theano_hamiltonian_functions(model_vars, shared, logpt, potential, use_single_leapfrog, integrator, **theano_kwargs)
    122     """
    123     H, q, dlogp = _theano_hamiltonian(model_vars, shared, logpt, potential)
--> 124     energy_function, p = _theano_energy_function(H, q, **theano_kwargs)
    125     velocity_function = _theano_velocity_function(H, p, **theano_kwargs)
    126     if use_single_leapfrog:

~\Anaconda3\lib\site-packages\pymc3\step_methods\hmc\trajectory.py in _theano_energy_function(H, q, **theano_kwargs)
     51     p = tt.vector('p')
     52     p.tag.test_value = q.tag.test_value
---> 53     total_energy = H.pot.energy(p) - H.logp(q)
     54     energy_function = theano.function(inputs=[q, p], outputs=total_energy, **theano_kwargs)
     55     energy_function.trust_input = True

~\Anaconda3\lib\site-packages\pymc3\step_methods\hmc\quadpotential.py in energy(self, x)
     96
     97     def energy(self, x):
---> 98         return .5 * x.dot(self.v * x)
     99
    100

~\Anaconda3\lib\site-packages\theano\tensor\var.py in __rmul__(self, other)
    231
    232     def __rmul__(self, other):
--> 233         return theano.tensor.basic.mul(other, self)

...

ValueError: Input dimension mis-match. (input[0].shape[0] = 2, input[1].shape[0] = 1)

The "..." stands in place of the traceback through theano, as I believe the issue here is in pymc3.

This occurs on the simplest of models that I created to reproduce the error. I initialized two variables and set one to Metropolis. You can see the code here:

import pymc3 as pm

with pm.Model() as m:
    x = pm.Normal('x')
    y = pm.Normal('y')
    pm.sample(step=[pm.Metropolis(vars=[x])])

It appears that the energy function is expecting a value with the shape of the number of variables in the full model but only getting the number variables assigned to NUTS.

It runs fine when auto-assigning NUTS to the full model though.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions