Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pytorch Operation to detect NaNs

Tags:

pytorch

Is there a Pytorch-internal procedure to detect NaNs in Tensors? Tensorflow has the tf.is_nan and the tf.check_numerics operations ... Does Pytorch have something similar, somewhere? I could not find something like this in the docs...

I am looking specifically for a Pytorch internal routine, since I would like this to happen on the GPU as well as on the CPU. This excludes numpy - based solutions (like np.isnan(sometensor.numpy()).any()) ...

like image 650
cleros Avatar asked Jan 08 '18 21:01

cleros


People also ask

How can I tell if my torch tensor is NaN?

torch.isnan() in PyTorch returns True for the elements if the element is nan(not a number). Otherwise, it returns False.

How does PyTorch deal with NaN?

Official pytorch losses has a flag called reduce or something similar which allows to return the value of the loss for each element of the batch instead of the average. At that step you can simply remove the NaN element and do a manual average+backprop.

Is equal to NaN Python?

The math. isnan() method checks whether a value is NaN (Not a Number), or not. This method returns True if the specified value is a NaN, otherwise it returns False.

What is Torch Full?

The full form of TORCH is toxoplasmosis, rubella cytomegalovirus, herpes simplex, and HIV. However, it can also contain other newborn infections. Sometimes the test is spelled TORCHS, where the extra "S" stands for syphilis.


1 Answers

You can always leverage the fact that nan != nan:

>>> x = torch.tensor([1, 2, np.nan]) tensor([  1.,   2., nan.]) >>> x != x tensor([ 0,  0,  1], dtype=torch.uint8) 

With pytorch 0.4 there is also torch.isnan:

>>> torch.isnan(x) tensor([ 0,  0,  1], dtype=torch.uint8) 
like image 105
nemo Avatar answered Oct 17 '22 06:10

nemo