Hi.
I try to adapt code which was written on pytorch 0.4.0 to a later version of torch 1.7.1,
And in the old code I had:
grad_target = (output_cl * label).sum()
grad_target.backward(gradient=label * output_cl, retain_graph=True)
But in my new pytorch version it complains about the fact that grad_target has dimension () and label*output_cl is a vector.
Here is the full traceback:
Traceback (most recent call last):
File “C:\Program Files\JetBrains\PyCharm 2021.1\plugins\python\helpers\pydev\pydevd.py”, line 1483, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
File “C:\Program Files\JetBrains\PyCharm 2021.1\plugins\python\helpers\pydev_pydev_imps_pydev_execfile.py”, line 18, in execfile
exec(compile(contents+“\n”, file, ‘exec’), glob, loc)
File “C:/Users/Student1/PycharmProjects/new/main_wrapper.py”, line 42, in
args.func(args)
File “C:\Users\Student1\PycharmProjects\new\main.py”, line 99, in train_handler
num_heatmaps=args.heatmaps_per_test)
File “C:\Users\Student1\PycharmProjects\new\gain.py”, line 369, in train
am_mask=sample[‘label/masks’])
File “C:\Users\Student1\PycharmProjects\new\gain.py”, line 317, in forward
return self._forward(data, label, extra_super, am_mask)
File “C:\Users\Student1\PycharmProjects\new\gain.py”, line 531, in forward
output_cl, loss_cl, gcams = self.attention_map_forward(data, labels)
File “C:\Users\Student1\PycharmProjects\new\gain.py”, line 489, in attention_map_forward
grad_target.backward(gradient=label * output_cl, retain_graph=True)
File “C:\Users\Student1\anaconda3\envs\new\lib\site-packages\torch\tensor.py”, line 221, in backward
torch.autograd.backward(self, gradient, retain_graph, create_graph)
File "C:\Users\Student1\anaconda3\envs\new\lib\site-packages\torch\autograd_init.py", line 126, in backward
grad_tensors = make_grads(tensors, grad_tensors)
File "C:\Users\Student1\anaconda3\envs\new\lib\site-packages\torch\autograd_init.py", line 37, in _make_grads
+ str(out.shape) + “.”)
RuntimeError: Mismatch in shape: grad_output[0] has a shape of torch.Size([1, 20]) and output[0] has a shape of torch.Size().
How can I change it preserving the original logic?
I wonder if I should just remove the gradient argument.
Thank you.