I am trying to compute a loss on the jacobian of the network (i.e. to perform double backprop), and I get the following error: RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation
I can't find the inplace operation in my code, so I don't know which line to fix.
*The error occurs in the last line:
loss3.backward()
inputs_reg = Variable(data, requires_grad=True)
output_reg = self.model.forward(inputs_reg)
num_classes = output.size()[1]
jacobian_list = []
grad_output = torch.zeros(*output_reg.size())
if inputs_reg.is_cuda:
grad_output = grad_output.cuda()
jacobian_list = jacobian.cuda()
for i in range(10):
zero_gradients(inputs_reg)
grad_output.zero_()
grad_output[:, i] = 1
jacobian_list.append(torch.autograd.grad(outputs=output_reg,
inputs=inputs_reg,
grad_outputs=grad_output,
only_inputs=True,
retain_graph=True,
create_graph=True)[0])
jacobian = torch.stack(jacobian_list, dim=0)
loss3 = jacobian.norm()
loss3.backward()
grad_output.zero_()
seems like an in-place operation. you might have in-place operations inself.model
. – Hanginggrad_output.zero_()
is the inplace operation. In PyTorch the inplace operations end with an underscore. I think you wanted to write `grad_output.zero_grad() – Rizika