DEV Community

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

Posted on • Updated on

minimum(), maximum(), kthvalue() and topk() in PyTorch

*Memos:

minimum() can get the zero or more minimum values from 2 tensors as shown below:

*Memos:

  • minimum() can be used with torch or a tensor.
  • minimum() can be used with 0D or more D tesnors.
  • 2 tensors must be the same size.
  • Only the tensor of zero or more integers, floating-point numbers or boolean values can be used so the tensor of zero or more complex numbers cannot be used.
import torch

tensor1 = torch.tensor([7, 1, 4])
tensor2 = torch.tensor([5, 3, 0])
torch.minimum(tensor1, tensor2)
tensor1.minimum(tensor2)
# tensor([5, 1, 0])

tensor1 = torch.tensor([[7, 1, 4], [5, 3, 0]])
tensor2 = torch.tensor([[3, 5, 1], [9, 0, 6]])
torch.minimum(tensor1, tensor2)
tensor1.minimum(tensor2)
# tensor([[3, 1, 1], [5, 0, 0]])

tensor1 = torch.tensor([[7., 1., 4.], [5., 3., 0.]])
tensor2 = torch.tensor([[3., 5., True], [9., False, 6.]])
torch.minimum(tensor1, tensor2)
tensor1.minimum(tensor2)
# tensor([[3., 1., 1.], [5., 0., 0.]])
Enter fullscreen mode Exit fullscreen mode

maximum() can get the zero or more maximum values from 2 tensors as shown below:

*Memos:

  • maximum() can be used with torch or a tensor.
  • maximum() can be used with 0D or more D tesnors.
  • 2 tensors must be the same size.
  • Only the tensor of zero or more integers, floating-point numbers or boolean values can be used so the tensor of zero or more complex numbers cannot be used.
import torch

tensor1 = torch.tensor([7, 1, 4])
tensor2 = torch.tensor([5, 3, 0])
torch.maximum(tensor1, tensor2)
tensor1.maximum(tensor2)
# tensor([7, 3, 4])

tensor1 = torch.tensor([[7, 1, 4], [5, 3, 0]])
tensor2 = torch.tensor([[3, 5, 1], [9, 0, 6]])
torch.maximum(tensor1, tensor2)
tensor1.maximum(tensor2)
# tensor([[7, 5, 4], [9, 3, 6]])

tensor1 = torch.tensor([[7., 1., 4.], [5., 3., 0.]])
tensor2 = torch.tensor([[3., 5., True], [9., False, 6.]])
torch.maximum(tensor1, tensor2)
tensor1.maximum(tensor2)
# tensor([[7., 5., 4.], [9., 3., 6.]])
Enter fullscreen mode Exit fullscreen mode

kthvalue() can get the one or more kth smallest elements and their indices of a 0D or more D tensor as shown below:

*Memos:

  • kthvalue() can be used with torch or a tensor.
  • Only the tensor of one or more integers, floating-point numbers or boolean values can be used so the tensor of zero or more complex numbers cannot be used.
  • The 2nd argument with torch or the 1st argument with a tensor is k(Required).
  • The 3rd argument with torch or the 2nd argument with a tensor is dim(Optional) which is a dimension.
  • The 4th argument with torch or the 3rd argument with a tensor is keepdim(Optional-Default:False) which keeps the dimension of the input tensor.
  • If there are the multiple same k th values, one is returned nondeterministically.
import torch

my_tensor = torch.tensor([5, 1, 9, 7, 6, 8, 0, 5])

torch.kthvalue(my_tensor, 3)
my_tensor.kthvalue(3)
torch.kthvalue(my_tensor, 3, 0)
my_tensor.kthvalue(3, 0)
torch.kthvalue(my_tensor, 3, -1)
my_tensor.kthvalue(3, -1)
# torch.return_types.kthvalue(
# values=tensor(5),
# indices=tensor(7))

torch.kthvalue(my_tensor, 3, 0, True)
# torch.return_types.kthvalue(
# values=tensor([5]),
# indices=tensor([7]))

torch.kthvalue(my_tensor, 4)
my_tensor.kthvalue(4)
torch.kthvalue(my_tensor, 4, 0)
my_tensor.kthvalue(4, 0)
torch.kthvalue(my_tensor, 4, -1)
my_tensor.kthvalue(4, -1)
# torch.return_types.kthvalue(
# values=tensor(5),
# indices=tensor(0))

torch.kthvalue(my_tensor, 4, 0, True)
# torch.return_types.kthvalue(
values=tensor([5]),
indices=tensor([0]))

my_tensor = torch.tensor([[5, 1, 9, 7],
                          [6, 8, 0, 5]])
