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(
        y0=y0,
        params=params,
        rhs=lotka_volterra,
        # The time points where we want to access the solution
        tvals=times,
        t0=times[0],
    )

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/as_theano.py in solve_ivp(t0, y0, params, tvals, rhs, derivatives, coords, make_solver, derivative_subset, solver_kwargs, simplify)
     81 
     82     y0_dims = read_dict(y0)
---> 83     params_dims = read_dict(params)
     84 
     85     if derivative_subset is None:

~/opt/miniconda3/lib/python3.9/site-packages/sunode/wrappers/as_theano.py 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/as_theano.py 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/as_theano.py 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/__init__.py in as_tensor_variable(x, name, ndim, **kwargs)
     39 
     40     """
---> 41     return _as_tensor_variable(x, name, ndim, **kwargs)
     42 
     43 

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

~/opt/miniconda3/lib/python3.9/site-packages/aesara/tensor/__init__.py 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.")
     49 
     50 

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

cheers

1 Like

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