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

AttributeError when sampling with PyMC model using conditional probabilities #7667

Open
ricardoV94 opened this issue Jan 31, 2025 · 1 comment
Labels
bug needs info Additional information required pytensor

Comments

@ricardoV94
Copy link
Member

Discussed in #7618

Originally posted by yk4978 December 18, 2024

I am encountering an error when trying to sample from a PyMC model. In the model, I am defining a set of Bernoulli variables with conditional probabilities based on another parent Bernoulli variable A. The model definition itself works fine, but when I attempt to sample and calculate the posterior, I get the following error:
AttributeError: 'Scratchpad' object has no attribute 'ufunc'

Here is the code I am using:

import pymc as pm

n = 40
with pm.Model() as model:
    A = pm.Bernoulli('A', p=0.001)
    for i in range(n):
        if i < n // 2:
            pm.Bernoulli(f"A{i}", p=pm.math.switch(A, 0.6, 0.01), observed=1)
        else:
            pm.Bernoulli(f"A{i}", p=pm.math.switch(A, 0.6, 0.01))
    trace = pm.sample(1000, return_inferencedata=False, cores=1)

probs = sum(trace['A']) / len(trace['A'])
print(f"probs: {probs}")

Can anyone help me identify what might be causing this error and suggest a possible solution?

@ricardoV94 ricardoV94 added bug needs info Additional information required pytensor labels Jan 31, 2025
@ricardoV94
Copy link
Member Author

@yk4978 I've seen that when PyTensor fails to fuse long graphs. Did you see any warnings prior to the failure?

How did you install pymc? And can you try on the latest version?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug needs info Additional information required pytensor
Projects
None yet
Development

No branches or pull requests

1 participant