Skip to content

RV size is not correctly inferred from dims #5181

Closed
@lucianopaz

Description

@lucianopaz

Description of your problem

While writing a hierarchical model, I ran into a shape problem. Minimally, what I want to do looks like this:

>>> with pm.Model(coords={"feature": [1], "group": ["A", "B"]}):
...     x = pm.Normal("x", 0, 1, dims="feature")
...     y = pm.Normal("y", x[..., None], 1, dims=("feature", "group"))
... y.shape.eval()
WARNING (aesara.tensor.basic_opt): Failed to infer_shape from Op normal_rv{0, (0, 0), floatX, True}.
Input shapes: [None, (TensorConstant{0},), (), (<TensorType(int64, scalar)>, TensorConstant{1}), ()]
Exception encountered during infer_shape: <class 'Exception'>
Exception message: sharedvar.value does not exist anymore. Use sharedvar.get_value() or sharedvar.set_value() instead.
Traceback: Traceback (most recent call last):
  File "/home/lpaz/repos/aesara/aesara/tensor/basic_opt.py", line 867, in get_node_infer_shape
    o_shapes = shape_infer(
  File "/home/lpaz/repos/aesara/aesara/tensor/random/op.py", line 299, in infer_shape
    shape = self._infer_shape(size, dist_params, param_shapes=param_shapes)
  File "/home/lpaz/repos/aesara/aesara/tensor/random/op.py", line 196, in _infer_shape
    param_shapes = params_broadcast_shapes(
  File "/home/lpaz/repos/aesara/aesara/tensor/random/utils.py", line 47, in params_broadcast_shapes
    rev_extra_dims = [
  File "/home/lpaz/repos/aesara/aesara/tensor/random/utils.py", line 48, in <listcomp>
    max_bcast(a, b)
  File "/home/lpaz/repos/aesara/aesara/tensor/random/utils.py", line 41, in max_bcast
    if getattr(x, "value", x) == 1:
  File "/home/lpaz/repos/aesara/aesara/compile/sharedvalue.py", line 211, in _value_get
    raise Exception(
Exception: sharedvar.value does not exist anymore. Use sharedvar.get_value() or sharedvar.set_value() instead.
array([1, 1])

So the shape of y is inferred incorrectly, and this led to problems downstream in the full model. If instead of using dims, I use size, everything works fine.

>>> with pm.Model(coords={"feature": [1], "group": ["A", "B"]}):
...     x = pm.Normal("x", 0, 1, size=(1,))
...     y = pm.Normal("y", x[..., None], 1, size=(1, 2))
... y.shape.eval()
array([1, 2])

Versions and main components

  • PyMC/PyMC3 Version: main branch at commit 073e26b
  • Aesara/Theano Version: 2.2.6
  • Python Version: 3.8.5
  • Operating system: Ubuntu 18.04
  • How did you install PyMC/PyMC3: pip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions