How to slove the Runtime Error:in-place operations can be only used on variables that don't share storage with any other variables, but detected that there are 3 objects sharing it

RuntimeError: in-place operations can be only used on variables that don’t share storage with any other variables, but detected that there are 3 objects sharing it
I have this problem when I tried to run code below.

        x = coordinates[:, 0, :, :]
        y = coordinates[:, 1, :, :]
        x[x < 0] = 0

I don’t really get wthat is wrong.Could anybody help me?
]Thanks very much.

When you do x = coordinates[:, 0, :, :], then x actually shares the content of the coordinates tensor.
But then you try to do x[x < 0] = 0 which tries to change x in-place. But since it shares it’s tensor with coordinates (and y), you are not allowed to do that because you may loose some informations needed to do the backpropagation.

Thanks for replying me.I have tried to convert x to DoubleTensor,it can works by this way.But I don’t know whether it’s right .Could you tell me some way to remove the sharing ?

To remove the sharing you should use the .clone() function.

1 Like

Thank you very much.:joy: