# 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)
x.mul(2).sum().backward(create_graph=True)
y.backward()
``````

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: https://en.wikipedia.org/wiki/Jacobian_matrix_and_determinant

I think this function is a general way to accomplish what I was saying. I borrowed this code from: https://github.com/pytorch/pytorch/blob/85a7e0f/test/test_distributions.py#L1501

``````def jacobian(inputs, outputs):