(howto_debugging)=
:::{post} August 2, 2022 :tags: debugging, PyTensor :category: beginner :author: Thomas Wiecki, Igor Kuvychko :::
There are various levels on which to debug a model. One of the simplest is to just print out the values that different variables are taking on.
Because PyMC uses PyTensor expressions to build the model, and not functions, there is no way to place a print statement into a likelihood function. Instead, you can use the {class}pytensor.printing.Print class to print intermediate values.
PyTensor functionsSince PyTensor functions are compiled to C, you have to use pytensor.printing.Print class to print intermediate values (imported below as Print). Python print function will not work. Below is a simple example of using Print. For more information, see {ref}Debugging PyTensor <pytensor:debug_faq>.
To see what causes the inf value in the output, we can print intermediate values of $(x-y)$ using Print. Print class simply passes along its caller but prints out its value along a user-define message:
Print reveals the root cause: $(x-y)$ takes a zero value when $x=1, y=1$, causing the inf output.
Print output for further analysisWhen we expect many rows of output from Print, it can be desirable to redirect the output to a string buffer and access the values later on (thanks to Lindley Lentati for inspiring this example). Here is a toy example using Python print function:
Exception handling of PyMC v4 has improved, so now SamplingError exception prints out the intermediate values of mu and sd which led to likelihood of -inf. However, this technique of printing intermediate values with aeasara.printing.Print can be valuable in more complicated cases.
Raw output is a bit messy and requires some cleanup and formatting to convert to {ref}numpy.ndarray. In the example below regex is used to clean up the output, and then it is evaluated with eval to give a list of floats. Code below also works with higher-dimensional outputs (in case you want to experiment with different models).
Notice that we requested 5 draws, but got 34 sets of $a/b$ values. The reason is that for each iteration, all proposed values are printed (not just the accepted values). Negative values are clearly problematic.
:::{include} ../page_footer.md :::