torch.distributions.Categorical

Hi,

Is there any C++ API with same functionality as torch.distributions.Categorical?
I found torch::::multinomial that has same input parameters (const Tensor &self, int64_t num_samples, bool replacement = false, Generator *generator = nullptr), but it does not have sample or log_prob APIs.

Thanks,
Afshin

1 Like

Bump this… It would be nice if someone can answer this question.

Hi, we don’t have c++ apis for distributions at this point. But we do plan to add distributions support in cpp in the future.

3 Likes

Hi @glaringlee,

Thank you for clarifying this.

Separately from this topic, but somewhat related, the implementation for torch.distributions.Normal appears to be here: normal.py. In the sample() function, on line 63, there is a call to torch.normal(). I have not been able to find the implementation for it. Do you think you could provide a link to the file defining torch.normal()?

@cruzas
That’s a binding function which actually pointing to our c++ implementation, entry point is here:
https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/native/Distributions.cpp#L286
The cpu implementation is here:
https://github.com/pytorch/pytorch/blob/5ab36ec98ba28b61ac3be57427d67ad8e1f0bab4/aten/src/ATen/native/cpu/DistributionTemplates.h#L173
The gpu implementation is here:
https://github.com/pytorch/pytorch/blob/34523b70c1369024f805a0480bc39a5f21e64a03/aten/src/ATen/native/cuda/DistributionTemplates.h#L407

1 Like

Hi @glaringlee,

Thank you very much, seeing the implementation was very useful for me.
Would you also be able to point me to the implementation of the .log_prob() implementation for the normal distribution?

I assumed it is computed as the logarithm of the probability density function. I would like to clarify if that’s the case.

Thank you for your help.

Hi @glaringlee,

Now that I remember, I had actually found the implementation for .log_prob() some time ago here.

Thank you!

Are now any of sample or log_prob APIs implemented?