Trying to reproduce the https://tomicapretto.com/posts/2022-06-12_lkj-prior/

Hello,

I am trying to reproce the example ( Hierarchical modeling with the LKJ prior in PyMC – Tomi Capretto ) and getting the error message. I am suspecting these errors due to the version change.

When I run:

with pm.Model(coords=coords) as model_independent:

Common effects

β0 = pm.Normal(“β0”, mu=y_mean, sigma=50)
β1 = pm.Normal(“β1”, mu=0, sigma=10)

# Group-specific effects
# Intercept
σ_u0 = pm.HalfNormal("σ_u0", sigma=50)
u0 = pm.Normal("u0", mu=0, sigma=σ_u0, dims="subject")

# Slope
σ_u1 = pm.HalfNormal("σ_u1", sigma=10)   
u1 = pm.Normal("u1", mu=0, sigma=σ_u1, dims="subject")

# Construct intercept and slope
intercept = pm.Deterministic("intercept", β0 + u0[subjects_idx]) 
slope = pm.Deterministic("slope", (β1 + u1[subjects_idx]) * days) 

# Conditional mean
μ = pm.Deterministic("μ", intercept + slope)

# Residual standard deviation
σ = pm.HalfStudentT("σ", nu=4, sigma=50)

# Response
y = pm.Normal("y", mu=μ, sigma=σ, observed=data["Reaction"])

pm.model_to_graphviz(model_independent)

I get the following errors:

ERROR (pytensor.graph.rewriting.basic): Rewrite failure due to: random_make_inplace
ERROR (pytensor.graph.rewriting.basic): node: t_rv{"(),(),()->()"}(*0-<RandomGeneratorType>, *1-<NoneTypeT>, *2-<Scalar(int8, shape=())>, 0.0, *3-<Scalar(int8, shape=())>)
ERROR (pytensor.graph.rewriting.basic): TRACEBACK:
ERROR (pytensor.graph.rewriting.basic): Traceback (most recent call last):
  File "/home/naser/anaconda3/envs/pymc_env/lib/python3.13/site-packages/pytensor/graph/rewriting/basic.py", line 1920, in process_node
    replacements = node_rewriter.transform(
        fgraph, node, enforce_tracks=enforce_tracks
    )
  File "/home/naser/anaconda3/envs/pymc_env/lib/python3.13/site-packages/pytensor/graph/rewriting/basic.py", line 993, in transform
    return self.fn(fgraph, node)
           ~~~~~~~^^^^^^^^^^^^^^
  File "/home/naser/anaconda3/envs/pymc_env/lib/python3.13/site-packages/pytensor/tensor/random/rewriting/basic.py", line 54, in random_make_inplace
    new_outputs = new_op.make_node(*node.inputs).outputs
                  ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^
  File "/home/naser/anaconda3/envs/pymc_env/lib/python3.13/site-packages/pytensor/tensor/random/op.py", line 368, in make_node
    size = normalize_size_param(size)
  File "/home/naser/anaconda3/envs/pymc_env/lib/python3.13/site-packages/pytensor/tensor/random/utils.py", line 190, in normalize_size_param
    shape = cast(as_tensor_variable(shape, ndim=1, dtype="int64"), "int64")
                 ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/naser/anaconda3/envs/pymc_env/lib/python3.13/site-packages/pytensor/tensor/__init__.py", line 50, in as_tensor_variable
    return _as_tensor_variable(x, name, ndim, **kwargs)
  File "/home/naser/anaconda3/envs/pymc_env/lib/python3.13/functools.py", line 934, in wrapper
    return dispatch(args[0].__class__)(*args, **kw)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
  File "/home/naser/anaconda3/envs/pymc_env/lib/python3.13/site-packages/pytensor/tensor/basic.py", line 113, in _as_tensor_Variable
    raise TypeError(
        f"Tensor type field must be a TensorType; found {type(x.type)}."
    )
TypeError: Tensor type field must be a TensorType; found <class 'pytensor.tensor.type_other.NoneTypeT'>.

