This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)
*My post explains how to create nan
and inf
in PyTorch.
Arithmetic operations with torch.nan
and 100.0
, torch.nan
or torch.inf
as shown below:
*Memos:
- Arithmetic operations with
nan
are alwaysnan
: -
complex
type cannot do mod(modulo) calculation with%
.
import torch
""" `float` type """
torch.nan + 100.0 # nan
torch.nan + torch.nan # nan
torch.nan + -torch.nan # nan
torch.nan + torch.inf # nan
torch.nan + -torch.inf # nan
torch.nan - 100.0 # nan
torch.nan - torch.nan # nan
torch.nan - -torch.nan # nan
torch.nan - torch.inf # nan
torch.nan - -torch.inf # nan
torch.nan * 100.0 # nan
torch.nan * torch.nan # nan
torch.nan * -torch.nan # nan
torch.nan * torch.inf # nan
torch.nan * -torch.inf # nan
torch.nan / 100.0 # nan
torch.nan / torch.nan # nan
torch.nan / -torch.nan # nan
torch.nan / torch.inf # nan
torch.nan / -torch.inf # nan
torch.nan % 100.0 # nan
torch.nan % torch.nan # nan
torch.nan % -torch.nan # nan
torch.nan % torch.inf # nan
torch.nan % -torch.inf # nan
""" `complex` type """
complex(torch.nan, torch.nan) + (100.0+0.j) # (nan+nanj)
complex(torch.nan, torch.nan) + complex(torch.nan, torch.nan) # (nan+nanj)
complex(torch.nan, torch.nan) + complex(torch.nan, -torch.nan) # (nan+nanj)
complex(torch.nan, torch.nan) + complex(-torch.nan, torch.nan) # (nan+nanj)
complex(torch.nan, torch.nan) + complex(-torch.nan, -torch.nan) # (nan+nanj)
complex(torch.nan, torch.nan) + complex(torch.inf, torch.inf) # (nan+nanj)
complex(torch.nan, torch.nan) + complex(torch.inf, -torch.inf) # (nan+nanj)
complex(torch.nan, torch.nan) + complex(-torch.inf, torch.inf) # (nan+nanj)
complex(torch.nan, torch.nan) + complex(-torch.inf, -torch.inf) # (nan+nanj)
complex(torch.nan, torch.nan) - (100.0+0.j) # (nan+nanj)
complex(torch.nan, torch.nan) - complex(torch.nan, torch.nan) # (nan+nanj)
complex(torch.nan, torch.nan) - complex(torch.nan, -torch.nan) # (nan+nanj)
complex(torch.nan, torch.nan) - complex(-torch.nan, torch.nan) # (nan+nanj)
complex(torch.nan, torch.nan) - complex(-torch.nan, -torch.nan) # (nan+nanj)
complex(torch.nan, torch.nan) - complex(torch.inf, torch.inf) # (nan+nanj)
complex(torch.nan, torch.nan) - complex(torch.inf, -torch.inf) # (nan+nanj)
complex(torch.nan, torch.nan) - complex(-torch.inf, torch.inf) # (nan+nanj)
complex(torch.nan, torch.nan) - complex(-torch.inf, -torch.inf) # (nan+nanj)
complex(torch.nan, torch.nan) * (100.0+0.j) # (nan+nanj)
complex(torch.nan, torch.nan) * complex(torch.nan, torch.nan) # (nan+nanj)
complex(torch.nan, torch.nan) * complex(torch.nan, -torch.nan) # (nan+nanj)
complex(torch.nan, torch.nan) * complex(-torch.nan, torch.nan) # (nan+nanj)
complex(torch.nan, torch.nan) * complex(-torch.nan, -torch.nan) # (nan+nanj)
complex(torch.nan, torch.nan) * complex(torch.inf, torch.inf) # (nan+nanj)
complex(torch.nan, torch.nan) * complex(torch.inf, -torch.inf) # (nan+nanj)
complex(torch.nan, torch.nan) * complex(-torch.inf, torch.inf) # (nan+nanj)
complex(torch.nan, torch.nan) * complex(-torch.inf, -torch.inf) # (nan+nanj)
complex(torch.nan, torch.nan) / (100.0+0.j) # (nan+nanj)
complex(torch.nan, torch.nan) / complex(torch.nan, torch.nan) # (nan+nanj)
complex(torch.nan, torch.nan) / complex(torch.nan, -torch.nan) # (nan+nanj)
complex(torch.nan, torch.nan) / complex(-torch.nan, torch.nan) # (nan+nanj)
complex(torch.nan, torch.nan) / complex(-torch.nan, -torch.nan) # (nan+nanj)
complex(torch.nan, torch.nan) / complex(torch.inf, torch.inf) # (nan+nanj)
complex(torch.nan, torch.nan) / complex(torch.inf, -torch.inf) # (nan+nanj)
complex(torch.nan, torch.nan) / complex(-torch.inf, torch.inf) # (nan+nanj)
complex(torch.nan, torch.nan) / complex(-torch.inf, -torch.inf) # (nan+nanj)
Arithmetic operations with torch.inf
and 100.0
or torch.inf
as shown below. *complex
type cannot do mod(modulo) calculation with %
:
import torch
""" `float` type """
torch.inf + 100.0 # inf
torch.inf + torch.inf # inf
torch.inf + -torch.inf # nan
torch.inf - 100.0 # inf
torch.inf - torch.inf # nan
torch.inf - -torch.inf # inf
torch.inf * 100.0 # inf
torch.inf * torch.inf # inf
torch.inf * -torch.inf # -inf
torch.inf / 100.0 # inf
torch.inf / torch.inf # nan
torch.inf / -torch.inf # nan
torch.inf % 100.0 # nan
torch.inf % torch.inf # nan
torch.inf % -torch.inf # nan
""" `complex` type """
complex(torch.inf, torch.inf) + (100.0+0.j) # (inf+infj)
complex(torch.inf, torch.inf) + complex(torch.inf, torch.inf) # (inf+infj)
complex(torch.inf, torch.inf) + complex(torch.inf, -torch.inf) # (inf+nanj)
complex(torch.inf, torch.inf) + complex(-torch.inf, torch.inf) # (nan+infj)
complex(torch.inf, torch.inf) + complex(-torch.inf, -torch.inf) # (nan+nanj)
complex(torch.inf, torch.inf) - (100.0+0.j) # (inf+infj)
complex(torch.inf, torch.inf) - complex(torch.inf, torch.inf) # (nan+nanj)
complex(torch.inf, torch.inf) - complex(torch.inf, -torch.inf) # (nan+infj)
complex(torch.inf, torch.inf) - complex(-torch.inf, torch.inf) # (inf+nanj)
complex(torch.inf, torch.inf) - complex(-torch.inf, -torch.inf) # (inf+infj)
complex(torch.inf, torch.inf) * (100.0+0.j) # (nan+nanj)
complex(torch.inf, torch.inf) * complex(torch.inf, torch.inf) # (nan+infj)
complex(torch.inf, torch.inf) * complex(torch.inf, -torch.inf) # (inf+nanj)
complex(torch.inf, torch.inf) * complex(-torch.inf, torch.inf) # (-inf+nanj)
complex(torch.inf, torch.inf) * complex(-torch.inf, -torch.inf) # (nan-infj)
complex(torch.inf, torch.inf) / (100.0+0.j) # (nan+nanj)
complex(torch.inf, torch.inf) / complex(torch.inf, torch.inf) # (nan+nanj)
complex(torch.inf, torch.inf) / complex(torch.inf, -torch.inf) # (nan+nanj)
complex(torch.inf, torch.inf) / complex(-torch.inf, torch.inf) # (nan+nanj)
complex(torch.inf, torch.inf) / complex(-torch.inf, -torch.inf) # (nan+nanj)
Additionally, arithmetic operations with nan
and 100.0
, nan
or inf
with float() and complex() which are Python's built-in functions as shown below:
*Memos:
- Arithmetic operations with
nan
are alwaysnan
: -
complex
type cannot do mod(modulo) calculation with%
.
import torch
""" `float` type """
float('nan') + 100.0 # nan
float('nan') + float('nan') # nan
float('nan') + float('-nan') # nan
float('nan') + float('inf') # nan
float('nan') + float('-inf') # nan
float('nan') - 100.0 # nan
float('nan') - float('nan') # nan
float('nan') - float('-nan') # nan
float('nan') - float('inf') # nan
float('nan') - float('-inf') # nan
float('nan') * 100.0 # nan
float('nan') * float('nan') # nan
float('nan') * float('-nan') # nan
float('nan') * float('inf') # nan
float('nan') * float('-inf') # nan
float('nan') / 100.0 # nan
float('nan') / float('nan') # nan
float('nan') / float('-nan') # nan
float('nan') / float('inf') # nan
float('nan') / float('-inf') # nan
float('nan') % 100.0 # nan
float('nan') % float('nan') # nan
float('nan') % float('-nan') # nan
float('nan') % float('inf') # nan
float('nan') % float('-inf') # nan
""" `complex` type """
complex('nan+nanj') + (100.0+0.j) # (nan+nanj)
complex('nan+nanj') + complex('nan+nanj') # (nan+nanj)
complex('nan+nanj') + complex('nan-nanj') # (nan+nanj)
complex('nan+nanj') + complex('-nan+nanj') # (nan+nanj)
complex('nan+nanj') + complex('-nan-nanj') # (nan+nanj)
complex('nan+nanj') + complex('inf+infj') # (nan+nanj)
complex('nan+nanj') + complex('inf-infj') # (nan+nanj)
complex('nan+nanj') + complex('-inf+infj') # (nan+nanj)
complex('nan+nanj') + complex('-inf-infj') # (nan+nanj)
complex('nan+nanj') - (100.0+0.j) # (nan+nanj)
complex('nan+nanj') - complex('nan+nanj') # (nan+nanj)
complex('nan+nanj') - complex('nan-nanj') # (nan+nanj)
complex('nan+nanj') - complex('-nan+nanj') # (nan+nanj)
complex('nan+nanj') - complex('-nan-nanj') # (nan+nanj)
complex('nan+nanj') - complex('inf+infj') # (nan+nanj)
complex('nan+nanj') - complex('inf-infj') # (nan+nanj)
complex('nan+nanj') - complex('-inf+infj') # (nan+nanj)
complex('nan+nanj') - complex('-inf-infj') # (nan+nanj)
complex('nan+nanj') * (100.0+0.j) # (nan+nanj)
complex('nan+nanj') * complex('nan+nanj') # (nan+nanj)
complex('nan+nanj') * complex('nan-nanj') # (nan+nanj)
complex('nan+nanj') * complex('-nan+nanj') # (nan+nanj)
complex('nan+nanj') * complex('-nan-nanj') # (nan+nanj)
complex('nan+nanj') * complex('inf+infj') # (nan+nanj)
complex('nan+nanj') * complex('inf-infj') # (nan+nanj)
complex('nan+nanj') * complex('-inf+infj') # (nan+nanj)
complex('nan+nanj') * complex('-inf-infj') # (nan+nanj)
complex('nan+nanj') / (100.0+0.j) # (nan+nanj)
complex('nan+nanj') / complex('nan+nanj') # (nan+nanj)
complex('nan+nanj') / complex('nan-nanj') # (nan+nanj)
complex('nan+nanj') / complex('-nan+nanj') # (nan+nanj)
complex('nan+nanj') / complex('-nan-nanj') # (nan+nanj)
complex('nan+nanj') / complex('inf+infj') # (nan+nanj)
complex('nan+nanj') / complex('inf-infj') # (nan+nanj)
complex('nan+nanj') / complex('-inf+infj') # (nan+nanj)
complex('nan+nanj') / complex('-inf-infj') # (nan+nanj)
Additionally, arithmetic operations with inf
and 100.0
or inf
with float() and complex() which are Python's built-in functions as shown below. *complex
type cannot do mod(modulo) calculation with %
:
import torch
""" `float` type """
float('inf') + 100.0 # inf
float('inf') + float('inf') # inf
float('inf') + float('-inf') # nan
float('inf') - 100.0 # inf
float('inf') - float('inf') # nan
float('inf') - float('-inf') # inf
float('inf') * 100.0 # inf
float('inf') * float('inf') # inf
float('inf') * float('-inf') # -inf
float('inf') / 100.0 # inf
float('inf') / float('inf') # nan
float('inf') / float('-inf') # nan
float('inf') % 100.0 # nan
float('inf') % float('inf') # nan
float('inf') % float('-inf') # nan
""" `complex` type """
complex('inf+infj') + (100.0+0.j) # (inf+infj)
complex('inf+infj') + complex('inf+infj') # (inf+infj)
complex('inf+infj') + complex('inf-infj') # (inf+nanj)
complex('inf+infj') + complex('-inf+infj') # (nan+infj)
complex('inf+infj') + complex('-inf-infj') # (nan+nanj)
complex('inf+infj') - (100.0+0.j) # (inf+infj)
complex('inf+infj') - complex('inf+infj') # (nan+nanj)
complex('inf+infj') - complex('inf-infj') # (nan+infj)
complex('inf+infj') - complex('-inf+infj') # (inf+nanj)
complex('inf+infj') - complex('-inf-infj') # (inf+infj)
complex('inf+infj') * (100.0+0.j) # (nan+nanj)
complex('inf+infj') * complex('inf+infj') # (nan+infj)
complex('inf+infj') * complex('inf-infj') # (inf+nanj)
complex('inf+infj') * complex('-inf+infj') # (-inf+nanj)
complex('inf+infj') * complex('-inf-infj') # (nan-infj)
complex('inf+infj') / (100.0+0.j) # (nan+nanj)
complex('inf+infj') / complex('inf+infj') # (nan+nanj)
complex('inf+infj') / complex('inf-infj') # (nan+nanj)
complex('inf+infj') / complex('-inf+infj') # (nan+nanj)
complex('inf+infj') / complex('-inf-infj') # (nan+nanj)
This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito) | Sciencx (2024-07-22T01:03:17+00:00) Arithmetic operations with `nan` and `inf` in PyTorch. Retrieved from https://www.scien.cx/2024/07/22/arithmetic-operations-with-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.