TianhongDai / integrated-gradient-pytorch

This is the pytorch implementation of the paper - Axiomatic Attribution for Deep Networks.
MIT License
180 stars 26 forks source link

Issue with gradient computation #1

Open anirbanl opened 5 years ago

anirbanl commented 5 years ago

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.
tumble-weed commented 4 years ago

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.