Let’s say I have the indices which I optained using locs = Train_y == 0

in numpy I can just do Train_negx = Train_x[np.invert(locs)]

but in pytorch it is not working, what is the equivalent to this method ? Knowing I’m working on output Variable.
I guess the problem is in the invert version

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.

Should I take indicies before or after converting the input into FloatTensor ?

if after should I just use locs = Train_y == 0 and Train_negx = Train_x[~locs] ?
if before should I convert locs into Tensor and which type of tensor ?

I started like this, when I used .sum() or torch.sum() it is giving me exactly the same sum and that’s why I start change the code. what couldbe the reason the sum is not equal to the same value ?

I wrote the same code before and after converting the input into Tensorfloat here is the output
locs0 = 270
locs1 = 242
after TensorFloat and calculating the sum
locs0 = 14
locs1 = 242

when taking the values Train_x[~locs] or Train_x[locs] it gives the correct answer. it is just the sum is wrong

So Train_y is originally a numpy array, right?
What dtype is it?

I think a torch.FloatTensor generally contains 32 bit floats, I’m guessing that the numpy array isn’t a 32 bit float, maybe the conversion is altering the precision a little.

Another point concerns the limitations of floating point precision which can be seen with this example

print(1 - .8 - .2)
# outputs -5.551115123125783e-17 on my machine

Train_y == 0 means exactly equal which is probably too precise when Train_y is some sort of floating point datatype. It might make sense to change Train_y == 0 into abs(Train_y) < epsilon where epsilon is chosen to be some really small value such as 1e-8.

Train_y is just classes 0 or 1. it is converted into TensorFloat just for the reason of cross entropy.
even though the sum is giving wrong output still indexing train_x on locs gives correct output.

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