DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Updated on

median() and nanmedian() in PyTorch

Buy Me a Coffee

*Memos:

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) with torch or using a tensor(Required-Type:tensor of int or float).
  • The 2nd argument with torch or the 1st argument with a tensor is dim(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 is keepdim(Optional-Default:False-Type:bool): *Memos:
    • It must be used with dim.
    • My post explains keepdim argument.
  • There is out argument with torch(Optional-Type:tuple(tensor, tensor) or list(tensor, tensor): *Memos:
    • It must be used with dim.
    • out= must be used.
    • My post explains out argument.
  • Normally, the arithmetic operation with a NaN results in a NaN.
  • The empty 1D or more D input tensor or tensor without dim gets a NaN.
  • The empty 1D or more D input tensor or tensor with the deepest dim 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
Enter fullscreen mode Exit fullscreen mode

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 with torch or a tensor.
  • The 1st argument(input) with torch or using a tensor(Required-Type:tensor of int or float).
  • The 2nd argument with torch or the 1st argument with a tensor is dim(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 is keepdim(Optional-Default:False-Type:bool): *Memos:
    • It must be used with dim.
    • My post explains keepdim argument.
  • There is out argument with torch(Optional-Type:tuple(tensor, tensor) or list(tensor, tensor): *Memos:
    • It must be used with dim.
    • out= must be used.
    • My post explains out argument.
  • Normally, the arithmetic operation with a NaN results in a NaN.
  • The empty 1D or more D input tensor or tensor without dim gets a NaN.
  • The empty 1D or more D input tensor or tensor with the deepest dim 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
Enter fullscreen mode Exit fullscreen mode

Top comments (0)