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

I’m trying to visualize the feature maps of my network through guided backpropagation. I’m running this through model.eval().

My first error, upon the final stages, is RuntimeError: invalid gradient at index 0 - got [1, 6] but expected shape compatible with [1, 10647, 6]. It seems this stems from:

onehot_out = torch.FloatTensor(1, model_out.size()[-1]).zero_()

so I fix it with:

onehot_out = torch.FloatTensor(1, model_out.size()[1], model_out.size()[-1]).zero_()

This then seems to work, however when I then do the backward pass:

onehot_out[0][target_class] = 1
model_out.backward(gradient = onehot_out)

I get the error present in the title:

RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.FloatTensor [1, 3, 52, 52, 2]], which is output 0 of SigmoidBackward, is a
t version 6; expected version 5 instead. Hint: enable anomaly detection to find the operation that failed to compute its gradient, with torch.autograd.set_detect_anomaly(True)

I’m unsure what this means or where to start looking. Would appreciate any help.

Hi,

The best way to look at these issues is to enable the anomaly detection mode. This will give you the method in the forward that caused this issue.

Hi,

Thank you yes. I enabled it shortly after posting this and solved my problem.

Shall I close or delete this thread?

You can simply accept the answer of “enable anomaly mode” and leave it like that.
Nice that you found the problem !