Getting SunODE to work

Hi everyone,

I’m trying to get the SunODE package to work with PyMC3.
I’m currently following the Lotka Volterra example given here: Quickstart with PyMC3 — sunode documentation.

However, at the following part, when assigning random parameters to tensor objects, I’m getting a NotImplementedError by Theano/Aesara.

Does anyone know how I could best deal with this?
I’m running Python 3.9.1 on Mac OS Big Sur, with PyMC3 3.8 , SunODE version 0.2, Aesara version 2.0.10, and Theano-PyMC version 1.1.2. I believe I don’t have Theano installed.

with model:
    from sunode.wrappers.as_theano import solve_ivp
    solution, *_ = solve_ivp(
        # The time points where we want to access the solution

For reference, here’s the complete error.

NotImplementedError                       Traceback (most recent call last)
<ipython-input-48-cb2e836fc4d6> in <module>
      1 with model:
      2     from sunode.wrappers.as_theano import solve_ivp
----> 3     solution, *_ = solve_ivp(
      4         y0=y0,
      5         params=params,

~/opt/miniconda3/lib/python3.9/site-packages/sunode/wrappers/ in solve_ivp(t0, y0, params, tvals, rhs, derivatives, coords, make_solver, derivative_subset, solver_kwargs, simplify)
     82     y0_dims = read_dict(y0)
---> 83     params_dims = read_dict(params)
     85     if derivative_subset is None:

~/opt/miniconda3/lib/python3.9/site-packages/sunode/wrappers/ in read_dict(vals, name)
     53     def read_dict(vals, name=None):
     54         if isinstance(vals, dict):
---> 55             return {name: read_dict(item, name) for name, item in vals.items()}
     56         else:
     57             if isinstance(vals, tuple):

~/opt/miniconda3/lib/python3.9/site-packages/sunode/wrappers/ in <dictcomp>(.0)
     53     def read_dict(vals, name=None):
     54         if isinstance(vals, dict):
---> 55             return {name: read_dict(item, name) for name, item in vals.items()}
     56         else:
     57             if isinstance(vals, tuple):

~/opt/miniconda3/lib/python3.9/site-packages/sunode/wrappers/ in read_dict(vals, name)
     66             if isinstance(dim_names, (str, int)):
     67                 dim_names = (dim_names,)
---> 68             tensor = aet.as_tensor_variable(tensor)
     69             if tensor.ndim != len(dim_names):
     70                 raise ValueError(

~/opt/miniconda3/lib/python3.9/site-packages/aesara/tensor/ in as_tensor_variable(x, name, ndim, **kwargs)
     40     """
---> 41     return _as_tensor_variable(x, name, ndim, **kwargs)

~/opt/miniconda3/lib/python3.9/ in wrapper(*args, **kw)
    875                             '1 positional argument')
--> 877         return dispatch(args[0].__class__)(*args, **kw)
    879     funcname = getattr(func, '__name__', 'singledispatch function')

~/opt/miniconda3/lib/python3.9/site-packages/aesara/tensor/ in _as_tensor_variable(x, name, ndim, **kwargs)
     46     x, name: Optional[str], ndim: Optional[int], **kwargs
     47 ) -> NoReturn:
---> 48     raise NotImplementedError(f"Cannot convert {x} to a tensor variable.")

NotImplementedError: Cannot convert alpha ~ Normal to a tensor variable.

with model:
    # We can access the individual variables of the solution using the
    # variable names.
    pm.Deterministic('hares_mu', solution['hares'])
    pm.Deterministic('lynxes_mu', solution['lynxes'])

    sd = pm.HalfNormal('sd')
    pm.Lognormal('hares', mu=solution['hares'], sd=sd, observed=hare_data)
    pm.Lognormal('lynxes', mu=solution['lynxes'], sd=sd, observed=lynx

Hi @yunus,

the error indicates that the alpha variable from the model was not identified as a Tensor. This is because your installation mixes incompatible versions of PyMC3, Aesara, Theano-PyMC and sunode.

:point_right: PyMC3 3.8 is too old to begin with. Try the current version 3.11.2

:point_right: Because of major refactorings in both PyMC3 and its backend Theano-PyMC/Aesara we have pinned the backend version.

:point_right: Aesara and Theano-PyMC can’t be mixed together. PyMC3 version 3.x needs Theano-PyMC.

For reference: Installation Guide (MacOS) · pymc-devs/pymc3 Wiki · GitHub



Thanks @michaelosthege , I installed it like this because I first had issues loading pymc3 at all after initially installing it according to the installation guide but a reinstall and removing Aesara, the error is gone :).

1 Like

Hi @michaelosthege, or anyone else, sorry to bother you again but I’m installing pymc3 with sunode on a different computer now and I’ve run into a different issue. It seems like sunode is now asking for Aesara, which I do not have installed currently. Is this a common issue? Has anything changed since June?

ModuleNotFoundError                       Traceback (most recent call last)
~/Anaconda/anaconda3/lib/python3.7/site-packages/sunode/wrappers/ in <module>
      1 try:
----> 2     import aesara
      3     import aesara.tensor as aet

ModuleNotFoundError: No module named 'aesara'

During handling of the above exception, another exception occurred:

ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-2-651ebd86326b> in <module>
      9 theano.config.compute_test_value = 'ignore'
     10 from scipy.integrate import odeint
---> 11 from sunode.wrappers.as_theano import solve_ivp
     12 SEED=2021
     13 import jax

~/Anaconda/anaconda3/lib/python3.7/site-packages/sunode/wrappers/ in <module>
----> 1 from . import as_theano as as_aesara
      2 from . import as_theano
      4 __all__ = ['as_theano', 'as_aesara']

~/Anaconda/anaconda3/lib/python3.7/site-packages/sunode/wrappers/ in <module>
     10     if hasattr(theano, "gof"):
     11         from theano.gof.fg import MissingInputError
---> 12         from theano.gof.var import Constant, Variable
     13         from theano.gof.op import Op
     14     else:

ModuleNotFoundError: No module named 'theano.gof.var'

Anyone familiar with the above issue?

Try installing aesara instead of theano.

Sunode has changed in the meantime. I am not up to date with the current developments there, but if its unclear if the current release should still be backwards-compatible, or just support Aesara moving forward, please ask the question an issue in the sunode project.
Maybe it was unintentional.

Hey @yunus

I was facing the same issue as you until I had questioned someone who succeeded in using it recently (i.e with pymc3 version > 3.11.x). The main difference I realized was a different version on Sunode, a little older. When I downgraded to sunode==0.2.1 (along with pymc3==3.11.4 specifically), this error completely vanished. I hope this might help you!

1 Like

I am following the same example posted in the SunODE documentation page for a “Quickstart with Pymc3”.

In particular, I am running the example code on MacOs 12.3 with:

  • numpy 1.21.2
  • sunode 0.2.1
  • pymc3 3.11.4

In my case, everything works just fine until I sample the model

with model:
    trace = pm.sample()

I got

NameError                                 Traceback (most recent call last)
Input In [8], in <cell line: 1>()
      1 with model:
----> 2     trace = pm.sample()

File /opt/anaconda3/envs/vh/lib/python3.9/site-packages/sunode/wrappers/, in SolveODEAdjoint.grad(self, inputs, g)
    258 backward = SolveODEAdjointBackward(self._solver, self._t0, self._tvals)
    259 lamda, gradient = backward(y0, params, params_fixed, g)
--> 260 return [-lamda, gradient, aesara.gradient.grad_not_implemented(self, 2, params_fixed)]

NameError: name 'aesara' is not defined

Can anyone help? Why am I getting this error instead (for example) of a ModuleNotFoundError ?

That’s an import error inside the sunode module.

The imports at the beginning fall back to Theano, but they don’t create an alias like import theano as aesara.

If you want to continue using v3.x, that will require a Bugfix in sunode. Otherwise you could update to a PyMC v4 beta.

If you can spare the time, it would be great to upgrade the sunode CI pipelines to test against pymc==4.0.0b6
Correction, sunode doesn’t even need to test against PyMC. But the last commit was in November, so there’s at least the need to a trigger the CI with something…