-
Notifications
You must be signed in to change notification settings - Fork 134
BUG: InconsistencyError Multiple destroyers of CGemv{no_inplace}.0 #1420
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
Comments
I can reproduce, sounds like a PyTensor bug. Perhaps something breaks when the graph is too large during rewrites. In the meantime just wanted to say that you probably want to think of a way you can vectorize your model, instead of defining separate variables / likelihood for each group. It should scale much better: NOTE: UNTESTED CODE AND LOGIC, LIKELY MESSED SOMETHING UP IN THE REWRITE import numpy as np
import pymc as pm
import pytensor.tensor as pt
# Simulated data of some spline
N = 500
np.random.seed(42)
x = np.linspace(0, 10, N)
true_knots = [3, 7]
y = np.piecewise(
x,
[x <= 3, (x > 3) & (x <= 7), x > 7],
[lambda x: 0.5 * x, lambda x: 1.5 + 0.2 * (x - 3), lambda x: 2.3 - 0.1 * (x - 7)],
)
y += np.random.normal(0, 0.2, size=len(x)) # Add noise
# Artificial groups
group = np.random.choice([1, 2, 3], size=N)
# Try to fit another spline with knots at:
# many knots leads to InconsistencyError error
knots = np.linspace(0, 10, num=25)
# Few knots seems to work
# knots = [0, 1, 2, 7, 9]
n_knots = len(knots)
groups = np.unique(group).tolist()
with pm.Model() as model:
sigma = pm.HalfCauchy("sigma", beta=1)
sigma_beta0 = pm.HalfNormal("sigma_beta0", sigma=10)
beta0 = pm.HalfNormal("beta_0", sigma=sigma_beta0, shape=len(groups))
z = pm.Normal(f"z", mu=0, sigma=2, shape=(len(groups), n_knots))
X = pt.maximum(0, x[:, None] - knots[None, :]) # (n, knots)
delta_factors = pt.special.softmax(z, axis=-1) # (groups, knots)
slope_factors = 1 - pt.cumsum(delta_factors[:, :-1], axis=1) # (groups, knots-1)
spline_slopes = pt.join(-1, beta0[:, None], beta0[:, None] * slope_factors) # (groups, knots-1)
beta = pt.join(-1, beta0[:, None], pt.diff(spline_slopes, axis=-1)) # (groups, knots)
for i, gr in enumerate(groups):
# (n_i, knots) @ (knots) -> (n_i)
mu_i = pt.matvec(X[group == gr], beta[i])
y_obs = pm.Normal(f"y_obs_{gr}", mu_i, sigma=sigma, observed=y[group == gr])
# If you ve many groups this may be better
# ((n, knots) * (n, knots)).sum(-1) = (n,)
# mu = (X * beta[group]).sum(-1)
# y_obs = pm.Normal("y_obs", mu=mu, sigma=sigma, observed=y)
model.compile_logp()(model.initial_point()) The logp at the initial point matches, which is the first sanity check I would do to make sure I didn't mess up. |
I'll move this issue to PyTensor. Thanks for reporting :) |
Describe the issue:
I get the following error when setting up a hierarchical model for a Bayesian spline:
I tried to simplify to the most simple setup I could find
I am completely clueless, as the error only seems to happen when I increase the number of knots to a somewhat larger number.
Reproduceable code example:
Error message:
PyMC version information:
PYMC 5.22.0
Pytensor 2.30.3
Winpython 3.13.3
Context for the issue:
No response
The text was updated successfully, but these errors were encountered: