Skip to content

Instantly share code, notes, and snippets.

View skmda37's full-sized avatar

Stefan Kolek skmda37

View GitHub Profile
@skmda37
skmda37 / torch_jacobian.py
Created May 21, 2019 19:04 — forked from sbarratt/torch_jacobian.py
Get the jacobian of a vector-valued function that takes batch inputs, in pytorch.
def get_jacobian(net, x, noutputs):
x = x.squeeze()
n = x.size()[0]
x = x.repeat(noutputs, 1)
x.requires_grad_(True)
y = net(x)
y.backward(torch.eye(noutputs))
return x.grad.data