I want to get a non-negative, nonzero min value from the tensor. Can someone help me to do this in an easy and efficient way
Will this work?
>>> x = (torch.rand((1,1,4,4)) * 2) - 1 # creating a tensor with a mixture of positive and negative value
>>> x
tensor([[[[ 0.8101, -0.5538, 0.2049, -0.8969],
[-0.5394, -0.8587, -0.5657, 0.0481],
[-0.9651, 0.8118, -0.3242, -0.5439],
[ 0.0358, -0.1856, -0.1928, 0.7507]]]])
>>> min_x = torch.min(x[x>0]) # taking non-negative non-zero min value from the tensor
>>> min_x
tensor(0.0358)
I discovered a trick for this which avoids construction of a mask.
# how to determine the smallest positive value in the tensor x
# by shifting and taking inverse, we accomplish the following
# - zero maps to "negative infinity"
# - small positive numbers go to large positive numbers
# - negative numbers remain negative
small_shift = 0.001
invs = 1.0/(x - small_shift)
# undo the mapping
min_value = 1.0/invs.max().values() + small_shift