pymc-devs / pymc-resources

PyMC educational resources
MIT License
1.96k stars 745 forks source link

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

Open chiyu1203 opened 1 year ago

chiyu1203 commented 1 year ago

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.
fonnesbeck commented 1 year ago

Can you try running this with the most recent version of PyMC (v5)?

chiyu1203 commented 1 year ago

Can you try running this with the most recent version of PyMC (v5)?

Hello! I have tried the most recent version PyMC (v5). This time, I bumped into another error in an earlier cell so I am not sure if that would fix 5.34 Below is the new error I saw in 5.19

data["Divorce_std"] = standardize(data["Divorce"])
data["Marriage_std"] = standardize(data["Marriage"])
data["MedianAgeMarriage_std"] = standardize(data["MedianAgeMarriage"])

# Use Aesara shared variables so we can change them later
marriage_shared = shared(data["Marriage_std"].values)
age_shared = shared(data["MedianAgeMarriage_std"].values)

with pm.Model() as m5_3_A:
    # A -> D <- M
    sigma = pm.Exponential("sigma", 1)
    bA = pm.Normal("bA", 0, 0.5)
    bM = pm.Normal("bM", 0, 0.5)

    a = pm.Normal("a", 0, 0.2)
    mu = pm.Deterministic("mu", a + bA * age_shared + bM * marriage_shared)
    divorce = pm.Normal("divorce", mu, sigma, observed=data["Divorce_std"].values)

    # A -> M
    sigma_M = pm.Exponential("sigma_m", 1)
    bAM = pm.Normal("bAM", 0, 0.5)
    aM = pm.Normal("aM", 0, 0.2)
    mu_M = pm.Deterministic("mu_m", aM + bAM * age_shared)
    marriage = pm.Normal("marriage", mu_M, sigma_M, observed=data["Marriage_std"].values)

    m5_3_A_trace = pm.sample()

NotImplementedError Traceback (most recent call last) Cell In[33], line 16 13 bM = pm.Normal("bM", 0, 0.5) 15 a = pm.Normal("a", 0, 0.2) ---> 16 mu = pm.Deterministic("mu", a + bA age_shared + bM marriage_shared) 17 divorce = pm.Normal("divorce", mu, sigma, observed=data["Divorce_std"].values) 19 # A -> M

File ~\anaconda3\envs\pymc_env\Lib\site-packages\aesara\tensor\var.py:207, in _tensor_py_operators.rmul(self, other) 206 def rmul(self, other): --> 207 return at.math.mul(other, self)

File ~\anaconda3\envs\pymc_env\Lib\site-packages\aesara\graph\op.py:297, in Op.call(self, *inputs, *kwargs) 255 r"""Construct an Apply node using :meth:Op.make_node and return its outputs. 256 257 This method is just a wrapper around :meth:Op.make_node. (...) 294 295 """ 296 return_list = kwargs.pop("return_list", False) --> 297 node = self.make_node(inputs, **kwargs) 299 if config.compute_test_value != "off": 300 compute_test_value(node)

File ~\anaconda3\envs\pymc_env\Lib\site-packages\aesara\tensor\elemwise.py:484, in Elemwise.make_node(self, inputs) 478 def make_node(self, inputs): 479 """ 480 If the inputs have different number of dimensions, their shape 481 is left-completed to the greatest number of dimensions with 1s 482 using DimShuffle. 483 """ --> 484 inputs = [as_tensor_variable(i) for i in inputs] 485 out_dtypes, out_shapes, inputs = self.get_output_info(DimShuffle, *inputs) 486 outputs = [ 487 TensorType(dtype=dtype, shape=shape)() 488 for dtype, shape in zip(out_dtypes, out_shapes) 489 ]

File ~\anaconda3\envs\pymc_env\Lib\site-packages\aesara\tensor\elemwise.py:484, in (.0) 478 def make_node(self, inputs): 479 """ 480 If the inputs have different number of dimensions, their shape 481 is left-completed to the greatest number of dimensions with 1s 482 using DimShuffle. 483 """ --> 484 inputs = [as_tensor_variable(i) for i in inputs] 485 out_dtypes, out_shapes, inputs = self.get_output_info(DimShuffle, inputs) 486 outputs = [ 487 TensorType(dtype=dtype, shape=shape)() 488 for dtype, shape in zip(out_dtypes, out_shapes) 489 ]

File ~\anaconda3\envs\pymc_env\Lib\site-packages\aesara\tensor__init__.py:49, in as_tensor_variable(x, name, ndim, kwargs) 17 def as_tensor_variable( 18 x: TensorLike, name: Optional[str] = None, ndim: Optional[int] = None, kwargs 19 ) -> "TensorVariable": 20 """Convert x into an equivalent TensorVariable. 21 22 This function can be used to turn ndarrays, numbers, ScalarType instances, (...) 47 48 """ ---> 49 return _as_tensor_variable(x, name, ndim, **kwargs)

File ~\anaconda3\envs\pymc_env\Lib\functools.py:909, in singledispatch..wrapper(*args, *kw) 905 if not args: 906 raise TypeError(f'{funcname} requires at least ' 907 '1 positional argument') --> 909 return dispatch(args[0].class)(args, **kw)

File ~\anaconda3\envs\pymc_env\Lib\site-packages\aesara\tensor__init__.py:56, in _as_tensor_variable(x, name, ndim, kwargs) 52 @singledispatch 53 def _as_tensor_variable( 54 x: TensorLike, name: Optional[str], ndim: Optional[int], kwargs 55 ) -> "TensorVariable": ---> 56 raise NotImplementedError(f"Cannot convert {x!r} to a tensor variable.")

NotImplementedError: Cannot convert bA to a tensor variable.

Last but not least, this is my computer setting: Window11, anaconda, pymc v5 conda create -c conda-forge -n pymc_env "pymc>=5" Libraries required for this notebook were installed as below: pip install notebook, patsy, causalgraphicalmodels, daft, theano conda install -c conda-forge watermark, seaborn, tqdm, aesara==2.8.7 Initially, I was hoping to install all the libraries according to the environment yaml file for pymc v4. However, there was some verion conflicts so I ended up installing some of the libraries via pip. Please feel free to let me know if you have any thought.