import torch t = torch.tensor([1, 5, 3, 9, 2]) index = torch.argmax(t) print(index) # output: tensor(3)
import torch t = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) index = torch.argmax(t, dim=0) print(index) # output: tensor([2, 2, 2])In this example, we create a 2D tensor `t` and use the `argmax` function to find the index with the highest value along the first dimension (i.e. column-wise). The resulting tensor, `index`, contains the index with the highest value for each column of `t`. Package library: PyTorch.