Super Kai (Kazuya Ito)
Posted on July 19, 2024
*Memos:
- My post explains sum() and nansum().
- My post explains prod() and cartesian_prod().
- My post explains mean() and nanmean().
- My post explains cumsum() and cumprod().
-
My post explains
torch.nan
andtorch.inf
.
median() can get the 0 or more D tensor of one median element or two of the 0D or more D tensors of zero or more median elements and their indices, 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:
-
median()
can be used with torch or a tensor. - The 1st argument(
input
) withtorch
or using a tensor(Required-Type:tensor
ofint
orfloat
). - The 2nd argument with
torch
or the 1st argument with a tensor isdim
(Optional-Type:int
). *It can get two of the 0D or more D tensors of the zero or more median elements and their indices. - 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
out
argument withtorch
(Optional-Default:None
-Type:tuple
(tensor
,tensor
) orlist
(tensor
,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
gets a NaN. - The empty 1D or more D
input
tensor or tensor with the deepestdim
doesn't work to get a NaN.
import torch
my_tensor = torch.tensor([5., 4., 7., 7.])
torch.median(input=my_tensor)
my_tensor.median()
# tensor(5.)
torch.median(input=my_tensor, dim=0)
torch.median(input=my_tensor, dim=-1)
# torch.return_types.median(
# values=tensor(5.),
# indices=tensor(0))
my_tensor = torch.tensor([5., 4., torch.nan, 7., 7.])
torch.median(input=my_tensor)
# tensor(nan)
my_tensor = torch.tensor([[5., 4., 7., 7.],
[6., 5., 3., 5.],
[3., 8., 9., 3.]])
torch.median(input=my_tensor)
# tensor(5.)
torch.median(input=my_tensor, dim=0)
torch.median(input=my_tensor, dim=-2)
# torch.return_types.median(
# values=tensor([5., 5., 7., 5.]),
# indices=tensor([0, 1, 0, 1]))
torch.median(input=my_tensor, dim=1)
torch.median(input=my_tensor, dim=-1)
# torch.return_types.median(
# values=tensor([5., 5., 3.]),
# indices=tensor([0, 1, 3]))
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.median(input=my_tensor)
# tensor(nan)
torch.median(input=my_tensor, dim=0)
# torch.return_types.median(
# values=tensor([nan, nan, nan, nan, 7., 5., nan]),
# indices=tensor([0, 1, 2, 0, 0, 1, 0]))
torch.median(input=my_tensor, dim=1)
# torch.return_types.median(
# values=tensor([nan, nan, nan]),
# indices=tensor([0, 1, 2]))
my_tensor = torch.tensor([[5, 4, 7, 7],
[6, 5, 3, 5],
[3, 8, 9, 3]])
torch.median(input=my_tensor)
# tensor(5)
my_tensor = torch.tensor([])
torch.median(input=my_tensor)
# tensor(nan)
torch.median(input=my_tensor, dim=0) # Error
my_tensor = torch.tensor([[]])
torch.median(input=my_tensor)
# tensor(nan)
torch.median(input=my_tensor, dim=0)
# torch.return_types.median(
# values=tensor([]),
# indices=tensor([], dtype=torch.int64))
torch.median(input=my_tensor, dim=1) # Error
my_tensor = torch.tensor([[[]]])
torch.median(input=my_tensor)
# tensor(nan)
torch.median(input=my_tensor, dim=0)
torch.median(input=my_tensor, dim=1)
# torch.return_types.median(
# values=tensor([], size=(1, 0)),
# indices=tensor([], size=(1, 0), dtype=torch.int64))
torch.median(input=my_tensor, dim=2) # Error
nanmedian() can get the 0D or more D tensor of one median element or two of the 0D or more D tensors of zero or more median elements and their indices, 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:
-
nanmedian()
can be used withtorch
or a tensor. - The 1st argument(
input
) withtorch
or using a tensor(Required-Type:tensor
ofint
orfloat
). - The 2nd argument with
torch
or the 1st argument with a tensor isdim
(Optional-Type:int
). *It can get two of the 0D or more D tensors of the zero or more median elements and their indices. - 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
out
argument withtorch
(Optional-Default:None
-Type:tuple
(tensor
,tensor
) orlist
(tensor
,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
gets a NaN. - The empty 1D or more D
input
tensor or tensor with the deepestdim
doesn't work to get 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.nanmedian(input=my_tensor)
my_tensor.nanmedian()
# tensor(nan)
torch.nanmedian(input=my_tensor, dim=0)
torch.nanmedian(input=my_tensor, dim=-1)
# torch.return_types.nanmedian(
# values=tensor(nan),
# indices=tensor(0))
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.nanmedian(input=my_tensor)
# tensor(5.)
torch.nanmedian(input=my_tensor, dim=0)
torch.nanmedian(input=my_tensor, dim=-1)
# torch.return_types.nanmedian(
# values=tensor(5.),
# indices=tensor(0))
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.nanmedian(input=my_tensor)
# tensor(5.)
torch.nanmedian(input=my_tensor, dim=0)
torch.nanmedian(input=my_tensor, dim=-2)
# torch.return_types.nanmedian(
# values=tensor([3., 5., 4., nan, 7., 5., nan]),
# indices=tensor([2, 0, 0, 0, 0, 1, 0]))
torch.nanmedian(input=my_tensor, dim=1)
torch.nanmedian(input=my_tensor, dim=-1)
# torch.return_types.nanmedian(
# values=tensor([5., 5., 3.]),
# indices=tensor([1, 2, 5]))
my_tensor = torch.tensor([[5, 4, 7, 7],
[6, 5, 3, 5],
[3, 8, 9, 3]])
torch.nanmedian(input=my_tensor)
# tensor(5)
my_tensor = torch.tensor([])
torch.nanmedian(input=my_tensor)
# tensor(nan)
torch.nanmedian(input=my_tensor, dim=0) # Error
my_tensor = torch.tensor([[]])
torch.nanmedian(input=my_tensor)
# tensor(nan)
torch.nanmedian(input=my_tensor, dim=0)
# torch.return_types.nanmedian(
# values=tensor([]),
# indices=tensor([], dtype=torch.int64))
torch.nanmedian(input=my_tensor, dim=1) # Error
my_tensor = torch.tensor([[[]]])
torch.nanmedian(input=my_tensor)
# tensor(nan)
torch.nanmedian(input=my_tensor, dim=0)
torch.nanmedian(input=my_tensor, dim=1)
# torch.return_types.nanmedian(
# values=tensor([], size=(1, 0)),
# indices=tensor([], size=(1, 0), dtype=torch.int64))
torch.nanmedian(input=my_tensor, dim=2) # Error
💖 💪 🙅 🚩
Super Kai (Kazuya Ito)
Posted on July 19, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.