You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a Module that computes a Tensor vector from a Tensor vector. I wish to compute the gradient of each output element with respect to each input element. In the Pytorch documentation, they show this can be done through the use of torch.autograd.functional.jacobian (see torch.autograd.functional.jacobian). I'm not sure the same can be done using TorchSharp.
I tried looping over the elements of the output tensor and doing a backward call:
for (int i = 0; i < output.Length; i++)
output[i].backward(create_graph: false, retain_graph: true);
But even with retain_graph=true that results in the error:
System.Runtime.InteropServices.ExternalException: 'Trying to backward through the graph a second time (or directly access saved tensors after they have already been freed). Saved intermediate values of the graph are freed when you call .backward() or autograd.grad(). Specify retain_graph=True if you need to backward through the graph a second time or if you need to access saved tensors after calling backward.
Is there currently already a way to compute the jacobian of a function/module in TorchSharp? If not, can you suggest how I could get started on adding support for the above Pytorch functionality?
It's hard to say why you are getting the error without more code, but I believe that you're getting it because you're not calling zero_grad() after a first back-prop run.
I couldn't find anything in the native APIs for it, and it seems like it's all implemented in Python for PyTorch. It will take some time to figure out, but does not seem impossibly hard.
The first call of backward works like a charm, but the second fails. As you can see, using just a function of the input tensors, I cannot call zero_grad() anywhere, so I don't really know how to reset the gradients.
If not, can you suggest how I could get started on adding support for the above Pytorch functionality?
Hey, if you want to contribute the implementation, that would be awesome. The PyTorch jacobian and hessian functionality is implemented in Python, it seems, so that should be straight-forward (but it would take a while -- lots of code to translate). If not, I'll get to it sometime in December, probably.
Activity
NiklasGustafsson commentedon Nov 21, 2023
It's hard to say why you are getting the error without more code, but I believe that you're getting it because you're not calling
zero_grad()after a first back-prop run.I couldn't find anything in the native APIs for it, and it seems like it's all implemented in Python for PyTorch. It will take some time to figure out, but does not seem impossibly hard.
GeorgeS2019 commentedon Nov 21, 2023
Source
roelofsmn commentedon Nov 22, 2023
A small code example:
The first call of
backwardworks like a charm, but the second fails. As you can see, using just a function of the input tensors, I cannot callzero_grad()anywhere, so I don't really know how to reset the gradients.roelofsmn commentedon Nov 22, 2023
I also tried
which should return a tensor with the same data as
x1, but this code returns the sum ofx1along the rows (i.e. [3, 7, 5]).NiklasGustafsson commentedon Nov 29, 2023
Hey, if you want to contribute the implementation, that would be awesome. The PyTorch jacobian and hessian functionality is implemented in Python, it seems, so that should be straight-forward (but it would take a while -- lots of code to translate). If not, I'll get to it sometime in December, probably.
https://pytorch.org/docs/stable/_modules/torch/autograd/functional.html#jacobian
NiklasGustafsson commentedon Jan 26, 2024
How about Hessians? :-)
roelofsmn commentedon Jan 29, 2024
Sure, can implement those somewhere this week as well.