DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

Create `nan` and `inf` in PyTorch

Buy Me a Coffee

*Memos:

nan:

  • means Not A Number.
  • is used for the undefined value of float or complex type.
  • 's arithmetic operations are always nan:
  • 's comparisons(except !=) are always False.
  • exists but -nan doesn't exit.

inf:

  • means Infinity.
  • is used when a float or complex type value exceeds its maximum value. *If a float or complex type value exceeds its minimum value, the value is zero at last.

For example, nan and inf are produced by torch.reciprocal() in PyTorch as shown below:

import torch

my_tensor = torch.tensor([-2., -1., 0., 1., 2.])

print(torch.rsqrt(input=my_tensor))
# tensor([nan, nan, inf, 1.0000, 0.7071])

my_tensor = torch.tensor([-2.+0.j, -1.+0.j, 0.+0.j, 1.+0.j, 2.+0.j])

print(torch.rsqrt(input=my_tensor))
# tensor([0.0000-0.7071j, 0.0000-1.0000j, inf+nanj,
#         1.0000+0.0000j, 0.7071+0.0000j])
Enter fullscreen mode Exit fullscreen mode

Now, you can create nan and inf with torch.nan and torch.inf respectively in PyTorch as shown below:

*Memos:

  • Don't set the value with j to imag argument otherwise the result will be different.
  • real= and imag= can be removed.
  • There aren't complex type versons of torch.nan and torch.inf.
  • complex type of nan or inf can be created with complex() which is a Python's built-in function.
import torch

""" `float` type """
torch.nan # nan
-torch.nan # nan
torch.inf # inf
-torch.inf # -inf
type(torch.nan) # float
type(torch.inf) # float

""" `complex` type """
complex(real=torch.nan) # (nan+0j)
complex(real=torch.nan, imag=0) # (nan+0j)
complex(real=-torch.nan) # (nan+0j)
complex(real=-torch.nan, imag=0) # (nan+0j)
complex(real=torch.inf) # (inf+0j)
complex(real=torch.inf, imag=0) # (inf+0j)
complex(real=-torch.inf) # (-inf+0j)
complex(real=-torch.inf, imag=0) # (-inf+0j)
complex(real=torch.nan, imag=torch.nan) # (nan+nanj)
complex(real=torch.nan, imag=-torch.nan) # (nan+nanj)
complex(real=-torch.nan, imag=torch.nan) # (nan+nanj)
complex(real=-torch.nan, imag=-torch.nan) # (nan+nanj)
complex(real=torch.nan, imag=torch.inf) # (nan+infj)
complex(real=torch.nan, imag=-torch.inf) # (nan-infj)
complex(real=-torch.nan, imag=torch.inf) # (nan+infj)
complex(real=-torch.nan, imag=-torch.inf) # (nan-infj)
complex(real=torch.inf, imag=torch.nan) # (inf+nanj)
complex(real=torch.inf, imag=-torch.nan) # (inf+nanj)
complex(real=-torch.inf, imag=torch.nan) # (-inf+nanj)
complex(real=-torch.inf, imag=-torch.nan) # (-inf+nanj)
complex(real=torch.inf, imag=torch.inf) # (inf+infj)
complex(real=torch.inf, imag=-torch.inf) # (inf-infj)
complex(real=-torch.inf, imag=torch.inf) # (-inf+infj)
complex(real=-torch.inf, imag=-torch.inf) # (-inf-infj)
Enter fullscreen mode Exit fullscreen mode

In addition, you can create nan and inf with float() and complex() which are Python's built-in functions as shown below:

*Memos:

  • -nan doesn't exit.
  • real= can be removed.
import torch

""" `float` type """
float('nan') # nan
float('-nan') # nan
float('inf') # inf
float('infinity') # inf
float('-inf') # -inf
float('-infinity') # -inf

""" `complex` type """
complex(real='nan') # (nan+0j)
complex(real='nan+0j') # (nan+0j)
complex(real='-nan') # (nan+0j)
complex(real='-nan+0j') # (nan+0j)
complex(real='nan-0j') # (nan-0j)
complex(real='-nan-0j') # (nan-0j)
complex(real='inf') # (inf+0j)
complex(real='infinity') # (inf+0j)
complex(real='inf+0j') # (inf+0j)
complex(real='infinity+0j') # (inf+0j)
complex(real='-inf') # (-inf+0j)
complex(real='-infinity') # (-inf+0j)
complex(real='-inf+0j') # (-inf+0j)
complex(real='-infinity+0j') # (-inf+0j)
complex(real='nan+nanj') # (nan+nanj)
complex(real='nan-nanj') # (nan+nanj)
complex(real='-nan+nanj') # (nan+nanj)
complex(real='-nan-nanj') # (nan+nanj)
complex(real='nan+infj') # (nan+infj)
complex(real='nan+infinityj') # (nan+infj)
complex(real='nan-infj') # (nan-infj)
complex(real='nan-infinityj') # (nan-infj)
complex(real='-nan+infj') # (nan+infj)
complex(real='-nan+infinityj') # (nan+infj)
complex(real='-nan-infj') # (nan-infj)
complex(real='-nan-infinityj') # (nan-infj)
complex(real='inf+nanj') # (inf+nanj)
complex(real='infinity+nanj') # (inf+nanj)
complex(real='inf-nanj') # (inf+nanj)
complex(real='infinity-nanj') # (inf+nanj)
complex(real='-inf+nanj') # (-inf+nanj)
complex(real='-infinity+nanj') # (-inf+nanj)
complex(real='-inf-nanj') # (-inf+nanj)
complex(real='-infinity-nanj') # (-inf+nanj)
complex(real='inf+infj') # (inf+infj)
complex(real='infinity+infinityj') # (inf+infj)
complex(real='inf-infj') # (inf-infj)
complex(real='infinity-infinityj') # (inf-infj)
complex(real='-inf+infj') # (-inf+infj)
complex(real='-infinity+infinityj') # (-inf+infj)
complex(real='-inf-infj') # (-inf-infj)
complex(real='-infinity-infinityj') # (-inf-infj)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)