How to select elements in tensor greater than and keep gradient?

X is tensor of dimension (d1,d2,x,x)

how to select elements in the (x,x) if the are greater than a constant c? while keeping the gradient as well ?


I’m using this way but it is taking the sum of all the 2D matrix instead of what is greater only

number of elements that are greater then zero = x5.view(self.batchsize, -1).ge(0).sum(1).float()
sum of all elements divided by the number of grater than zero = x5.view(self.batchsize, -1).sum(1).div(g)

I want this term. the average of the number greater than zero in dimension 1


To get all the elements greater that a value, you can use this: x[x > my_val].


when I do this part
x5.view(self.batchsize, -1)
I’ll have a tensor of size (batchsize,100)
if I apply your version I’ll have a tensor of one dimension.
I still need the sum of the value of the second dimension which means I want to have (batchsize,1)

1 Like

you can use or torch.le() . it results in a tensor with the same dimension as the input tensor then you can do what you want.

Hi, but does this operation keep the gradient flowing?


The gradients for all the elements that were not masked will flow properly. For the masked elements, their gradients will be set to 0.