I had the same issue. I guess with detach() function creates Tensor Y as the new alias of tensor X with requires_grad = False.
I solved this by Z = Y.clone() . Now changing the Z values won’t change X values.