DEV Community

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

Posted on • Updated on

mean() and median() in PyTorch

mean() can get the 0 or more D tensor of one or more mean(average) elements from 0 or more D tensor as shown below:

*Memos:

  • mean() can be used with torch or a tensor.
  • The 1st argument(tensor of float or complex) with torch or using a tensor(tensor of float or complex) is input(Required).
  • The 2nd argument(int, tuple of int or list of int) with torch or the 1st argument(int, tuple of int or list of int) with a tensor is dim(Optional).
  • The 3rd argument(bool) with torch or the 2nd argument(bool) with a tensor is keepdim(Optional-Default:False) which keeps the dimension of the input tensor. *keepdim must be used with dim.
  • There is dtype argument(torch.dtype) (Optional) with torch. *Memos:
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)

torch.mean(input=my_tensor, dim=0, keepdim=True)
# tensor([5.7500])

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])

torch.mean(input=my_tensor, dim=0, keepdim=True)
# tensor([[4.6667, 5.6667, 6.3333, 5.0000]])

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, dtype=torch.complex64)
# tensor(5.4167+0.j)
Enter fullscreen mode Exit fullscreen mode

median() can get the 0 or more D tensor of one or more median elements from 0 or more D tensor as shown below:

*Memos:

  • median() can be used with torch or a tensor.
  • The 1st argument(tensor of int or float) with torch or using a tensor(tensor of int or float) is input(Required).
  • The 2nd argument(int) with torch or the 1st argument(int) with a tensor is dim(Optional).
  • The 3rd argument(bool) with torch or the 2nd argument(bool) with a tensor is keepdim(Optional-Default:False) which keeps the dimension of the input tensor. *keepdim must be used with dim.
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))

torch.median(input=my_tensor, dim=0, keepdim=True)
# torch.return_types.median(
# values=tensor([5]),
# indices=tensor([0]))

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]))

torch.median(input=my_tensor, dim=0, keepdim=True)
# torch.return_types.median(
# values=tensor([[5, 5, 7, 5]]),
# indices=tensor([[0, 1, 0, 1]]))

my_tensor = torch.tensor([[5., 4., 7., 7.],
                          [6., 5., 3., 5.],
                          [3., 8., 9., 3.]])
torch.median(input=my_tensor)
# tensor(5.)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)