Emulate fmin in torch 1.7.1

Viewed 35

In the current version there exists torch.fmin Torch Documentation fimin

Unfortunately, my project relies on torch 1.7.1 and I can't upgrade. Is there another way to use min with NaNs in my tensor to emulate fmin. The NaNs are intended and are not a result of poor implementation. So I would like to keep them.

1 Answers

It's ugly but this works:

>>> a = torch.tensor([2.2, float('nan'), 2.1, float('nan')])
>>> b = torch.tensor([-9.3, 0.1, float('nan'), float('nan')])
>>> c = torch.stack((a,b))
>>> c[c.isnan()] = float('inf')
>>> min_, idx = torch.min(c, dim=0)
>>> min_[min_.isinf()] = float('nan')
>>> min_
tensor([-9.3000,  0.1000,  2.1000,     nan])
Related