Create `nan` and `inf` in PyTorch

Buy Me a Coffee☕

You can create nan and inf with torch.nan and torch.inf respectively in PyTorch as shown below:

*Memos:

-nan doesn’t exit.

real= and imag= can be removed.
There aren’t complex type versons of torch.nan and torch.inf.

complex typ…


This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)

Buy Me a Coffee

You can create nan and inf with torch.nan and torch.inf respectively in PyTorch as shown below:

*Memos:

  • -nan doesn't exit.
  • 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=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)


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » Create `nan` and `inf` in PyTorch." Super Kai (Kazuya Ito) | Sciencx - Sunday July 21, 2024, https://www.scien.cx/2024/07/21/create-nan-and-inf-in-pytorch/
HARVARD
Super Kai (Kazuya Ito) | Sciencx Sunday July 21, 2024 » Create `nan` and `inf` in PyTorch., viewed ,<https://www.scien.cx/2024/07/21/create-nan-and-inf-in-pytorch/>
VANCOUVER
Super Kai (Kazuya Ito) | Sciencx - » Create `nan` and `inf` in PyTorch. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/21/create-nan-and-inf-in-pytorch/
CHICAGO
" » Create `nan` and `inf` in PyTorch." Super Kai (Kazuya Ito) | Sciencx - Accessed . https://www.scien.cx/2024/07/21/create-nan-and-inf-in-pytorch/
IEEE
" » Create `nan` and `inf` in PyTorch." Super Kai (Kazuya Ito) | Sciencx [Online]. Available: https://www.scien.cx/2024/07/21/create-nan-and-inf-in-pytorch/. [Accessed: ]
rf:citation
» Create `nan` and `inf` in PyTorch | Super Kai (Kazuya Ito) | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.