Debugging a model: init(), etc

Hello,

I’m verifying some PyMC-based Python code I inherited and that gives results that are reproducible but disagree with results by other methods we are using (sometimes more, sometimes less, depending on inputs).

I noticed our code overrides Model.init without calling super().init(), although PyMC documentation says it should be called. What exactly happens when super().init is not called?

How can I print the contents of the Model (i.e. list of nodes with formulas or parameters, etc)?

Any other advice how to debug a Model?

Thanks!

Best, Oliver

Hi, there is one way to get a really detailed debugging report but I don’t know if that much level of detail is useful for you:

import pytensor
import pymc as pm

with pm.Model() as m:
   ...

print("Logp-dlogp fn")
pytensor.dprint(
    m.logp_dlogp_function()._pytensor_function
)

print("Trace fn")
pytensor.dprint(
    m.compile_fn(inputs=m.value_vars, outs=m.unobserved_value_vars, point_fn=False)
)

Notice it goes through pytensor which pymc depends on so perhaps looking at pytensor documents for debugging models would be more beneficial for you. On the other hand to get an overall view of the model you can do

pm.model_to_graphviz(model)

which will plot a graph of your model which might be easier to eyeball. Finally if the two models have comparable structure (like say some transformed priors that should be identical but you are not sure), you can use eval on the these to check they give the same results for different points such as:

with pm.Model() as model:
  a = pm.Normal('a', mu=0, sd=2)
  b = pm.Exp('b', 1)
  c = pm.Normal('c', mu=a, sd=1)

  print(c.eval({a:0, b:1}))

You may also look here which is similar to what I wrote above but has a bit more detail:

and
https://www.pymc.io/projects/docs/en/v5.4.0/api/generated/classmethods/pymc.Model.debug.html