Implementing Chinese Restaurant Process - pytensor error on CustomDist

Thanks for the answer @ricardoV94 !

CustomDist is called twice, once without size, and once with, just to figure out the support dimensionality of the distribution. The second time it’s called with the actual size. So you can try to handle the case where size.type.shape==(0,) just so the second call happens.

Yes, that’s what I have been trying.

There are however two greater issues, in that PyMC will not be able to guess the logp of your dist. The first is that your CustomDist involves two RVs, the first Uniform first_roll and the Categorical choice inside the Scan. Actually I don’t know if this is a problem in general, but definitely the graph between first_roll and assignments is too much for PyMC to figure out how to get the logprob. Either way, you could get around this by moving the first Uniform outside of the CustomDist and passing it as a parameter.

Thanks, I will do.

The more important issue is that PyMC does not know how to derive the logprob of a set_subtensor operation. If you can rewrite it to avoid it, it should be able to derive the whole logprob. Also PyMC doesn’t know what to do with the last astype(int) but looks like you should be able to avoid it.

Yes, I can easily avoid the astype(‘int’): I added it because the error message seemed to hint that it expected an “int64” tensor type somewhere.
Is there a list of supported and not supported tensor functions somewhere? So that I know if there are other operations that I should avoid.

I suggest you start with a simpler Scan RV (even if it’s not what you care about), to put a finger on where things fail. PyMC logprob inference is still new, and has a lot of operations it can’t handle. Unfortunately we don’t have useful messages besides “logprob could not be derived” which are not useful when you have so many operations that could have caused it to fail

One of the thing that stopped my debugging is that the error message did not explicitly say that the logprob derivation was the problem. It seemed more of a shape or tensor type problem.
Now that I know that subtensor operation and the derivation of logprob is the problem I’ll try different approaches.
For example, instead of trying to pass dist to CustomDist, I could try to provide random + logp directly.

Thanks again, I’ll post again once I solve the issue (or find more problems :sweat_smile:).

1 Like