Super Kai (Kazuya Ito)
Posted on July 18, 2024
*Memos:
- My post explains sum() and nansum().
- My post explains prod() and cartesian_prod().
- My post explains median() and nanmedian().
- My post explains cumsum() and cumprod().
-
My post explains
torch.nan
andtorch.inf
.
mean() can get the 0 or more D tensor of zero or more mean(average) elements, normally treating zero or more NaNs(Not a Numbers) from the 0D or more D tensor of zero or more elements as shown below:
*Memos:
-
mean()
can be used with torch or a tensor. - The 1st argument(
input
) withtorch
or using a tensor(Required-Type:tensor
offloat
orcomplex
). - 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:- It must be used with
dim
. -
My post explains
keepdim
argument.
- It must be used with
- There is
dtype
argument withtorch
(Optional-Default:None
-Type:dtype): *Memos:- If it's
None
, it's inferred frominput
. -
dtype=
must be used. -
My post explains
dtype
argument.
- If it's
- There is
out
argument withtorch
(Optional-Default:None
-Type:tensor
): *Memos:- It must be used with
dim
. -
out=
must be used. -
My post explains
out
argument.
- It must be used with
- 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 NaN.
import torch
my_tensor = torch.tensor([5., 4., 7., 7.])
torch.mean(input=my_tensor)
my_tensor.mean()
torch.mean(input=my_tensor, dim=0)
torch.mean(input=my_tensor, dim=-1)
torch.mean(input=my_tensor, dim=(0,))
torch.mean(input=my_tensor, dim=(-1,))
# tensor(5.7500)
my_tensor = torch.tensor([5., 4., torch.nan, 7., 7.])
torch.mean(input=my_tensor)
# tensor(nan)
my_tensor = torch.tensor([[5., 4., 7., 7.],
[6., 5., 3., 5.],
[3., 8., 9., 3.]])
torch.mean(input=my_tensor)
torch.mean(input=my_tensor, dim=(0, 1))
torch.mean(input=my_tensor, dim=(0, -1))
torch.mean(input=my_tensor, dim=(1, 0))
torch.mean(input=my_tensor, dim=(1, -2))
torch.mean(input=my_tensor, dim=(-1, 0))
torch.mean(input=my_tensor, dim=(-1, -2))
torch.mean(input=my_tensor, dim=(-2, 1))
torch.mean(input=my_tensor, dim=(-2, -1))
# tensor(5.4167)
torch.mean(input=my_tensor, dim=0)
torch.mean(input=my_tensor, dim=(0,))
torch.mean(input=my_tensor, dim=-2)
torch.mean(input=my_tensor, dim=(-2,))
# tensor([4.6667, 5.6667, 6.3333, 5.0000])
torch.mean(input=my_tensor, dim=1)
torch.mean(input=my_tensor, dim=(1,))
torch.mean(input=my_tensor, dim=-1)
torch.mean(input=my_tensor, dim=(-1,))
# tensor([5.7500, 4.7500, 5.7500])
my_tensor = torch.tensor([[torch.nan, 5., 4., torch.nan, 7., 7., torch.nan],
[6., torch.nan, 5., torch.nan, 3., 5., torch.nan],
[3., 8., torch.nan, torch.nan, 9., 3., torch.nan]])
torch.mean(input=my_tensor)
# tensor(nan)
torch.mean(input=my_tensor, dim=0)
# tensor([nan, nan, nan, nan, 6.3333, 5.0000, nan])
torch.mean(input=my_tensor, dim=1)
# tensor([nan, nan, nan])
my_tensor = torch.tensor([[5.+0.j, 4.+0.j, 7.+0.j, 7.+0.j],
[6.+0.j, 5.+0.j, 3.+0.j, 5.+0.j],
[3.+0.j, 8.+0.j, 9.+0.j, 3.+0.j]])
torch.mean(input=my_tensor)
# tensor(5.4167+0.j)
my_tensor = torch.tensor([[5.+0.j, 4.+0.j, torch.nan, 7.+0.j, 7.+0.j],
[6.+0.j, torch.nan, 5.+0.j, 3.+0.j, 5.+0.j],
[3.+0.j, 8.+0.j, 9.+0.j, torch.nan, 3.+0.j]])
torch.mean(input=my_tensor)
# tensor(nan+nanj)
my_tensor = torch.tensor([])
torch.mean(input=my_tensor)
# tensor(nan)
nanmean() can get the 0 or more D tensor of zero or more mean(average) elements, ignoring zero or more NaNs(Not a Numbers) only if they are with non-NaNs from the 0D or more D tensor of zero or more elements as shown below:
*Memos:
-
nanmean()
can be used withtorch
or a tensor. - The 1st argument(
input
) withtorch
or using a tensor(Required-Type:tensor
offloat
). - 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:- It must be used with
dim
. -
My post explains
keepdim
argument.
- It must be used with
- There is
dtype
argument withtorch
(Optional-Default:None
-Type:dtype): *Memos:- If it's
None
, it's inferred frominput
. -
dtype=
must be used. -
My post explains
dtype
argument.
- If it's
- There is
out
argument withtorch
(Optional-Default:None
-Type:tensor
): *Memos:- It must be used with
dim
. -
out=
must be used. -
My post explains
out
argument.
- It must be used with
- 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 NaN.
import torch
my_tensor = torch.tensor(torch.nan)
my_tensor = torch.tensor([torch.nan, torch.nan])
my_tensor = torch.tensor([torch.nan, torch.nan, torch.nan])
torch.nanmean(input=my_tensor)
my_tensor.nanmean()
torch.nanmean(input=my_tensor, dim=0)
torch.nanmean(input=my_tensor, dim=-1)
torch.nanmean(input=my_tensor, dim=(0,))
torch.nanmean(input=my_tensor, dim=(-1,))
# tensor(nan)
my_tensor = torch.tensor([5., 4., 7., 7.])
my_tensor = torch.tensor([5., 4., torch.nan, 7., 7.])
my_tensor = torch.tensor([5., 4., 7., 7., torch.nan])
torch.nanmean(input=my_tensor)
torch.nanmean(input=my_tensor, dim=0)
torch.nanmean(input=my_tensor, dim=-1)
torch.nanmean(input=my_tensor, dim=(0,))
torch.nanmean(input=my_tensor, dim=(-1,))
# tensor(5.7500)
my_tensor = torch.tensor([[torch.nan, 5., 4., torch.nan, 7., 7., torch.nan],
[6., torch.nan, 5., torch.nan, 3., 5., torch.nan],
[3., 8., torch.nan, torch.nan, 9., 3., torch.nan]])
torch.nanmean(input=my_tensor)
torch.nanmean(input=my_tensor, dim=(0, 1))
torch.nanmean(input=my_tensor, dim=(0, -1))
torch.nanmean(input=my_tensor, dim=(1, 0))
torch.nanmean(input=my_tensor, dim=(1, -2))
torch.nanmean(input=my_tensor, dim=(-1, 0))
torch.nanmean(input=my_tensor, dim=(-1, -2))
torch.nanmean(input=my_tensor, dim=(-2, 1))
torch.nanmean(input=my_tensor, dim=(-2, -1))
# tensor(5.4167)
torch.nanmean(input=my_tensor, dim=0)
torch.nanmean(input=my_tensor, dim=(0,))
torch.nanmean(input=my_tensor, dim=-2)
torch.nanmean(input=my_tensor, dim=(-2,))
# tensor([4.5000, 6.5000, 4.5000, nan, 6.3333, 5.0000, nan])
torch.nanmean(input=my_tensor, dim=1)
torch.nanmean(input=my_tensor, dim=(1,))
torch.nanmean(input=my_tensor, dim=-1)
torch.nanmean(input=my_tensor, dim=(-1,))
# tensor([5.7500, 4.7500, 5.7500])
my_tensor = torch.tensor([])
torch.nanmean(input=my_tensor)
# tensor(nan)
💖 💪 🙅 🚩
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.