Doing y = x was a good idea to prevent changing the input inplace. Unfortunately, doing it that way leads to x and y begin the same tensors and so you keep modifying the input inplace.
Try to replace it with y = x.clone().