ERROR (pytensor.graph.rewriting.basic): Rewrite failure due to: random_make_inplace
ERROR (pytensor.graph.rewriting.basic): node: t_rv{"(),(),()->()"}(*0-<RandomGeneratorType>, *1-<NoneTypeT>, *2-<Scalar(int8, shape=())>, 0.0, *3-<Scalar(int8, shape=())>)
ERROR (pytensor.graph.rewriting.basic): TRACEBACK:
ERROR (pytensor.graph.rewriting.basic): Traceback (most recent call last):
  File "/home/naser/anaconda3/envs/pymc_env/lib/python3.13/site-packages/pytensor/graph/rewriting/basic.py", line 1920, in process_node
    replacements = node_rewriter.transform(
        fgraph, node, enforce_tracks=enforce_tracks
    )
  File "/home/naser/anaconda3/envs/pymc_env/lib/python3.13/site-packages/pytensor/graph/rewriting/basic.py", line 993, in transform
    return self.fn(fgraph, node)
           ~~~~~~~^^^^^^^^^^^^^^
  File "/home/naser/anaconda3/envs/pymc_env/lib/python3.13/site-packages/pytensor/tensor/random/rewriting/basic.py", line 54, in random_make_inplace
    new_outputs = new_op.make_node(*node.inputs).outputs
                  ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^
  File "/home/naser/anaconda3/envs/pymc_env/lib/python3.13/site-packages/pytensor/tensor/random/op.py", line 368, in make_node
    size = normalize_size_param(size)
  File "/home/naser/anaconda3/envs/pymc_env/lib/python3.13/site-packages/pytensor/tensor/random/utils.py", line 190, in normalize_size_param
    shape = cast(as_tensor_variable(shape, ndim=1, dtype="int64"), "int64")
                 ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/naser/anaconda3/envs/pymc_env/lib/python3.13/site-packages/pytensor/tensor/__init__.py", line 50, in as_tensor_variable
    return _as_tensor_variable(x, name, ndim, **kwargs)
  File "/home/naser/anaconda3/envs/pymc_env/lib/python3.13/functools.py", line 934, in wrapper
    return dispatch(args[0].__class__)(*args, **kw)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
  File "/home/naser/anaconda3/envs/pymc_env/lib/python3.13/site-packages/pytensor/tensor/basic.py", line 113, in _as_tensor_Variable
    raise TypeError(
        f"Tensor type field must be a TensorType; found {type(x.type)}."
    )
TypeError: Tensor type field must be a TensorType; found <class 'pytensor.tensor.type_other.NoneTypeT'>.

1 Like

The issue was fixed here Handle non-constant NoneTypeT variables by ricardoV94 · Pull Request #1728 · pymc-devs/pytensor · GitHub but we haven’t released a new version with the patch yet.

Try to give an explicit shape to the half student variable, shape=()

Thank you, it worked.

I have another issue. When I run the model I get the follwoing errors:

with model_independent:
idata_independent = pm.sample(draws=1000, chains=4, random_seed=1234)

nitializing NUTS using jitter+adapt_diag...

---------------------------------------------------------------------------
SamplingError                             Traceback (most recent call last)
Cell In[43], line 2
      1 with model_independent:
----> 2     idata_independent = pm.sample(draws=1000, chains=4, random_seed=1234)

File ~/anaconda3/envs/pymc_env/lib/python3.13/site-packages/pymc/sampling/mcmc.py:825, in sample(draws, tune, chains, cores, random_seed, progressbar, progressbar_theme, step, var_names, nuts_sampler, initvals, init, jitter_max_retries, n_init, trace, discard_tuned_samples, compute_convergence_checks, keep_warning_stat, return_inferencedata, idata_kwargs, nuts_sampler_kwargs, callback, mp_ctx, blas_cores, model, compile_kwargs, **kwargs)
    823         [kwargs.setdefault(k, v) for k, v in nuts_kwargs.items()]
    824     with joined_blas_limiter():
