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
offloat
orcomplex
) withtorch
or using a tensor(tensor
offloat
orcomplex
) isinput
(Required). - The 2nd argument(
int
,tuple
ofint
orlist
ofint
) withtorch
or the 1st argument(int
,tuple
ofint
orlist
ofint
) with a tensor isdim
(Optional). - The 3rd argument(
bool
) withtorch
or the 2nd argument(bool
) with a tensor iskeepdim
(Optional-Default:False
) which keeps the dimension of theinput
tensor. *keepdim
must be used withdim
. - There is
dtype
argument(torch.dtype) (Optional) withtorch
. *Memos:- If
None
, the type of theinput
tensor is used. -
dtype
can also accept int(), float() and bool() but not complex() which are python built-in functions. -
dtype=
must be used.
- If
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)
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 withtorch
or a tensor. - The 1st argument(
tensor
ofint
orfloat
) withtorch
or using a tensor(tensor
ofint
orfloat
) isinput
(Required). - The 2nd argument(
int
) withtorch
or the 1st argument(int
) with a tensor isdim
(Optional). - The 3rd argument(
bool
) withtorch
or the 2nd argument(bool
) with a tensor iskeepdim
(Optional-Default:False
) which keeps the dimension of theinput
tensor. *keepdim
must be used withdim
.
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.)
Top comments (0)