pymc-devs / pymc-examples

Examples of PyMC models, including a library of Jupyter notebooks.
https://www.pymc.io/projects/examples/en/latest/
MIT License
292 stars 251 forks source link

Error in `examples/samplers/SMC-ABC_Lotka-Volterra_example.ipynb` #732

Open wd60622 opened 1 week ago

wd60622 commented 1 week ago
────────────────────────────────────── Error running examples/samplers/SMC-ABC_Lotka-Volterra_example.ipynb ──────────────────────────────────────

---------------------------------------------------------------------------
Exception encountered at "In [12]":
---------------------------------------------------------------------------
_RemoteTraceback                          Traceback (most recent call last)
_RemoteTraceback:
"""
Traceback (most recent call last):
  File "/home/wdean/micromamba/envs/pymc-examples/lib/python3.11/site-packages/pytensor/compile/function/types.py", line 959, in __call__
    self.vm()
  File "/home/wdean/micromamba/envs/pymc-examples/lib/python3.11/site-packages/pytensor/graph/op.py", line 524, in rval
    r = p(n, [x[0] for x in i], o)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/wdean/micromamba/envs/pymc-examples/lib/python3.11/site-packages/pytensor/tensor/random/op.py", line 403, in perform
    smpl_val = self.rng_fn(rng, *([*args, size]))
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/wdean/micromamba/envs/pymc-examples/lib/python3.11/site-packages/pymc/distributions/simulator.py", line 53, in rng_fn
    return cls.fn(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/ipykernel_43958/1881729530.py", line 27, in competition_model
    return odeint(dX_dt, y0=X0, t=t, rtol=0.01, args=(a, b, c, d))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/wdean/micromamba/envs/pymc-examples/lib/python3.11/site-packages/scipy/integrate/_odepack_py.py", line 243, in odeint
    output = _odepack.odeint(func, y0, t, args, Dfun, col_deriv, ml, mu,
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: The array return by func must be one-dimensional, but got ndim=3.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/wdean/micromamba/envs/pymc-examples/lib/python3.11/concurrent/futures/process.py", line 261, in _process_worker
    r = call_item.fn(*call_item.args, **call_item.kwargs)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/wdean/micromamba/envs/pymc-examples/lib/python3.11/site-packages/pymc/smc/sampling.py", line 303, in _sample_smc_int
    smc._initialize_kernel()
  File "/home/wdean/micromamba/envs/pymc-examples/lib/python3.11/site-packages/pymc/smc/kernels.py", line 249, in _initialize_kernel
    likelihoods =
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/wdean/micromamba/envs/pymc-examples/lib/python3.11/site-packages/pymc/smc/kernels.py", line 249, in <listcomp>
    likelihoods =
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/wdean/micromamba/envs/pymc-examples/lib/python3.11/site-packages/pytensor/compile/function/types.py", line 972, in __call__
    raise_with_op(
  File "/home/wdean/micromamba/envs/pymc-examples/lib/python3.11/site-packages/pytensor/link/utils.py", line 524, in raise_with_op
    raise exc_value.with_traceback(exc_trace)
  File "/home/wdean/micromamba/envs/pymc-examples/lib/python3.11/site-packages/pytensor/compile/function/types.py", line 959, in __call__
    self.vm()
  File "/home/wdean/micromamba/envs/pymc-examples/lib/python3.11/site-packages/pytensor/graph/op.py", line 524, in rval
    r = p(n, [x[0] for x in i], o)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/wdean/micromamba/envs/pymc-examples/lib/python3.11/site-packages/pytensor/tensor/random/op.py", line 403, in perform
    smpl_val = self.rng_fn(rng, *([*args, size]))
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/wdean/micromamba/envs/pymc-examples/lib/python3.11/site-packages/pymc/distributions/simulator.py", line 53, in rng_fn
    return cls.fn(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/ipykernel_43958/1881729530.py", line 27, in competition_model
    return odeint(dX_dt, y0=X0, t=t, rtol=0.01, args=(a, b, c, d))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/wdean/micromamba/envs/pymc-examples/lib/python3.11/site-packages/scipy/integrate/_odepack_py.py", line 243, in odeint
    output = _odepack.odeint(func, y0, t, args, Dfun, col_deriv, ml, mu,
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: The array return by func must be one-dimensional, but got ndim=3.
Apply node that caused the error: Simulator_sim_rv{"(),()->()"}(simulator_rng, [100   2], Exp.0, Exp.0)
Toposort index: 8
Inputs types: [RandomGeneratorType, TensorType(int64, shape=(2,)), TensorType(float64, shape=(1, 1)), TensorType(float64, shape=(1, 1))]
Inputs shapes: ['No shapes', (2,), (1, 1), (1, 1)]
Inputs strides: ['No strides', (8,), (8, 8), (8, 8)]
Inputs values: [Generator(PCG64) at 0x7F6744184C80, array([100,   2]), array([[0.74738862]]), array([[0.78745484]])]
Outputs clients: [[output[1](Simulator_sim_rv{"(),()->()"}.0)], [Composite{sqr((0.1 * (i0 - i1)))}(sim{[[ 9.52728 ... 47175637]]},
Simulator_sim_rv{"(),()->()"}.out)]]

Backtrace when the node is created (use PyTensor flag traceback__limit=N to make it longer):
  File "/home/wdean/micromamba/envs/pymc-examples/lib/python3.11/multiprocessing/process.py", line 314, in _bootstrap
    self.run()
  File "/home/wdean/micromamba/envs/pymc-examples/lib/python3.11/multiprocessing/process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "/home/wdean/micromamba/envs/pymc-examples/lib/python3.11/concurrent/futures/process.py", line 261, in _process_worker
    r = call_item.fn(*call_item.args, **call_item.kwargs)
  File "/home/wdean/micromamba/envs/pymc-examples/lib/python3.11/site-packages/pymc/smc/sampling.py", line 303, in _sample_smc_int
    smc._initialize_kernel()
  File "/home/wdean/micromamba/envs/pymc-examples/lib/python3.11/site-packages/pymc/smc/kernels.py", line 244, in _initialize_kernel
    self.likelihood_logp_func = _logp_forw(
  File "/home/wdean/micromamba/envs/pymc-examples/lib/python3.11/site-packages/pymc/smc/kernels.py", line 636, in _logp_forw
    out_list, inarray0 = join_nonshared_inputs(
  File "/home/wdean/micromamba/envs/pymc-examples/lib/python3.11/site-packages/pymc/pytensorf.py", line 619, in join_nonshared_inputs
    new_outputs = [
  File "/home/wdean/micromamba/envs/pymc-examples/lib/python3.11/site-packages/pymc/pytensorf.py", line 620, in <listcomp>
    pytensor.clone_replace(output, replace, rebuild_strict=False) for output in outputs

HINT: Use the PyTensor flag `exception_verbosity=high` for a debug print-out and storage map footprint of this Apply node.
"""

The above exception was the direct cause of the following exception:

RuntimeError                              Traceback (most recent call last)
Cell In[12], line 7
      3 b = pm.HalfNormal("b", 1.0)
      5 sim = pm.Simulator("sim", competition_model, params=(a, b), epsilon=10, observed=observed)
----> 7 idata_lv = pm.sample_smc()

File ~/micromamba/envs/pymc-examples/lib/python3.11/site-packages/pymc/smc/sampling.py:180, in sample_smc(draws, kernel, start, model,
random_seed, chains, cores, compute_convergence_checks, return_inferencedata, idata_kwargs, progressbar, **kernel_kwargs)
    171 params = (
    172     draws,
    173     kernel,
    174     start,
    175     model,
    176 )
    178 t1 = time.time()
--> 180 results = run_chains(chains, progressbar, params, random_seed, kernel_kwargs, cores)
    182 (
    183     traces,
    184     sample_stats,
    185     sample_settings,
    186 ) = zip(*results)
    188 trace = MultiTrace(traces)

File ~/micromamba/envs/pymc-examples/lib/python3.11/site-packages/pymc/smc/sampling.py:385, in run_chains(chains, progressbar, params,
random_seed, kernel_kwargs, cores)
    378                 # update the progress bar for this task:
    379                 progress.update(
    380                     status=f"Stage: {stage} Beta: {beta:.3f}",
    381                     task_id=task_id,
    382                     refresh=True,
    383                 )
