integrated-gradient-pytorch icon indicating copy to clipboard operation
integrated-gradient-pytorch copied to clipboard

Issue with gradient computation

Open anirbanl opened this issue 6 years ago • 1 comments

On running the following line of code in main.py, i am getting RuntimeError: gradients, label_index = calculate_outputs_and_gradients([img], model, None, args.cuda)

Error trace:

RuntimeError Traceback (most recent call last) in () 5 img = img[:, :, (2, 1, 0)] 6 # calculate the gradient and the label index ----> 7 gradients, label_index = calculate_outputs_and_gradients([img], model, None, args.cuda)

/dccstor/cssblr/anirban/integrated-gradient-pytorch/utils.pyc in calculate_outputs_and_gradients(inputs, model, target_label_idx, cuda) 21 # clear grad 22 model.zero_grad() ---> 23 output.backward() 24 gradient = input.grad.detach().cpu().numpy()[0] 25 gradients.append(gradient)

/dccstor/anirlaha1/deep/apr2018/lib/python2.7/site-packages/torch/tensor.pyc in backward(self, gradient, retain_graph, create_graph) 100 products. Defaults to False. 101 """ --> 102 torch.autograd.backward(self, gradient, retain_graph, create_graph) 103 104 def register_hook(self, hook):

/dccstor/anirlaha1/deep/apr2018/lib/python2.7/site-packages/torch/autograd/init.pyc in backward(tensors, grad_tensors, retain_graph, create_graph, grad_variables) 88 Variable._execution_engine.run_backward( 89 tensors, grad_tensors, retain_graph, create_graph, ---> 90 allow_unreachable=True) # allow_unreachable flag 91 92

RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation

Please help. Thanks.

anirbanl avatar Feb 11 '19 08:02 anirbanl

faced same problem, changing the architecture to resnet152 worked: python main.py --cuda --img='01.jpg' --model-type resnet152

so might have something to do with the inception architecture.

tumble-weed avatar Nov 21 '19 13:11 tumble-weed