*Memos:
-
My post explains the arithmetic operations with
nan
andinf
in PyTorch. -
My post explains the comparisons with
nan
andinf
in PyTorch. - My post explains isreal(), isnan() and isfinite().
- My post explains isinf(), isposinf() and isneginf().
- My post explains nan_to_num().
nan
:
- means Not A Number.
- is used for the undefined value of
float
orcomplex
type. - 's arithmetic operations are always
nan
: - 's comparisons(except
!=
) are alwaysFalse
. - exists but
-nan
doesn't exit.
inf
:
- means Infinity.
- is used when a
float
orcomplex
type value exceeds its maximum value. *If afloat
orcomplex
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])
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
toimag
argument otherwise the result will be different. -
real=
andimag=
can be removed. - There aren't
complex
type versons oftorch.nan
andtorch.inf
. -
complex
type ofnan
orinf
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)
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)
Top comments (0)