--> 385 return tuple(cloudpickle.loads(r.result()) for r in done)

File ~/micromamba/envs/pymc-examples/lib/python3.11/site-packages/pymc/smc/sampling.py:385, in <genexpr>(.0)
    378                 # update the progress bar for this task:
    379                 progress.update(
    380                     status=f"Stage: {stage} Beta: {beta:.3f}",
    381                     task_id=task_id,
    382                     refresh=True,
    383                 )
--> 385 return tuple(cloudpickle.loads(r.result()) for r in done)

File ~/micromamba/envs/pymc-examples/lib/python3.11/concurrent/futures/_base.py:449, in Future.result(self, timeout)
    447     raise CancelledError()
    448 elif self._state == FINISHED:
--> 449     return self.__get_result()
    451 self._condition.wait(timeout)
    453 if self._state in [CANCELLED, CANCELLED_AND_NOTIFIED]:

File ~/micromamba/envs/pymc-examples/lib/python3.11/concurrent/futures/_base.py:401, in Future.__get_result(self)
    399 if self._exception:
    400     try:
--> 401         raise self._exception
    402     finally:
    403         # Break a reference cycle with the exception in self._exception
    404         self = None

RuntimeError: The array return by func must be one-dimensional, but got ndim=3.
Apply node that caused the error: Simulator_sim_rv{"(),()->()"}(simulator_rng, [100   2], Exp.0, Exp.0)
Toposort index: 8
Inputs types: [RandomGeneratorType, TensorType(int64, shape=(2,)), TensorType(float64, shape=(1, 1)), TensorType(float64, shape=(1, 1))]
Inputs shapes: ['No shapes', (2,), (1, 1), (1, 1)]
Inputs strides: ['No strides', (8,), (8, 8), (8, 8)]
Inputs values: [Generator(PCG64) at 0x7F6744184C80, array([100,   2]), array([[0.74738862]]), array([[0.78745484]])]
Outputs clients: [[output[1](Simulator_sim_rv{"(),()->()"}.0)], [Composite{sqr((0.1 * (i0 - i1)))}(sim{[[ 9.52728 ... 47175637]]},
Simulator_sim_rv{"(),()->()"}.out)]]

Backtrace when the node is created (use PyTensor flag traceback__limit=N to make it longer):
  File "/home/wdean/micromamba/envs/pymc-examples/lib/python3.11/multiprocessing/process.py", line 314, in _bootstrap
    self.run()
  File "/home/wdean/micromamba/envs/pymc-examples/lib/python3.11/multiprocessing/process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "/home/wdean/micromamba/envs/pymc-examples/lib/python3.11/concurrent/futures/process.py", line 261, in _process_worker
    r = call_item.fn(*call_item.args, **call_item.kwargs)
  File "/home/wdean/micromamba/envs/pymc-examples/lib/python3.11/site-packages/pymc/smc/sampling.py", line 303, in _sample_smc_int
    smc._initialize_kernel()
  File "/home/wdean/micromamba/envs/pymc-examples/lib/python3.11/site-packages/pymc/smc/kernels.py", line 244, in _initialize_kernel
    self.likelihood_logp_func = _logp_forw(
  File "/home/wdean/micromamba/envs/pymc-examples/lib/python3.11/site-packages/pymc/smc/kernels.py", line 636, in _logp_forw
    out_list, inarray0 = join_nonshared_inputs(
  File "/home/wdean/micromamba/envs/pymc-examples/lib/python3.11/site-packages/pymc/pytensorf.py", line 619, in join_nonshared_inputs
    new_outputs = [
  File "/home/wdean/micromamba/envs/pymc-examples/lib/python3.11/site-packages/pymc/pytensorf.py", line 620, in <listcomp>
    pytensor.clone_replace(output, replace, rebuild_strict=False) for output in outputs

HINT: Use the PyTensor flag `exception_verbosity=high` for a debug print-out and storage map footprint of this Apply node.
Alessange commented 3 days ago

I get the same problem, I'm think it should be the interaction with scipy and pymc api