*Memos:
- My post explains aminmax(), amin() and amax().
- My post explains minimum(), maximum(). fmin() and fmax().
- My post explains kthvalue() and topk().
min() can get the one or more minimum elements of a 0D or more D tensor from one or two of 0D or more D tensors as shown below:
*Memos:
-
min()
can be used with torch or a tensor. - The 1st argument(
tensor
ofint
,float
orbool
) withtorch
or using a tensor(tensor
ofint
,float
orbool
) isinput
(Required). - The 2nd argument(
int
) withtorch
or the 1st argument(int
) isdim
(Optional). *Settingdim
can get one or more 1st minimum elements and the indices of them. - The 2nd argument(
tensor
ofint
,float
orbool
) withtorch
or the 1st argument(tensor
ofint
,float
orbool
) isother
(Optional). *Memos:- It can only be used with
input
. - This is the functionality of minimum().
- It can only be used with
- The 3rd argument(
bool
) withtorch
or the 2nd argument(bool
) iskeepdim
(Optional). *Memos:- It keeps the dimension of the
input
tensor. - It must be used with
dim
.
- It keeps the dimension of the
import torch
my_tensor = torch.tensor([[5, 4, 7, 7],
[6, 5, 3, 5],
[3, 8, 9, 3]])
torch.min(input=my_tensor)
my_tensor.min()
# tensor(3)
torch.min(input=my_tensor, dim=0)
torch.min(input=my_tensor, dim=-2)
# torch.return_types.min(
# values=tensor([3, 4, 3, 3]),
# indices=tensor([2, 0, 1, 2]))
torch.min(input=my_tensor, dim=1)
torch.min(input=my_tensor, dim=-1)
# torch.return_types.min(
# values=tensor([4, 3, 3]),
# indices=tensor([1, 2, 0]))
torch.min(input=my_tensor, dim=0, keepdim=True)
# torch.return_types.min(
# values=tensor([[3, 4, 3, 3]]),
# indices=tensor([[2, 0, 1, 2]]))
tensor1 = torch.tensor([5, 4, 7, 7])
tensor2 = torch.tensor([[6, 5, 3, 5],
[3, 8, 9, 3]])
torch.min(input=tensor1, other=tensor2)
# tensor([[5, 4, 3, 5],
# [3, 4, 7, 3]])
tensor1 = torch.tensor([5., 4., 7., 7.])
tensor2 = torch.tensor([[6., 5., 3., 5.],
[3., 8., 9., 3.]])
torch.min(input=tensor1, other=tensor2)
# tensor([[5., 4., 3., 5.],
# [3., 4., 7., 3.]])
tensor1 = torch.tensor([True, False, True, False])
tensor2 = torch.tensor([[True, False, True, False],
[False, True, False, True]])
torch.min(input=tensor1, other=tensor2)
# tensor([[True, False, True, False],
# [False, False, False, False]])
max() can get the one or more maximum elements of a 0D or more D tensor from one or two of 0D or more D tensors as shown below:
- The 1st argument(
tensor
ofint
,float
orbool
) withtorch
or using a tensor(tensor
ofint
,float
orbool
) isinput
(Required). - The 2nd argument(
int
ortuple
ofint
) withtorch
or the 1st argument(int
ortuple
ofint
) isdim
(Optional). *Settingdim
can get one or more 1st maximum elements and the indices of them. - The 2nd argument(
tensor
ofint
,float
orbool
) withtorch
or the 1st argument(tensor
ofint
,float
orbool
) isother
(Optional). *Memos:- It can only be used with
input
. - This is the functionality of maximum().
- It can only be used with
- The 3rd argument(
bool
) withtorch
or the 2nd argument(bool
) iskeepdim
(Optional). *Memos:- It keeps the dimension of the
input
tensor. - It must be used with
dim
.
- It keeps the dimension of the
import torch
my_tensor = torch.tensor([[5, 4, 7, 7],
[6, 5, 3, 5],
[3, 8, 9, 3]])
torch.max(input=my_tensor)
my_tensor.max()
# tensor(9)
torch.max(input=my_tensor, dim=0)
torch.max(input=my_tensor, dim=-2)
# torch.return_types.max(
# values=tensor([6, 8, 9, 7]),
# indices=tensor([1, 2, 2, 0]))
torch.max(input=my_tensor, dim=1)
torch.max(input=my_tensor, dim=-1)
# torch.return_types.max(
# values=tensor([7, 6, 9]),
# indices=tensor([2, 0, 2]))
torch.max(input=my_tensor, dim=0, keepdim=True)
# torch.return_types.max(
# values=tensor([[6, 8, 9, 7]]),
# indices=tensor([[1, 2, 2, 0]]))
tensor1 = torch.tensor([5, 4, 7, 7])
tensor2 = torch.tensor([[6, 5, 3, 5],
[3, 8, 9, 3]])
torch.max(input=tensor1, other=tensor2)
# tensor([[6, 5, 7, 7],
# [5, 8, 9, 7]])
tensor1 = torch.tensor([5., 4., 7., 7.])
tensor2 = torch.tensor([[6., 5., 3., 5.],
[3., 8., 9., 3.]])
torch.max(input=tensor1, other=tensor2)
# tensor([[6., 5., 7., 7.],
# [5., 8., 9., 7.]])
tensor1 = torch.tensor([True, False, True, False])
tensor2 = torch.tensor([[True, False, True, False],
[False, True, False, True]])
torch.max(input=tensor1, other=tensor2)
# tensor([[True, False, True, False],
# [True, True, True, True]])
argmin() can get the one or more indices of the 1st minimum elements of a 0D or more D tensor from a 0D or more D tensor as shown below:
*Memos:
-
argmin()
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
) isdim
(Optional). *Settingdim
can get the one or more indices of the 1st minimum elements. - One complex number or boolean value of a 1D or more D tensor with
dim=
works.
import torch
my_tensor = torch.tensor([[5, 4, 7, 7],
[6, 5, 3, 5],
[3, 8, 9, 3]])
torch.argmin(input=my_tensor)
my_tensor.argmin()
# tensor(6)
torch.argmin(input=my_tensor, dim=0)
torch.argmin(input=my_tensor, dim=-2)
# tensor([2, 0, 1, 2])
torch.argmin(input=my_tensor, dim=1)
torch.argmin(input=my_tensor, dim=-1)
# tensor([1, 2, 0])
torch.argmin(input=my_tensor, keepdim=True)
# tensor([[6]])
torch.argmin(input=my_tensor, dim=0, keepdim=True)
# tensor([[2, 0, 1, 2]])
my_tensor = torch.tensor([[5., 4., 7., 7.],
[6., 5., 3., 5.],
[3., 8., 9., 3.]])
torch.argmin(input=my_tensor)
# tensor(6)
my_tensor = torch.tensor([5+7j])
torch.argmin(input=my_tensor, dim=0)
torch.argmin(input=my_tensor, dim=-1)
# tensor(0)
my_tensor = torch.tensor([[True]])
torch.argmin(input=my_tensor, dim=0)
torch.argmin(input=my_tensor, dim=-1)
# tensor([0])
argmax() can get the indices of the 1st maximum elements of a 0D or more D tensor from a 0D or more D tensor as shown below:
*Memos:
-
argmax()
can be called both fromtorch
and 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
) isdim
(Optional). *Settingdim
can get the one or more indices of the 1st maximum elements. - One complex number or boolean value of a 1D or more D tensor with
dim=
works.
import torch
my_tensor = torch.tensor([[5, 4, 7, 7],
[6, 5, 3, 5],
[3, 8, 9, 3]])
torch.argmax(input=my_tensor)
my_tensor.argmax()
# tensor(10)
torch.argmax(input=my_tensor, dim=0)
torch.argmax(input=my_tensor, dim=-2)
# tensor([1, 2, 2, 0])
torch.argmax(input=my_tensor, dim=1)
torch.argmax(input=my_tensor, dim=-1)
# tensor([2, 0, 2])
torch.argmax(input=my_tensor, keepdim=True)
# tensor([[10]])
torch.argmax(input=my_tensor, dim=0, keepdim=True)
# tensor([[1, 2, 2, 0]])
my_tensor = torch.tensor([[5., 4., 7., 7.],
[6., 5., 3., 5.],
[3., 8., 9., 3.]])
torch.argmax(input=my_tensor)
# tensor(10)
my_tensor = torch.tensor([5+7j])
torch.argmax(input=my_tensor, dim=0)
torch.argmax(input=my_tensor, dim=-1)
# tensor(0)
my_tensor = torch.tensor([[True]])
torch.argmax(input=my_tensor, dim=0)
torch.argmax(input=my_tensor, dim=-1)
# tensor([0])