Super Kai (Kazuya Ito)
Posted on July 18, 2024
*Memos:
- My post explains prod() and cartesian_prod().
- My post explains mean() and nanmean().
- My post explains median() and nanmedian().
- My post explains cumsum() and cumprod().
-
My post explains
torch.nan
andtorch.inf
.
sum() can get the 0D or more D tensor of zero or more sum's elements, normally treating one or more NaNs(Not a Numbers) from the 0D or more D tensor of zero or more elements as shown below:
*Memos:
-
sum()
can be used with torch or a tensor. - The 1st argument(
input
) withtorch
or using a tensor(Required-Type:tensor
ofint
,float
,complex
orbool
). - The 2nd argument with
torch
or the 1st argument with a tensor isdim
(Optional-Type:int
,tuple
ofint
orlist
ofint
). - The 3rd argument with
torch
or the 2nd argument with a tensor iskeepdim
(Optional-Default:False
-Type:bool
): *Memos:-
keepdim=
must be used withdim=
. -
My post explains
keepdim
argument.
-
- There is
dtype
argument withtorch
(Optional-Default:None
-Type:dtype): *Memos:- If it's
None
, it's inferred frominput
or a tensor. -
dtype=
must be used. -
My post explains
dtype
argument.
- If it's
- Normally, the arithmetic operation with a NaN results in a NaN.
- The empty 1D or more D
input
tensor or tensor withoutdim
or with the deepestdim
gets a zero.
import torch
my_tensor = torch.tensor([1, 2, 3, 4])
torch.sum(input=my_tensor)
my_tensor.sum()
torch.sum(input=my_tensor, dim=0)
torch.sum(input=my_tensor, dim=-1)
torch.sum(input=my_tensor, dim=(0,))
torch.sum(input=my_tensor, dim=(-1,))
# tensor(10)
my_tensor = torch.tensor([1, 2, torch.nan, 4])
torch.sum(input=my_tensor)
# tensor(nan)
my_tensor = torch.tensor([[1, 2, 3, 4], [5, 6, 7, 8]])
torch.sum(input=my_tensor)
torch.sum(input=my_tensor, dim=(0, 1))
torch.sum(input=my_tensor, dim=(0, -1))
torch.sum(input=my_tensor, dim=(1, 0))
torch.sum(input=my_tensor, dim=(1, -2))
torch.sum(input=my_tensor, dim=(-1, 0))
torch.sum(input=my_tensor, dim=(-1, -2))
torch.sum(input=my_tensor, dim=(-2, 1))
torch.sum(input=my_tensor, dim=(-2, -1))
# tensor(36)
torch.sum(input=my_tensor, dim=0)
torch.sum(input=my_tensor, dim=-2)
torch.sum(input=my_tensor, dim=(0,))
torch.sum(input=my_tensor, dim=(-2,))
# tensor([6, 8, 10, 12])
torch.sum(input=my_tensor, dim=1)
torch.sum(input=my_tensor, dim=-1)
torch.sum(input=my_tensor, dim=(1,))
torch.sum(input=my_tensor, dim=(-1,))
# tensor([10, 26])
my_tensor = torch.tensor([[1, 2, torch.nan, 4], [torch.nan, 6, 7, 8]])
torch.sum(input=my_tensor)
# tensor(nan)
torch.sum(input=my_tensor, dim=0)
# tensor([nan, 8., nan, 12.])
torch.sum(input=my_tensor, dim=1)
# tensor([nan, nan])
my_tensor = torch.tensor([[1., 2., 3., 4.], [5., 6., 7., 8.]])
torch.sum(input=my_tensor)
# tensor(36.)
my_tensor = torch.tensor([[1, 2, torch.nan, 4], [torch.nan, 6, 7, 8]])
torch.sum(input=my_tensor)
# tensor(nan)
my_tensor = torch.tensor([[1.+0.j, 2.+0.j, 3.+0.j, 4.+0.j],
[5.+0.j, 6.+0.j, 7.+0.j, 8.+0.j]])
torch.sum(input=my_tensor)
# tensor(36.+0.j)
my_tensor = torch.tensor([[1.+0.j, 2.+0.j, torch.nan, 4.+0.j],
[torch.nan, 6.+0.j, 7.+0.j, 8.+0.j]])
torch.sum(input=my_tensor)
# tensor(nan+0.j)
my_tensor = torch.tensor([[True, False, True, False],
[False, True, False, True]])
torch.sum(input=my_tensor)
# tensor(4)
my_tensor = torch.tensor([])
torch.sum(input=my_tensor)
# tensor(0.)
nansum() can get the 0D or more D tensor of zero or more sum's elements, treating nan
as zero from the 0D or more D tensor of zero or more elements as shown below:
*Memos:
-
nansum()
can be used withtorch
or a tensor. - The 1st argument(
input
) withtorch
or using a tensor(Required-Type:tensor
ofint
,float
orbool
). - The 2nd argument with
torch
or the 1st argument with a tensor isdim
(Optional-Type:int
,tuple
ofint
orlist
ofint
). - The 3rd argument with
torch
or the 2nd argument with a tensor iskeepdim
(Optional-Default:False
-Type:bool
): *Memos:-
keepdim=
must be used withdim=
. -
My post explains
keepdim
argument.
-
- There is
dtype
argument withtorch
(Optional-Default:None
-Type:dtype): *Memos:- If it's
None
, it's inferred frominput
or a tensor. -
dtype=
must be used. -
My post explains
dtype
argument.
- If it's
- Normally, the arithmetic operation with a NaN results in a NaN.
- The empty 1D or more D
input
tensor or tensor withoutdim
or with the deepestdim
gets a zero.
import torch
my_tensor = torch.tensor([1., 2., torch.nan, 4.])
torch.nansum(input=my_tensor)
my_tensor.nansum()
torch.nansum(input=my_tensor, dim=0)
torch.nansum(input=my_tensor, dim=-1)
torch.nansum(input=my_tensor, dim=(0,))
torch.nansum(input=my_tensor, dim=(-1,))
# tensor(7.)
my_tensor = torch.tensor([[1., 2., torch.nan, 4.],
[torch.nan, 6., 7., 8.]])
torch.nansum(input=my_tensor)
torch.nansum(input=my_tensor, dim=(0, 1))
torch.nansum(input=my_tensor, dim=(0, -1))
torch.nansum(input=my_tensor, dim=(1, 0))
torch.nansum(input=my_tensor, dim=(1, -2))
torch.nansum(input=my_tensor, dim=(-1, 0))
torch.nansum(input=my_tensor, dim=(-1, -2))
torch.nansum(input=my_tensor, dim=(-2, 1))
torch.nansum(input=my_tensor, dim=(-2, -1))
# tensor(28.)
torch.nansum(input=my_tensor, dim=0)
torch.nansum(input=my_tensor, dim=-2)
torch.nansum(input=my_tensor, dim=(0,))
torch.nansum(input=my_tensor, dim=(-2,))
# tensor([1., 8., 7., 12.])
torch.nansum(input=my_tensor, dim=1)
torch.nansum(input=my_tensor, dim=-1)
torch.nansum(input=my_tensor, dim=(1,))
torch.nansum(input=my_tensor, dim=(-1,))
# tensor([7., 21.])
my_tensor = torch.tensor([[1, 2, 3, 4], [5, 6, 7, 8]])
torch.nansum(input=my_tensor)
# tensor(36)
my_tensor = torch.tensor([[True, False, True, False],
[False, True, False, True]])
torch.nansum(input=my_tensor)
# tensor(4)
my_tensor = torch.tensor([])
torch.nansum(input=my_tensor)
# tensor(0.)
💖 💪 🙅 🚩
Super Kai (Kazuya Ito)
Posted on July 18, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.