where output is a tuple. In order to modify this output, I use the code above. Note how I create a new variable as I cannot directly modify the first element of the tuple directly.
The thing is, when calculating the gradients by using .backward(), the following error jumps:
RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [32, 256, 75, 17]], which is output 0 of ReluBackward0, is at version 2; expected version 1 instead. Hint: enable anomaly detection to find the operation that failed to compute its gradient, with torch.autograd.set_detect_anomaly(True).
I wonder how would I correctly modify the output of a layer’s hook accordingly.
The error message is indicating that there is an in-place modification of a tensor in the input or output of a layer, which is preventing the calculation of gradients during backpropagation.
To fix this issue, you can create a copy of the output tensor and modify the copy instead of modifying the original tensor in-place. Can you try below code? or Resolved reference link
Because the output argument is of type tuple (and not tensor), I cannot use the .clone() method directly. So, what I just did, while seeming rudimentary, works by simply making the following intermediate steps like so: