DEV Community

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

Posted on • Edited on

cummin and cummax in PyTorch

Buy Me a Coffee

*Memos:

cummin() can get two of the 0D or more D tensors of zero or more cumulative minima and their indices from the 0D or more D tensors of zero or more elements as shown below:

*Memos:

  • cummin() can be used with torch or a tensor.
  • The 1st argument(input) with torch or using a tensor(Required-Type:tensor of int, float or bool).
  • The 2nd argument with torch or the 1st argument is dim(Required-Type:int).
  • There is out argument with torch(Optional-Default:None-Type:tuple(tensor, tensor) or list(tensor, tensor)): *Memos:
    • out= must be used.
    • My post explains out argument.
import torch

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

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

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

torch.cummin(input=my_tensor, dim=1)
torch.cummin(input=my_tensor, dim=-1)
# torch.return_types.cummin(
# values=tensor([[5, 5, 4, 4], [9, 1, 1, 0], [3, 3, 2, 2]]),
# indices=tensor([[0, 0, 2, 2], [0, 1, 1, 3], [0, 0, 2, 2]]))

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

my_tensor = torch.tensor([[True, False, True, False],
                          [False, True, False, True],
                          [True, False, True, False]])
torch.cummin(input=my_tensor, dim=0)
# torch.return_types.cummin(
# values=tensor([[True, False, True, False],
#                [False, False, False, False],
#                [False, False, False, False]]),
# indices=tensor([[0, 0, 0, 0],
#                 [1, 0, 1, 0],
#                 [1, 2, 1, 2]]))
Enter fullscreen mode Exit fullscreen mode

cummax() can get two of the 0D or more D tensors of zero or more cumulative maxima and their indices from the 0D or more D tensors of zero or more elements as shown below:

  • cummax() can be used with torch or a tensor.
  • The 1st argument(input) with torch or using a tensor(Required-Type:tensor of int, float or bool).
  • The 2nd argument with torch or the 1st argument is dim(Required-Type:int).
  • There is out argument with torch(Optional-Default:None-Type:tuple(tensor, tensor) or list(tensor, tensor)): *Memos:
    • out= must be used.
    • My post explains out argument.
import torch

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

torch.cummax(input=my_tensor, dim=0)
my_tensor.cummax(dim=0)
torch.cummax(input=my_tensor, dim=-1)
# torch.return_types.cummax(
# values=tensor([5, 8, 8, 8, 9, 9, 9, 9]),
# indices=tensor([0, 1, 1, 1, 4, 4, 4, 4]))

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

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

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

my_tensor = torch.tensor([[True, False, True, False],
                          [False, True, False, True],
                          [True, False, True, False]])
torch.cummax(input=my_tensor, dim=0)
# torch.return_types.cummax(
# values=tensor([[True, False, True, False],
#                [True, True, True, True],
#                [True, True, True, True]]),
# indices=tensor([[0, 0, 0, 0],
#                 [0, 1, 0, 1],
#                 [2, 1, 2, 1]]))
Enter fullscreen mode Exit fullscreen mode

Top comments (0)