mean() and median() in PyTorch

mean() can get the mean(average) values as shown below:

*Memos:

  • mean() can be called both from torch and a tensor.
  • The 2nd argument is one or more dimensions with torch.
  • The 1st argument is one or more dimensions with a tensor.
  • mean() can only accept floating-point or complex numbers so you need conversion to them if they are not as I explain it in my answer(17.1) otherwise there is the error.
import torch my_tensor = torch.tensor([[5, 4, 7, 7], [6, 5, 3, 5], [3, 8, 9, 3]])
torch.mean(my_tensor.float())tensor(5.4167)
my_tensor.float().mean()
# tensor(5.4167)

torch.mean(my_tensor.float(), 0)
my_tensor.float().mean(0)
torch.mean(my_tensor.float(), (0,))
my_tensor.float().mean((0,))
torch.mean(my_tensor.float(), -2)
my_tensor.float().mean(-2)
torch.mean(my_tensor.float(), (-2,))
my_tensor.float().mean((-2,))
# tensor([4.6667, 5.6667, 6.3333, 5.0000])

torch.mean(my_tensor.float(), 1)
my_tensor.float().mean(1)
torch.mean(my_tensor.float(), (1,))
my_tensor.float().mean((1,))
torch.mean(my_tensor.float(), -1)
my_tensor.float().mean(-1)
torch.mean(my_tensor.float(), (-1,))
my_tensor.float().mean((-1,))
# tensor([5.7500, 4.7500, 5.7500])

torch.mean(my_tensor.float(), (0, 1))
my_tensor.float().mean((0, 1))
torch.mean(my_tensor.float(), (0, -1))
my_tensor.float().mean((0, -1))
torch.mean(my_tensor.float(), (1, 0))
my_tensor.float().mean((1, 0))
torch.mean(my_tensor.float(), (1, -2))
my_tensor.float().mean((1, -2))
torch.mean(my_tensor.float(), (-1, 0))
my_tensor.float().mean((-1, 0))
torch.mean(my_tensor.float(), (-1, -2))
my_tensor.float().mean((-1, -2))
torch.mean(my_tensor.float(), (-2, 1))
my_tensor.float().mean((-2, 1))
torch.mean(my_tensor.float(), (-2, -1))
my_tensor.float().mean((-2, -1))
# tensor(5.4167)
Enter fullscreen mode
Exit fullscreen mode
import torch my_tensor = torch.tensor([[[0., 1., 2.], [3., 4., 5.]], [[6., 7., 8.], [9., 10., 11.]], [[12., 13., 14.], [15., 16., 17.]], [[18., 19., 20.], [21., 22., 23.]]])
torch.mean(my_tensor)
my_tensor.mean()
# tensor(11.5000)

torch.mean(my_tensor, 0)
my_tensor.mean(0)
torch.mean(my_tensor, (0,))
my_tensor.mean((0,))
# tensor([[9., 10., 11.], [12., 13., 14.]])

torch.mean(my_tensor, 1)
my_tensor.mean(1)
torch.mean(my_tensor, (1,))
my_tensor.mean((1,))
# tensor([[1.5000, 2.5000, 3.5000], [7.5000, 8.5000, 9.5000],
# [13.5000, 14.5000, 15.5000], [19.5000, 20.5000, 21.5000]])

torch.mean(my_tensor, 2)
torch.mean(my_tensor, (2,))
my_tensor.mean(2)
my_tensor.mean((2,))
torch.mean(my_tensor, -1)
my_tensor.mean(-1)
torch.mean(my_tensor, (-1,))
my_tensor.mean((-1,))
# tensor([[1., 4.], [7., 10.], [13., 16.], [19., 22.]])

torch.mean(my_tensor, -2)
my_tensor.mean(-2)
torch.mean(my_tensor, (-2,))
my_tensor.mean((-2,))
# tensor([[1.5000, 2.5000, 3.5000],
# [7.5000, 8.5000, 9.5000],
# [13.5000, 14.5000, 15.5000],
# [19.5000, 20.5000, 21.5000]])

torch.mean(my_tensor, -3)
my_tensor.mean(-3)
torch.mean(my_tensor, (-3,))
my_tensor.mean((-3,))
# tensor([[ 9., 10., 11.], [12., 13., 14.]])

torch.mean(my_tensor, (0, 1))
my_tensor.mean((0, 1))
torch.mean(my_tensor, (1, 0))
my_tensor.mean((1, 0))
torch.mean(my_tensor, (1, -3))
my_tensor.mean((1, -3))
torch.mean(my_tensor, (0, -2))
my_tensor.mean((0, -2))
torch.mean(my_tensor, (-2, 0))
my_tensor.mean((-2, 0))
torch.mean(my_tensor, (-2, -3))
my_tensor.mean((-2, -3))
torch.mean(my_tensor, (-3, 1))
my_tensor.mean((-3, 1))
torch.mean(my_tensor, (-3, -2))
my_tensor.mean((-3, -2))
# tensor([10.5000, 11.5000, 12.5000])

torch.mean(my_tensor, (0, 2))
my_tensor.mean((0, 2))
torch.mean(my_tensor, (0, -1))
my_tensor.mean((0, -1))
torch.mean(my_tensor, (2, 0))
my_tensor.mean((2, 0))
torch.mean(my_tensor, (2, -3))
my_tensor.mean((2, -3))
torch.mean(my_tensor, (-1, 0))
my_tensor.mean((-1, 0))
torch.mean(my_tensor, (-1, -3))
my_tensor.mean((-1, -3))
torch.mean(my_tensor, (-3, 2))
my_tensor.mean((-3, 2))
torch.mean(my_tensor, (-3, -1))
my_tensor.mean((-3, -1))
# tensor([10., 13.]) 
torch.mean(my_tensor, (1, 2))
my_tensor.mean((1, 2))
torch.mean(my_tensor, (1, -1))
my_tensor.mean((1, -1))
torch.mean(my_tensor, (2, 1))
my_tensor.mean((2, 1))
torch.mean(my_tensor, (2, -2))
my_tensor.mean((2, -2))
torch.mean(my_tensor, (-1, 1))
my_tensor.mean((-1, 1))
torch.mean(my_tensor, (-1, -2))
my_tensor.mean((-1, -2))
torch.mean(my_tensor, (-2, 2))
my_tensor.mean((-2, 2))
torch.mean(my_tensor, (-2, -1))
my_tensor.mean((-2, -1))
# tensor([2.5000, 8.5000, 14.5000, 20.5000])

torch.mean(my_tensor, (0, 1, 2))
my_tensor.mean((0, 1, 2))
etc.
# tensor(11.5000)
Enter fullscreen mode
Exit fullscreen mode

median() can get the median(average) values as shown below:

*Memos:

  • median() can be called both from torch and a tensor.
  • The 2nd argument is one or more dimensions with torch.
  • The 1st argument is one or more dimensions with a tensor.
import torch my_tensor = torch.tensor([[5, 4, 7, 7], [6, 5, 3, 5], [3, 8, 9, 3]])
torch.median(my_tensor)
my_tensor.median()
# tensor(5)

torch.median(my_tensor, 0)
my_tensor.median(0)
torch.median(my_tensor, -2)
my_tensor.median(-2)
# torch.return_types.median(
# values=tensor([5, 5, 7, 5]),
# indices=tensor([0, 1, 0, 1]))

torch.median(my_tensor, 1)
my_tensor.median(1)
torch.median(my_tensor, -1)
my_tensor.median(-1)
# torch.return_types.median(
# values=tensor([5, 5, 3]),
# indices=tensor([0, 1, 3]))
Enter fullscreen mode
Exit fullscreen mode
import torch my_tensor = torch.tensor([[[0., 1., 2.], [3., 4., 5.]], [[6., 7., 8.], [9., 10., 11.]], [[12., 13., 14.], [15., 16., 17.]], [[18., 19., 20.], [21., 22., 23.]]])
torch.median(my_tensor)
my_tensor.median()
# tensor(11.)

torch.median(my_tensor, 0)
my_tensor.median(0)
# torch.return_types.median(
# values=tensor([[6., 7., 8.], [9., 10., 11.]]),
# indices=tensor([[1, 1, 1], [1, 1, 1]]))

torch.median(my_tensor, 1)
my_tensor.median(1)
# torch.return_types.median(
# values=tensor([[0., 1., 2.], [6., 7., 8.],
# [12., 13., 14.], [18., 19., 20.]]),
# indices=tensor([[0, 0, 0], [0, 0, 0],
# [0, 0, 0], [0, 0, 0]]))

torch.median(my_tensor, 2)
my_tensor.median(2)
torch.median(my_tensor, -1)
my_tensor.median(-1)
# torch.return_types.median(
# values=tensor([[1., 4.], [7., 10.], [13., 16.], [19., 22.]]),
# indices=tensor([[1, 1], [1, 1], [1, 1], [1, 1]]))

torch.median(my_tensor, -2)
my_tensor.median(-2)
# torch.return_types.median(
# values=tensor([[0., 1., 2.], [6., 7., 8.],
# [12., 13., 14.], [18., 19., 20.]]),
# indices=tensor([[0, 0, 0], [0, 0, 0],
# [0, 0, 0], [0, 0, 0]]))

torch.median(my_tensor, -3)
my_tensor.median(-3)
# torch.return_types.median(
# values=tensor([[6., 7., 8.], [9., 10., 11.]]),
# indices=tensor([[1, 1, 1], [1, 1, 1]]))
Enter fullscreen mode
Exit fullscreen mode

Discover more from Coursity

Subscribe to get the latest posts sent to your email.

Leave a Comment

Your email address will not be published. Required fields are marked *

Discover more from Coursity

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top
Maximize remote it job opportunities with virtual assistant services. For users that register on our website (if any), we also store the personal information they provide in their user profile. Install docker compose ubuntu.