Skip to content

Compute jacobian #1149

Closed
Closed

Description

@roelofsmn

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?

Activity

  1. NiklasGustafsson commented on Nov 21, 2023

    @NiklasGustafsson

    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.

  2. GeorgeS2019 commented on Nov 21, 2023

    @GeorgeS2019
  3. roelofsmn commented on Nov 22, 2023

    @roelofsmn
    ContributorAuthor

    A small code example:

    var device = torch.cuda_is_available() ? torch.CPU : torch.CPU;
    
    torch.Tensor x1 = torch.tensor(new double[,] { { 1.0, 3.0 }, { 2.0, 4.0 } }, device: device, requires_grad: true);
    x1.retain_grad();
    
    torch.Tensor intercepts = torch.tensor(new double[] { 1.0, 5.0 }, device: device, requires_grad: true);
    
    torch.Tensor objectiveFunction(torch.Tensor x, torch.Tensor b)
    {
    
        return torch.einsum("ij,j->i", x, b);
    }
    var example = objectiveFunction(x1, intercepts);
    example[0].backward(retain_graph: true);
    Console.WriteLine(string.Join(", ", x1.grad().data<double>().ToArray()));
    
    x1.grad()?.zero_();
    example.grad()?.zero_();
    intercepts.grad()?.zero_();
    example[1].backward(retain_graph: true);
    Console.WriteLine(string.Join(", ", x1.grad().data<double>().ToArray()));
    

    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.

  4. roelofsmn commented on Nov 22, 2023

    @roelofsmn
    ContributorAuthor

    I also tried

    torch.Tensor x1 = torch.tensor(new double[,] { { 1.0, 3.0, 2.0 }, { 2.0, 4.0, 3.0 } }, device: device, requires_grad: true);
    x1.retain_grad();
    
    torch.Tensor intercepts = torch.tensor(new double[] { 1.0, 5.0, 7.0 }, device: device, requires_grad: true);
    
    torch.Tensor objectiveFunction(torch.Tensor x, torch.Tensor b)
    {
    
        return torch.einsum("ij,j->i", x, b);
    }
    var example = objectiveFunction(x1, intercepts);
    
    var jacobian = torch.autograd.grad(new torch.Tensor[] { example }, new torch.Tensor[] { intercepts }, new torch.Tensor[] { torch.tensor(new double[,] { { 1.0, 0.0 }, { 0.0, 1.0 } }) }, retain_graph: true);
    

    which should return a tensor with the same data as x1, but this code returns the sum of x1 along the rows (i.e. [3, 7, 5]).

  5. NiklasGustafsson commented on Nov 29, 2023

    @NiklasGustafsson

    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.

    https://pytorch.org/docs/stable/_modules/torch/autograd/functional.html#jacobian

  6. NiklasGustafsson commented on Jan 26, 2024

    @NiklasGustafsson

    How about Hessians? :-)

  7. roelofsmn commented on Jan 29, 2024

    @roelofsmn
    ContributorAuthor

    Sure, can implement those somewhere this week as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions