@cluhmann : I rewrote my routine as follows, and it compiles. However, when sampling the model, I have an error output: `
with pm.Model() as model:
# The DP priors to obtain w, the cluster weights
alpha = pm.Gamma('alpha', 1., 1.)
beta = pm.Beta('beta', 1, alpha, shape=K)
w = pm.Deterministic('w', stick_breaking(beta))
theta = pm.Beta('theta', 1, 1, shape=(K, P))
def bernoulli_loglhh(theta1, weights):
def _logp(value):
value_neg = 1 - value
logtheta = tt.log(theta1)
neglogtheta = tt.log(1-theta1)
# N*K matrix of likelihood contributions from X_i with mu_k
betas = tt.tensordot(value, logtheta, axes=[1, 1]) + tt.tensordot(value_neg, neglogtheta, axes=[1, 1])
## Form alphas, NxK matrix with the component weights included
alphas = (betas + tt.log(weights)).T
# Take LSE rowise to get N vector
# and add alpha_cluster1 to get the total likelihood across all
# components for each X_i
lse_clust = pm.math.logsumexp(alphas - alphas[0, :], axis=0) + alphas[0,:]
# Final overall sum
return tt.sum(lse_clust)
return _logp
obs = pm.DensityDist('obs', w, logp=bernoulli_loglhh, observed=df) # no error without this line
and the models’ execution:
with model:
trace2 = pm.sample(random_seed=17)
which generates the following error message (Here is the last line:
AttributeError: 'function' object has no attribute 'owner'
FULL ERROR MESSAGE:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Input In [55], in <cell line: 1>()
1 with model:
----> 2 trace2 = pm.sample(random_seed=17)
File ~/anaconda3/envs/pymc4/lib/python3.9/site-packages/pymc/sampling.py:524, in sample(draws, step, init, n_init, initvals, trace, chain_idx, chains, cores, tune, progressbar, model, random_seed, discard_tuned_samples, compute_convergence_checks, callback, jitter_max_retries, return_inferencedata, idata_kwargs, mp_ctx, **kwargs)
521 auto_nuts_init = False
523 initial_points = None
--> 524 step = assign_step_methods(model, step, methods=pm.STEP_METHODS, step_kwargs=kwargs)
526 if isinstance(step, list):
527 step = CompoundStep(step)
File ~/anaconda3/envs/pymc4/lib/python3.9/site-packages/pymc/sampling.py:207, in assign_step_methods(model, step, methods, step_kwargs)
204 # Use competence classmethods to select step methods for remaining
205 # variables
206 selected_steps = defaultdict(list)
--> 207 model_logpt = model.logpt()
209 for var in model.value_vars:
210 if var not in assigned_vars:
211 # determine if a gradient can be computed
File ~/anaconda3/envs/pymc4/lib/python3.9/site-packages/pymc/model.py:745, in Model.logpt(self, vars, jacobian, sum)
743 rv_logps: List[TensorVariable] = []
744 if rv_values:
--> 745 rv_logps = joint_logpt(list(rv_values.keys()), rv_values, sum=False, jacobian=jacobian)
746 assert isinstance(rv_logps, list)
748 # Replace random variables by their value variables in potential terms
File ~/anaconda3/envs/pymc4/lib/python3.9/site-packages/pymc/distributions/logprob.py:238, in joint_logpt(var, rv_values, jacobian, scaling, transformed, sum, **kwargs)
234 # Raise if there are unexpected RandomVariables in the logp graph
235 # Only SimulatorRVs are allowed
236 from pymc.distributions.simulator import SimulatorRV
--> 238 unexpected_rv_nodes = [
239 node
240 for node in aesara.graph.ancestors(list(temp_logp_var_dict.values()))
241 if (
242 node.owner
243 and isinstance(node.owner.op, RandomVariable)
244 and not isinstance(node.owner.op, SimulatorRV)
245 )
246 ]
247 if unexpected_rv_nodes:
248 raise ValueError(
249 f"Random variables detected in the logp graph: {unexpected_rv_nodes}.\n"
250 "This can happen when DensityDist logp or Interval transform functions "
251 "reference nonlocal variables."
252 )
File ~/anaconda3/envs/pymc4/lib/python3.9/site-packages/pymc/distributions/logprob.py:238, in <listcomp>(.0)
234 # Raise if there are unexpected RandomVariables in the logp graph
235 # Only SimulatorRVs are allowed
236 from pymc.distributions.simulator import SimulatorRV
--> 238 unexpected_rv_nodes = [
239 node
240 for node in aesara.graph.ancestors(list(temp_logp_var_dict.values()))
241 if (
242 node.owner
243 and isinstance(node.owner.op, RandomVariable)
244 and not isinstance(node.owner.op, SimulatorRV)
245 )
246 ]
247 if unexpected_rv_nodes:
248 raise ValueError(
249 f"Random variables detected in the logp graph: {unexpected_rv_nodes}.\n"
250 "This can happen when DensityDist logp or Interval transform functions "
251 "reference nonlocal variables."
252 )
File ~/anaconda3/envs/pymc4/lib/python3.9/site-packages/aesara/graph/basic.py:845, in ancestors(graphs, blockers)
842 if r.owner and (not blockers or r not in blockers):
843 return reversed(r.owner.inputs)
--> 845 yield from cast(Generator[Variable, None, None], walk(graphs, expand, False))
File ~/anaconda3/envs/pymc4/lib/python3.9/site-packages/aesara/graph/basic.py:808, in walk(nodes, expand, bfs, return_children, hash_fn)
804 if node_hash not in rval_set:
806 rval_set.add(node_hash)
--> 808 new_nodes: Optional[Iterable[T]] = expand(node)
810 if return_children:
811 yield node, new_nodes
File ~/anaconda3/envs/pymc4/lib/python3.9/site-packages/aesara/graph/basic.py:842, in ancestors.<locals>.expand(r)
841 def expand(r: Variable) -> Optional[Iterator[Variable]]:
--> 842 if r.owner and (not blockers or r not in blockers):
843 return reversed(r.owner.inputs)
AttributeError: 'function' object has no attribute 'owner'