YouTip LogoYouTip

Pytorch Torch Topk

# PyTorch torch.topk Function * * * [![Image 3: Pytorch torch Reference Manual](https://example.com/images/up.gif) Pytorch torch Reference Manual](https://example.com/pytorch/pytorch-torch-ref.html) `torch.topk` is a function in PyTorch used to return the largest k elements and their indices. ### Function Definition torch.topk(input, k, dim, largest, sorted, out) * * * ## Usage Example ## Example import torch x = torch.tensor([[3,1,2],[6,4,5]]) # Return the largest 2 elements values, indices = torch.topk(x, k=2) print("Largest 2 values:") print(values) print("Corresponding indices:") print(indices) Output: Largest 2 values: tensor([[3, 2], [6, 5]])Corresponding indices: tensor([[0, 2], [0, 2]]) * * * [![Image 4: Pytorch torch Reference Manual](https://example.com/images/up.gif) Pytorch torch Reference Manual](https://example.com/pytorch/pytorch-torch-ref.html)
← Pytorch Torch TransposePytorch Torch Slogdet β†’