Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: TypeError: Variables do not support boolean operations with compute_test_value = "raise" for observed random variable #6981

Closed
vandalt opened this issue Nov 1, 2023 · 1 comment · Fixed by #6982
Labels

Comments

@vandalt
Copy link
Contributor

vandalt commented Nov 1, 2023

Describe the issue:

Hi!

When running the tests for dfm/celerite2, with pytensor.config.compute_test_value = "raise", for PyMC v5 and v4, I encountered the error below. I'm adding a simple example to reproduce.

The error seems to come from the use of np.broadcast_to() on rv_var.shape here.
I believe replacing it rv_var.type.shape fixes the issue.

Should I submit a PR with the change? If I write a test with the example below, where is the best place to put it?

Thank you!

Reproduceable code example:

import numpy as np
import pytensor
import pytensor.tensor as pt

import pymc as pm

pytensor.config.compute_test_value = "raise"

rng = np.random.default_rng(123)
data = rng.standard_normal(100)
with pm.Model():
    # NOTE: pt -> np yields same error
    obs = pm.Normal("obs", mu=pt.zeros_like(data), sigma=pt.ones_like(data), observed=data)

Error message:

```python Traceback (most recent call last): File "/home/vandal/repos/astro/pymc/repro_error.py", line 13, in obs = pm.Normal("obs", mu=pt.zeros_like(data), sigma=pt.ones_like(data), observed=data) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/vandal/repos/astro/pymc/pymc/distributions/distribution.py", line 316, in __new__ rv_out = model.register_rv( ^^^^^^^^^^^^^^^^^^ File "/home/vandal/repos/astro/pymc/pymc/model/core.py", line 1315, in register_rv rv_var = self.make_obs_var(rv_var, observed, dims, transform, total_size) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/vandal/repos/astro/pymc/pymc/model/core.py", line 1356, in make_obs_var rv_var.tag.test_value = np.broadcast_to(test_value, rv_var.shape) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/vandal/repos/astro/exoplanet-dev/celerite2-pymc5/venv/lib/python3.11/site-packages/numpy/lib/stride_tricks.py", line 413, in broadcast_to return _broadcast_to(array, shape, subok=subok, readonly=True) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/vandal/repos/astro/exoplanet-dev/celerite2-pymc5/venv/lib/python3.11/site-packages/numpy/lib/stride_tricks.py", line 345, in _broadcast_to if any(size < 0 for size in shape): ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/vandal/repos/astro/exoplanet-dev/celerite2-pymc5/venv/lib/python3.11/site-packages/pytensor/tensor/variable.py", line 72, in __bool__ raise TypeError("Variables do not support boolean operations.") TypeError: Variables do not support boolean operations. ```

PyMC version information:

Output of `pymc.__version__`: 5.9.1+1.g58217cc8.dirty (I played with the code to debug the issue, but it's basically the main branch)

Context for the issue:

As mentioned above, the error affects dfm/celerite2 when running the test suite. Here is the output of that:

```python python/test/pymc/test_pymc_celerite2.py:115: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ python/celerite2/pymc/celerite2.py:96: in marginal return CeleriteNormal( ../../pymc/pymc/distributions/distribution.py:316: in __new__ rv_out = model.register_rv( ../../pymc/pymc/model/core.py:1315: in register_rv rv_var = self.make_obs_var(rv_var, observed, dims, transform, total_size) ../../pymc/pymc/model/core.py:1356: in make_obs_var rv_var.tag.test_value = np.broadcast_to(test_value, rv_var.shape) venv/lib/python3.11/site-packages/numpy/lib/stride_tricks.py:413: in broadcast_to return _broadcast_to(array, shape, subok=subok, readonly=True) venv/lib/python3.11/site-packages/numpy/lib/stride_tricks.py:345: in _broadcast_to if any(size < 0 for size in shape): _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = Lt.0

def __bool__(self):
    # This is meant to prohibit stuff like a < b < c, which is internally
    # implemented as (a < b) and (b < c). The trouble with this is the
    # side-effect that checking for a non-NULL a by typing "if a: ..."
    # uses the same __nonzero__ method.  We want these both to work, but
    # it seems impossible.  Currently, all vars evaluate to nonzero except
    # the return values of comparison operators, which raise this
    # exception.  If you can think of a better solution, go for it!
    #
    # __bool__ is Python 3.x data model. __nonzero__ is Python 2.x.
    if self._is_nonzero:
        return True
    else:
      raise TypeError("Variables do not support boolean operations.")

E TypeError: Variables do not support boolean operations.

venv/lib/python3.11/site-packages/pytensor/tensor/variable.py:72: TypeError

</details>
@vandalt vandalt added the bug label Nov 1, 2023
Copy link

welcome bot commented Nov 1, 2023

Welcome Banner
🎉 Welcome to PyMC! 🎉 We're really excited to have your input into the project! 💖

If you haven't done so already, please make sure you check out our Contributing Guidelines and Code of Conduct.

@vandalt vandalt changed the title BUG: <Please write a comprehensive title after the 'BUG: ' prefix> BUG: TypeError: Variables do not support boolean operations with compute_test_value = "raise" for observed random variable Nov 1, 2023
vandalt added a commit to vandalt/pymc that referenced this issue Nov 1, 2023
…ed RV in model creation (Fix pymc-devs#6981)

- Issue: pymc-devs#6981
- This fix only affects cases with `pytensor.config.compute_test_value!="off"`. Code is not executed otherwise.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant