This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)
You can create nan
and inf
with torch.nan
and torch.inf
respectively in PyTorch as shown below:
*Memos:
-
-nan
doesn't exit. -
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=0j) # (nan+0j)
complex(real=torch.nan, imag=0) # (nan+0j)
complex(real=-torch.nan) # (nan+0j)
complex(real=-torch.nan, imag=0j) # (nan+0j)
complex(real=-torch.nan, imag=0) # (nan+0j)
complex(real=torch.inf) # (inf+0j)
complex(real=torch.inf, imag=0j) # (inf+0j)
complex(real=torch.inf, imag=0) # (inf+0j)
complex(real=-torch.inf) # (-inf+0j)
complex(real=-torch.inf, imag=0j) # (-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)
This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito) | Sciencx (2024-07-21T02:58:54+00:00) Create `nan` and `inf` in PyTorch. Retrieved from https://www.scien.cx/2024/07/21/create-nan-and-inf-in-pytorch/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.