Calculating Jacobian in a Differentiable Way

Is there a general way calculate the Jacobian of a module, in a way that we retain the graph and can backprop through operations on the Jacobian as well?

Something similar to how WGAN operates but defined on more than a single output.

1 Like

Do you mean backward of backward (second derivative)?
try loss.backward(create_graph=True)


x = Variable(torch.randn(2, 2), requires_grad=True)
y = x.grad.mul(2).sum()
# This accumulates grad of grad into x.grad, adding together results of both backward() calls

Yes, but in this case the output is a single number, therefore you can calculate the differential in a straightforward way.

I’m wondering how you do this for multiple outputs. Here is an article describing the jacobian matrix I’m referring to:

I think this function is a general way to accomplish what I was saying. I borrowed this code from:

def jacobian(inputs, outputs):
    return torch.stack([grad([outputs[:, i].sum()], [inputs], retain_graph=True, create_graph=True)[0]
                        for i in range(outputs.size(1))], dim=-1)

Have you measured the running time of this method? One concern is that the for loop would make the backprop time grow linearly with the dimension of the output. I’m wondering whether there is way to directly get the Jacobian without the for loop.


The following code will do the trick with a single call to backward, taking advantage of when the function takes batched inputs. You can modify the script by passing retain_graph=True to backward and returning .grad instead of

1 Like