Skip to content

rethinking chpater5 notebook: 5.34 ValueError: shape mismatch: objects cannot be broadcast to a single shape. #231

Open
@chiyu1203

Description

@chiyu1203

I am using chapter 5 notebook. Everything worked until 5.34. which returned this error. Has anyone seen this before?

I use WIndow11, anaconda, pymc v4, installed via yaml file for v4

I have also registered this environment as the new kernel with the following command
python -m ipykernel install --user --name stat-rethink2-pymc3_4 --display-name "Python 3.10 (stat-rethink2-pymc3_4)"

Note that I have tried v3 version but it would return AttributeError: module 'numpy' has no attribute 'bool' when importing pymc3
That is why I continoue the rest of the cells with pymc v4.

Below is the problem cell I have and the returned error message.

xseq = [-2, 2]
shared_N.set_value(np.array(xseq))
with m5_5_draft:
    m5_5_draft_prior_predictive = pm.sample_prior_predictive()
> Sampling: [K, a, bN, sigma]
> ---------------------------------------------------------------------------
> ValueError                                Traceback (most recent call last)
> File ~\anaconda3\envs\stat-rethink2-pymc_v4\lib\site-packages\aesara\compile\function\types.py:971, in Function.__call__(self, *args, **kwargs)
>     969 try:
>     970     outputs = (
> --> 971         self.vm()
>     972         if output_subset is None
>     973         else self.vm(output_subset=output_subset)
>     974     )
>     975 except Exception:
> 
> File ~\anaconda3\envs\stat-rethink2-pymc_v4\lib\site-packages\aesara\graph\op.py:543, in Op.make_py_thunk.<locals>.rval(p, i, o, n, params)
>     539 @is_thunk_type
>     540 def rval(
>     541     p=p, i=node_input_storage, o=node_output_storage, n=node, params=None
>     542 ):
> --> 543     r = p(n, [x[0] for x in i], o)
>     544     for o in node.outputs:
> 
> File ~\anaconda3\envs\stat-rethink2-pymc_v4\lib\site-packages\aesara\tensor\random\op.py:368, in RandomVariable.perform(self, node, inputs, outputs)
>     366 rng_var_out[0] = rng
> --> 368 smpl_val = self.rng_fn(rng, *(args + [size]))
>     370 if (
>     371     not isinstance(smpl_val, np.ndarray)
>     372     or str(smpl_val.dtype) != out_var.type.dtype
>     373 ):
> 
> File ~\anaconda3\envs\stat-rethink2-pymc_v4\lib\site-packages\aesara\tensor\random\op.py:166, in RandomVariable.rng_fn(self, rng, *args, **kwargs)
>     165 """Sample a numeric random variate."""
> --> 166 return getattr(rng, self.name)(*args, **kwargs)
> 
> File _generator.pyx:1220, in numpy.random._generator.Generator.normal()
> 
> File _common.pyx:600, in numpy.random._common.cont()
> 
> File _common.pyx:517, in numpy.random._common.cont_broadcast_2()
> 
> File __init__.cython-30.pxd:780, in numpy.PyArray_MultiIterNew3()
> 
> ValueError: shape mismatch: objects cannot be broadcast to a single shape.  Mismatch is between arg 0 with shape (17,) and arg 1 with shape (2,).
> 
> During handling of the above exception, another exception occurred:
> 
> ValueError                                Traceback (most recent call last)
> Cell In[54], line 4
>       2 shared_N.set_value(np.array(xseq))
>       3 with m5_5_draft:
> ----> 4     m5_5_draft_prior_predictive = pm.sample_prior_predictive()
> 
> File ~\anaconda3\envs\stat-rethink2-pymc_v4\lib\site-packages\pymc\sampling.py:2169, in sample_prior_predictive(samples, model, var_names, random_seed, return_inferencedata, idata_kwargs, compile_kwargs)
>    2167 # All model variables have a name, but mypy does not know this
>    2168 _log.info(f"Sampling: {list(sorted(volatile_basic_rvs, key=lambda var: var.name))}")  # type: ignore
> -> 2169 values = zip(*(sampler_fn() for i in range(samples)))
>    2171 data = {k: np.stack(v) for k, v in zip(names, values)}
>    2172 if data is None:
> 
> File ~\anaconda3\envs\stat-rethink2-pymc_v4\lib\site-packages\pymc\sampling.py:2169, in <genexpr>(.0)
>    2167 # All model variables have a name, but mypy does not know this
>    2168 _log.info(f"Sampling: {list(sorted(volatile_basic_rvs, key=lambda var: var.name))}")  # type: ignore
> -> 2169 values = zip(*(sampler_fn() for i in range(samples)))
>    2171 data = {k: np.stack(v) for k, v in zip(names, values)}
>    2172 if data is None:
> 
> File ~\anaconda3\envs\stat-rethink2-pymc_v4\lib\site-packages\aesara\compile\function\types.py:984, in Function.__call__(self, *args, **kwargs)
>     982     if hasattr(self.vm, "thunks"):
>     983         thunk = self.vm.thunks[self.vm.position_of_error]
> --> 984     raise_with_op(
>     985         self.maker.fgraph,
>     986         node=self.vm.nodes[self.vm.position_of_error],
>     987         thunk=thunk,
>     988         storage_map=getattr(self.vm, "storage_map", None),
>     989     )
>     990 else:
>     991     # old-style linkers raise their own exceptions
>     992     raise
> 
> File ~\anaconda3\envs\stat-rethink2-pymc_v4\lib\site-packages\aesara\link\utils.py:534, in raise_with_op(fgraph, node, thunk, exc_info, storage_map)
>     529     warnings.warn(
>     530         f"{exc_type} error does not allow us to add an extra error message"
>     531     )
>     532     # Some exception need extra parameter in inputs. So forget the
>     533     # extra long error message in that case.
> --> 534 raise exc_value.with_traceback(exc_trace)
> 
> File ~\anaconda3\envs\stat-rethink2-pymc_v4\lib\site-packages\aesara\compile\function\types.py:971, in Function.__call__(self, *args, **kwargs)
>     968 t0_fn = time.time()
>     969 try:
>     970     outputs = (
> --> 971         self.vm()
>     972         if output_subset is None
>     973         else self.vm(output_subset=output_subset)
>     974     )
>     975 except Exception:
>     976     restore_defaults()
> 
> File ~\anaconda3\envs\stat-rethink2-pymc_v4\lib\site-packages\aesara\graph\op.py:543, in Op.make_py_thunk.<locals>.rval(p, i, o, n, params)
>     539 @is_thunk_type
>     540 def rval(
>     541     p=p, i=node_input_storage, o=node_output_storage, n=node, params=None
>     542 ):
> --> 543     r = p(n, [x[0] for x in i], o)
>     544     for o in node.outputs:
>     545         compute_map[o][0] = True
> 
> File ~\anaconda3\envs\stat-rethink2-pymc_v4\lib\site-packages\aesara\tensor\random\op.py:368, in RandomVariable.perform(self, node, inputs, outputs)
>     364     rng = copy(rng)
>     366 rng_var_out[0] = rng
> --> 368 smpl_val = self.rng_fn(rng, *(args + [size]))
>     370 if (
>     371     not isinstance(smpl_val, np.ndarray)
>     372     or str(smpl_val.dtype) != out_var.type.dtype
>     373 ):
>     374     smpl_val = _asarray(smpl_val, dtype=out_var.type.dtype)
> 
> File ~\anaconda3\envs\stat-rethink2-pymc_v4\lib\site-packages\aesara\tensor\random\op.py:166, in RandomVariable.rng_fn(self, rng, *args, **kwargs)
>     164 def rng_fn(self, rng, *args, **kwargs):
>     165     """Sample a numeric random variate."""
> --> 166     return getattr(rng, self.name)(*args, **kwargs)
> 
> File _generator.pyx:1220, in numpy.random._generator.Generator.normal()
> 
> File _common.pyx:600, in numpy.random._common.cont()
> 
> File _common.pyx:517, in numpy.random._common.cont_broadcast_2()
> 
> File __init__.cython-30.pxd:780, in numpy.PyArray_MultiIterNew3()


> ValueError: shape mismatch: objects cannot be broadcast to a single shape.  Mismatch is between arg 0 with shape (17,) and arg 1 with shape (2,).
> Apply node that caused the error: normal_rv{0, (0, 0), floatX, True}(RandomGeneratorSharedVariable(<Generator(PCG64) at 0x27DC19E8E40>), TensorConstant{(1,) of 17}, TensorConstant{11}, mu, sigma)
> Toposort index: 6
> Inputs types: [RandomGeneratorType, TensorType(int64, (1,)), TensorType(int64, ()), TensorType(float64, (None,)), TensorType(float64, ())]
> Inputs shapes: ['No shapes', (1,), (), (2,), ()]
> Inputs strides: ['No strides', (8,), (), (8,), ()]
> Inputs values: [Generator(PCG64) at 0x27DC19E8E40, array([17], dtype=int64), array(11, dtype=int64), array([ 1.14271312, -4.0934738 ]), array(0.8151317)]
> Outputs clients: [['output'], ['output']]
> 
> HINT: Re-running with most Aesara optimizations disabled could provide a back-trace showing when this node was created. This can be done by setting the Aesara flag 'optimizer=fast_compile'. If that does not work, Aesara optimizations can be disabled with 'optimizer=None'.
> HINT: Use the Aesara flag `exception_verbosity=high` for a debug print-out and storage map footprint of this Apply node.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions