I've drawn samples from a multivariate normal distribution and would like to get the gradient of their log probability with respect to the mean. Since there are many samples, this requires a Jacobian:
import torch
mu = torch.ones((2,), requires_grad=True)
sigma = torch.eye(2)
dist = torch.distributions.multivariate_normal.MultivariateNormal(mu, sigma)
num_samples=10
samples = dist.sample((num_samples,))
logprobs = dist.log_prob(samples)
Now I would like to get the derivative of each entry in logprobs
with respect to each entry in mu
.
A simple solution is a python loop:
grads = []
for logprob in logprobs:
grad = torch.autograd.grad(logprob, mu, retain_graph=True)
grads.append(grad)
If you stack the grads, the result is the desired Jacobian. Is there also built-in and vectorized support for this?
Related questions/internet ressources:
This is a huge topic, there are lots of related posts. Nevertheless, I think that this specific question (regarding distributions) hasn't been answered yet:
This question is basically the same as mine (but without example code and solution attempt), sadly it is unanswered: Pytorch custom function jacobian gradient
This question shows the calculation of a jacobian in pytorch, but I don't think the solution is applicable to my problem: Pytorch most efficient Jacobian/Hessian calculation It requires stacking the input in a way that seems incompatible with the distribution. I couldn't make it work.
This gist has some code snippets for Jacobians. In principle they are similar to the approach from the question above.