Just a thought. If Train_y contains only zeros and ones, then if you set locs = Train_y == 0 then Train_y should be equal to ~locs excepting differences of type.
You should run zero_grad after calling optimizer.step and before the next call to .backward