Train_y == 0 doesn’t return the indices of the matching elements, it returns a tensor full of zeros and ones indicating which elements match the condition.
np.invert(locs) transforms the zeros to 255, and the ones to 254. The docs say that np.invert is a bitwise negation. Now in numpy locs would be an array of booleans (i.e. one bit per element) whereas in pytorch locs is a tensor of bytes (i.e. 8 bits per element). That is why using np.invert pytorch doesn’t work as expected.
Besides it is unwise to use numpy operations on Variables. Use the pytorch tensor ops where possible.
Train_negx = Train_x[~locs] should do what you want.