-
-
Save YingGwan/aaf10d5a3b07078f3d8afd6ebcdf1c8f to your computer and use it in GitHub Desktop.
Get the jacobian of a vector-valued function that takes batch inputs, in pytorch.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
def get_batch_jacobian(net, x, noutputs):
print(f"Exact in: x shape is {x.shape}") #9000, 3
x = x.unsqueeze(1) # b, 1 ,in_dim #9000, 1, 3
print(f"in: x shape is {x.shape}")
n = x.size()[0]
print(f"in: n is {n}")
x = x.repeat(1, noutputs, 1) # b, out_dim, in_dim #9000, 2, 3
print(f"in: x shape is {x.shape}")
x.requires_grad_(True)
y = net(x)
input_val = torch.eye(noutputs).reshape(1,noutputs, noutputs).repeat(n, 1, 1)
print(f"input_val shape is {input_val.shape}")
y.backward(input_val)
return x.grad.data
class Net(torch.nn.Module):
def init(self, in_dim, out_dim):
super(Net, self).init()
self.fc1 = torch.nn.Linear(in_dim, out_dim)
batch = 9000
num_features = 3
num_outputs = 5
x = torch.randn(batch, num_features)
print(f"NEW: x shape is {x.shape}")
net = Net(num_features, num_outputs)
print(f"output dimension\n")
print(x.shape)
print(net(x).shape)
result = get_batch_jacobian(net, x, num_outputs)
print(result.shape)