Values replacements in custom defined loss throw error


I am using a custom defined loss function to implement a model I have read in a paper which requires me change all pixels in the output of the NN which do not meet a condition.

This command (in the Loss function) throws an error:
output[target != 1] = 1

The error I get is “one of the variables needed for gradient computation has been modified by an inplace operation”

Any suggestion to solve this?
Thanks for helping

UPDATE: Giving a second thought, this makes sense: in the Loss I am changing the output. I should rather create a clone and operating on it…I do not get the error anymore. I am not sure if it works. Will let you know for collective knowledge