torch.kthvalue(my_tensor, 3)
my_tensor.kthvalue(3)
torch.kthvalue(my_tensor, 3, 1)
my_tensor.kthvalue(3, 1)
torch.kthvalue(my_tensor, 3, -1)
my_tensor.kthvalue(3, -1)
# torch.return_types.kthvalue(
# values=tensor([7, 6]),
# indices=tensor([3, 0]))

torch.kthvalue(my_tensor, 3, 1, True)
# torch.return_types.kthvalue(
# values=tensor([[7], [6]]),
# indices=tensor([[3], [0]]))

my_tensor = torch.tensor([[5., True, 9., 7.],
                          [6, 8, False, 5]])
torch.kthvalue(my_tensor, 1)
my_tensor.kthvalue(1)
# torch.return_types.kthvalue(
# values=tensor([1., 0.]),
# indices=tensor([1, 2]))
Enter fullscreen mode Exit fullscreen mode

topk() can get the zero or more k largest or smallest elements and their indices of a 0D or more D tensor as shown below:

*Memos:

  • topk() can be used with torch or a tensor.
  • Only the tensor of one or more integers, floating-point numbers or boolean values can be used so the tensor of zero or more complex numbers cannot be used.
  • The 2nd argument with torch or the 1st argument with a tensor is k(Required).
  • The 3rd argument with torch or the 2nd argument with a tensor is dim(Optional) which is a dimension.
  • The 4th argument with torch or the 3rd argument with a tensor is largest(Optional-Default:True). *True gets the zero or more largest elements while False gets the zero or more smallest elements.
  • The 5th argument with torch or the 4th argument with a tensor is sorted(Optional-Default:True). *Sometimes, a return tensor is sorted with False but sometimes not so make it True if you want to definitely get a sorted tensor.
  • If there are the multiple same k values, one or more ones are returned nondeterministically.
import torch

my_tensor = torch.tensor([5, 1, 9, 7, 6, 8, 0, 5])

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

torch.topk(my_tensor, 3, 0, False)
my_tensor.topk(3, 0, False, True)
# torch.return_types.topk(
# values=tensor([0, 1, 5]),
# indices=tensor([6, 1, 0]))

torch.topk(my_tensor, 3, 0, False, False)
my_tensor.topk(3, 0, False, False)
# torch.return_types.topk(
# values=tensor([1, 0, 5]),
# indices=tensor([1, 6, 0]))

torch.topk(my_tensor, 4)
my_tensor.topk(4)
torch.topk(my_tensor, 4, 0)
my_tensor.topk(4, 0)
torch.topk(my_tensor, 4, -1)
my_tensor.topk(4, -1)
# torch.return_types.topk(
# values=tensor([9, 8, 7, 6]),
# indices=tensor([2, 5, 3, 4]))

torch.topk(my_tensor, 4, 0, False)
my_tensor.topk(4, 0, False)
# torch.return_types.topk(
# values=tensor([0, 1, 5, 5]),
# indices=tensor([6, 1, 0, 7]))

torch.topk(my_tensor, 4, 0, False, False)
my_tensor.topk(4, 0, False, False)
# torch.return_types.topk(
# values=tensor([1, 0, 5, 5]),
# indices=tensor([1, 6, 0, 7]))

my_tensor = torch.tensor([[5, 1, 9, 7],
                          [6, 8, 0, 5]])
torch.topk(my_tensor, 3)
my_tensor.topk(3)
torch.topk(my_tensor, 3, 1)
my_tensor.topk(3, 1)
torch.topk(my_tensor, 3, -1)
my_tensor.topk(3, -1)
# torch.return_types.topk(
# values=tensor([[9, 7, 5], [8, 6, 5]]),
# indices=tensor([[2, 3, 0], [1, 0, 3]]))

torch.topk(my_tensor, 3, 1, False)
my_tensor.topk(3, 1, False)
# torch.return_types.topk(
# values=tensor([[1, 5, 7], [0, 5, 6]]),
# indices=tensor([[1, 0, 3], [2, 3, 0]]))

torch.topk(my_tensor, 3, 1, False, False)
my_tensor.topk(3, 1, False, False)
# torch.return_types.topk(
# values=tensor([[1, 5, 7], [5, 0, 6]]),
# indices=tensor([[1, 0, 3], [3, 2, 0]]))

my_tensor = torch.tensor([[5., True, 9., 7.], [6, 8, False, 5]])
torch.topk(my_tensor, 4)
my_tensor.topk(4)
# torch.return_types.topk(
# values=tensor([[9., 7., 5., 1.], [8., 6., 5., 0.]]),
# indices=tensor([[2, 3, 0, 1], [1, 0, 3, 2]]))
Enter fullscreen mode Exit fullscreen mode

Top comments (0)