I’m writing a custom loss function, and am currently debugging with gradcheck(), and it throws this error
RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.FloatTensor []], which is output 0 of SelectBackward, is at version 9; expected version 8 instead. Hint: the backtrace further above shows the operation that failed to compute its gradient. The variable in question was changed in there or anywhere later. Good luck!

I understand what the error is. Turning on the anomaly detection, it pointed me to this piece of code:
if XYZ[2] > 0.008856:
    XYZ[2] = XYZ[2] ** 1/3
else:
    XYZ[2] = (7.787 * value) + (16 / 116)

How would I correctly rewrite this to make it not in place but also not introduce an overhead? Since this is going to be part of my loss function I require it to be efficient if possible. Thanks for any comments!