aesara-devs / aesara

Aesara is a Python library for defining, optimizing, and efficiently evaluating mathematical expressions involving multi-dimensional arrays.
https://aesara.readthedocs.io
Other
1.18k stars 154 forks source link

RandomVariables raise ValueError when size would broadcast parameter #1055

Closed ricardoV94 closed 2 years ago

ricardoV94 commented 2 years ago

Discussed in https://github.com/aesara-devs/aesara/discussions/1044

Originally posted by **ricardoV94** July 9, 2022 We have a common check in multivariate distributions (+ categorical), that raises when a non-size is not strictly equal to the batched dimensions of the parameters, saying objects cannot be broadcast to a single shape. https://github.com/aesara-devs/aesara/blob/510a9618454293fc00b3cfa78b337c1f2d6d2af3/aesara/tensor/random/basic.py#L377-L383 I don't know why we are imposing this limitation. Even if there is a good reason, the message is wrong, because the batched shapes may very well be broadcastable to size: ```python # Raises ValueError: shape mismatch: objects cannot be broadcast to a single shape # However batch_shape=(1,) and size=(5,) are clearly broadcastable at.random.dirichlet([[0.2, 0.3, 0.5]], size=5).eval() ``` The message for the CategoricalRV is more accurate: https://github.com/aesara-devs/aesara/blob/510a9618454293fc00b3cfa78b337c1f2d6d2af3/aesara/tensor/random/basic.py#L651-L652 However I still don't get why we would not broadcast the parameter to match size, when this is would be valid. Otherwise, the following should also be invalid, no? ```python # Just fine at.random.normal([0], size=5).eval() ```
brandonwillard commented 2 years ago

I don't know why we are imposing this limitation.

We are not imposing a limitation; it looks like there's simply a bug.