DEV Community

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

Posted on • Edited on

mean and nanmean in PyTorch

Buy Me a Coffee

*Memos:

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) with torch or using a tensor(Required-Type:tensor of float or complex).
  • The 2nd argument with torch or the 1st argument with a tensor is dim(Optional-Type:int, tuple of int or list of int).
  • 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 dtype argument with torch(Optional-Default:None-Type:dtype): *Memos:
    • If it's None, it's inferred from input.
    • dtype= must be used.
    • My post explains dtype argument.
  • There is out argument with torch(Optional-Default:None-Type: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 or with the deepest dim 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)
Enter fullscreen mode Exit fullscreen mode

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 with torch or a tensor.
  • The 1st argument(input) with torch or using a tensor(Required-Type:tensor of float).
  • The 2nd argument with torch or the 1st argument with a tensor is dim(Optional-Type:int, tuple of int or list of int).
  • 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 dtype argument with torch(Optional-Default:None-Type:dtype): *Memos:
    • If it's None, it's inferred from input.
    • dtype= must be used.
    • My post explains dtype argument.
  • There is out argument with torch(Optional-Default:None-Type: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 or with the deepest dim 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)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)