Created
March 25, 2020 16:02
-
-
Save jotterbach/3d5b49c20de000a84bb5b97b2ecc4ec5 to your computer and use it in GitHub Desktop.
Compute the jacobian of some output w.r.t. some inputs. This can be really useful in debugging autoregressive models
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
| from torch.autograd.gradcheck import zero_gradients | |
| def compute_jacobian(inputs, output): | |
| """ | |
| :param inputs: Batch X Size (e.g. Depth X Width X Height) | |
| :param output: Batch X Classes | |
| :return: jacobian: Batch X Classes X Size | |
| """ | |
| assert inputs.requires_grad | |
| num_classes = output.size()[1] | |
| jacobian = torch.zeros(num_classes, *inputs.size()) | |
| grad_output = torch.zeros(*output.size()) | |
| if inputs.is_cuda: | |
| grad_output = grad_output.cuda() | |
| jacobian = jacobian.cuda() | |
| for i in range(num_classes): | |
| zero_gradients(inputs) | |
| grad_output.zero_() | |
| grad_output[:, i] = 1 | |
| output.backward(grad_output, retain_graph=True) | |
| jacobian[i] = inputs.grad.data | |
| return torch.transpose(jacobian, dim0=0, dim1=1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment