Hi Codeflux!



 codeflux:

It appears that .backward does not seem to work with the MultivariateNormal function… when I use Normal instead for just one parameter, I can calculate the gradient.


I believe that MultivariateNormal and Normal have the same
behavior in this regard.
In general, you can’t differentiate or back propagate through calling
.sample() on a Distribution.  This is true for Normal, as well as
MultivariateNormal.  In contrast, you typically can backpropagate
through non-sampling methods such as .log_prob().
Here are some illustrative results:
>>> import torch
>>> torch.__version__
'1.9.0'
>>>
>>> meanN = torch.zeros (1, requires_grad = True)
>>> stdN = torch.ones (1, requires_grad = True)
>>> distN = torch.distributions.Normal (meanN, stdN)
>>>
>>> xN = distN.sample()
>>> xN.grad_fn
>>>
>>> yN = distN.log_prob (torch.zeros (1))
>>> yN.grad_fn
<SubBackward0 object at 0x00000188F9459CF8>
>>> yN.backward()
>>> meanN.grad
tensor([0.])
>>> stdN.grad
tensor([-1.])
>>>
>>> meanMV = torch.zeros (1, requires_grad = True)
>>> stdMV = torch.eye (1, requires_grad = True)
>>> distMV = torch.distributions.MultivariateNormal (meanMV, stdMV)
>>>
>>> xMV = distMV.sample()
>>> xMV.grad_fn
>>>
>>> yMV = distMV.log_prob (torch.zeros (1))
>>> yMV.grad_fn
<SubBackward0 object at 0x00000188FA12CA58>
>>> yMV.backward()
>>> meanMV.grad
tensor([0.])
>>> stdMV.grad
tensor([[-0.5000]])

Best.
K. Frank