min(), max(), aminmax(), amin(), amax(), argmin() and argmax() in PyTorch

min() can get one or more minimum values as shown below:

*Memos:

  • min() can be called both from torch and a tensor.
  • Setting a dimension(dim) to the 2nd argument with torch or the 1st argument with a tensor gets zero or more 1st minimum values and the indices of them.
import torch my_tensor = torch.tensor([[5, 4, 7, 7], [6, 5, 3, 5], [3, 8, 9, 3]])
torch.min(my_tensor)
my_tensor.min()
# tensor(3)

torch.min(my_tensor, 0)
my_tensor.min(0)
torch.min(my_tensor, -2)
my_tensor.min(-2)
# torch.return_types.min(
# values=tensor([3, 4, 3, 3]),
# indices=tensor([2, 0, 1, 2]))

torch.min(my_tensor, 1)
my_tensor.min(1)
torch.min(my_tensor, -1)
my_tensor.min(-1)
# torch.return_types.min(
# values=tensor([4, 3, 3]),
# indices=tensor([1, 2, 0]))
Enter fullscreen mode
Exit fullscreen mode

max() can get one or more maximum values as shown below:

*Memos:

  • max() can be called both from torch and a tensor.
  • Setting a dimension(dim) to the 2nd argument with torch or the 1st argument with a tensor gets zero or more 1st maximum values and the indices of them.
import torch my_tensor = torch.tensor([[5, 4, 7, 7], [6, 5, 3, 5], [3, 8, 9, 3]])
torch.max(my_tensor)
my_tensor.max()
# tensor(9)

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

torch.max(my_tensor, 1)
my_tensor.max(1)
torch.max(my_tensor, -1)
my_tensor.max(-1)
# torch.return_types.max(
# values=tensor([7, 6, 9]),
# indices=tensor([2, 0, 2]))
Enter fullscreen mode
Exit fullscreen mode

aminmax() can get one or more minimum and maximum values as shown below:

*Memos:

  • aminmax() can be called both from torch and a tensor.
  • Setting a dimension(dim) to the 2nd argument with torch or the 1st argument with a tensor gets zero or more 1st minimum and maximum values. *You must use the keyword dim=.
import torch my_tensor = torch.tensor([[5, 4, 7, 7], [6, 5, 3, 5], [3, 8, 9, 3]])
torch.aminmax(my_tensor)
my_tensor.aminmax()
# torch.return_types.aminmax(
# min=tensor(3),
# max=tensor(9))

torch.aminmax(my_tensor, dim=0)
my_tensor.aminmax(dim=0)
torch.aminmax(my_tensor, dim=-2)
my_tensor.aminmax(dim=-2)
# torch.return_types.aminmax(
# min=tensor([3, 4, 3, 3]),
# max=tensor([6, 8, 9, 7]))

torch.aminmax(my_tensor, dim=1)
my_tensor.aminmax(dim=1)
torch.aminmax(my_tensor, dim=-1)
my_tensor.aminmax(dim=-1)
# torch.return_types.aminmax(
# min=tensor([4, 3, 3]),
# max=tensor([7, 6, 9]))
Enter fullscreen mode
Exit fullscreen mode

amin() can get one or more minimum values as shown below:

*Memos:

  • amin() can be called both from torch and a tensor.
  • Setting a dimension(dim) to the 2nd argument with torch or the 1st argument with a tensor gets zero or more 1st minimum values. *You must use the keyword dim=.
import torch my_tensor = torch.tensor([[5, 4, 7, 7], [6, 5, 3, 5], [3, 8, 9, 3]])
torch.amin(my_tensor)
my_tensor.amin()
# tensor(3)

torch.amin(my_tensor, dim=0)
my_tensor.amin(dim=0)
torch.amin(my_tensor, dim=-2)
my_tensor.amin(dim=-2)
# tensor([3, 4, 3, 3])

torch.amin(my_tensor, dim=1)
my_tensor.amin(dim=1)
torch.amin(my_tensor, dim=-1)
my_tensor.amin(dim=-1)
# tensor([4, 3, 3])
Enter fullscreen mode
Exit fullscreen mode

amax() can get one or more maximum values as shown below:

*Memos:

  • amax() can be called both from torch and a tensor.
  • Setting a dimension(dim) to the 2nd argument with torch or the 1st argument with a tensor gets zero or more 1st maximum values. *You must use the keyword dim=.
import torch my_tensor = torch.tensor([[5, 4, 7, 7], [6, 5, 3, 5], [3, 8, 9, 3]])
torch.amax(my_tensor)
my_tensor.amax()
# tensor(9)

torch.amax(my_tensor, dim=0)
my_tensor.amax(dim=0)
torch.amax(my_tensor, dim=-2)
my_tensor.amax(dim=-2)
# tensor([6, 8, 9, 7])

torch.amax(my_tensor, dim=1)
my_tensor.amax(dim=1)
torch.amax(my_tensor, dim=-1)
my_tensor.amax(dim=-1)
# tensor([7, 6, 9])
Enter fullscreen mode
Exit fullscreen mode

argmin() can get the indices of the 1st minimum values as shown below:

*Memos:

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

torch.argmin(my_tensor, 0)
my_tensor.argmin(0)
torch.argmin(my_tensor, -2)
my_tensor.argmin(-2)
# tensor([2, 0, 1, 2])

torch.argmin(my_tensor, 1)
my_tensor.argmin(1) torch.argmin(my_tensor, -1)
my_tensor.argmin(-1) # tensor([1, 2, 0])
Enter fullscreen mode
Exit fullscreen mode

argmax() can get the indices of the 1st maximum values:

*Memos:

  • argmax() can be called both from torch and a tensor.
  • The 2nd argument is a dimension(dim) with torch.
  • The 1st argument is a dimension(dim) with a tensor.
import torch my_tensor = torch.tensor([[5, 4, 7, 7], [6, 5, 3, 5], [3, 8, 9, 3]])
torch.argmax(my_tensor)
my_tensor.argmax()
# tensor(10)

torch.argmax(my_tensor, 0)
my_tensor.argmax(0)
torch.argmax(my_tensor, -2)
my_tensor.argmax(-2)
# tensor([1, 2, 2, 0])

torch.argmax(my_tensor, 1)
my_tensor.argmax(1)
torch.argmax(my_tensor, -1)
my_tensor.argmax(-1)
# tensor([2, 0, 2])
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
Install docker compose ubuntu. Contact us zytrix zytrix training. Cloudystorage offers affordable online storage solutions tailored to meet the needs of both individual users and businesses.