--> 825         initial_points, step = init_nuts(
    826             init=init,
    827             chains=chains,
    828             n_init=n_init,
    829             model=model,
    830             random_seed=random_seed_list,
    831             progressbar=progress_bool,
    832             jitter_max_retries=jitter_max_retries,
    833             tune=tune,
    834             initvals=initvals,
    835             compile_kwargs=compile_kwargs,
    836             **kwargs,
    837         )
    838 else:
    839     # Get initial points
    840     ipfns = make_initial_point_fns_per_chain(
    841         model=model,
    842         overrides=initvals,
    843         jitter_rvs=set(),
    844         chains=chains,
    845     )

File ~/anaconda3/envs/pymc_env/lib/python3.13/site-packages/pymc/sampling/mcmc.py:1598, in init_nuts(init, chains, n_init, model, random_seed, progressbar, jitter_max_retries, tune, initvals, compile_kwargs, **kwargs)
   1595     q, _ = DictToArrayBijection.map(ip)
   1596     return logp_dlogp_func([q], extra_vars={})[0]
-> 1598 initial_points = _init_jitter(
   1599     model,
   1600     initvals,
   1601     seeds=random_seed_list,
   1602     jitter="jitter" in init,
   1603     jitter_max_retries=jitter_max_retries,
   1604     logp_fn=model_logp_fn,
   1605 )
   1607 apoints = [DictToArrayBijection.map(point) for point in initial_points]
   1608 apoints_data = [apoint.data for apoint in apoints]

File ~/anaconda3/envs/pymc_env/lib/python3.13/site-packages/pymc/sampling/mcmc.py:1479, in _init_jitter(model, initvals, seeds, jitter, jitter_max_retries, logp_fn)
   1476 if not np.isfinite(point_logp):
   1477     if i == jitter_max_retries:
   1478         # Print informative message on last attempted point
-> 1479         model.check_start_vals(point)
   1480     # Retry with a new seed
   1481     seed = rng.integers(2**30, dtype=np.int64)

File ~/anaconda3/envs/pymc_env/lib/python3.13/site-packages/pymc/model/core.py:1784, in Model.check_start_vals(self, start, **kwargs)
   1781 initial_eval = self.point_logps(point=elem, **kwargs)
   1783 if not all(np.isfinite(v) for v in initial_eval.values()):
-> 1784     raise SamplingError(
   1785         "Initial evaluation of model at starting point failed!\n"
   1786         f"Starting values:\n{elem}\n\n"
   1787         f"Logp initial evaluation results:\n{initial_eval}\n"
   1788         "You can call `model.debug()` for more details."
   1789     )

SamplingError: Initial evaluation of model at starting point failed!
Starting values:
{'β0': array(298.33678196), 'β1': array(0.06536442), 'σ_u0_log__': array(3.73674103), 'u0': array([ 0.62679198, -0.08377323,  0.80064435,  0.22881392, -0.24317533,
       -0.16407164,  0.17099546, -0.84041625,  0.14072142, -0.90524981,
       -0.07625477, -0.1374892 , -0.68311982,  0.89814437, -0.95859511,
        0.26011746, -0.31256015, -0.96481085]), 'σ_u1_log__': array(3.00462913), 'u1': array([ 0.22436432,  0.29857596,  0.3123814 ,  0.06949544,  0.53333861,
        0.27958275,  0.18009367, -0.30398205,  0.52690892, -0.55530598,
       -0.73721338, -0.69095114, -0.97563417, -0.93442142,  0.12691805,
        0.58573641, -0.2011475 ,  0.04993586]), 'σ_log__': array(4.25098235)}

Logp initial evaluation results:
{'β0': np.float64(-4.83), 'β1': np.float64(-3.22), 'σ_u0': np.float64(-0.75), 'u0': np.float64(-83.8), 'σ_u1': np.float64(-1.56), 'u1': np.float64(-70.63), 'σ': np.float64(nan), 'y': np.float64(-989.95)}
You can call `model.debug()` for more details.

The initial point for sigma may be too extreme, try a different seed, or reduce the sigma?

You can call model.debug see if tells anything useful

Thank you, it worked by changing the sigma value.