TUTORIALAI 与数据科学FETCHED

PyTorch torch.argsort 函数

PyTorch torch.argsort 函数 Pytorch torch 参考手册 torch.argsort 是 PyTorch 中用于返回排序后索引的函数。它返回将张量排序后各元素原位置的索引。 函数定义 torch.argsort(input, dim=-1, descending=False, stable=True) 使用示例 实例 [mycode4 type='python'] i..

更新于 2026/7/16

教程详情页默认 noindex。

PyTorch torch.argsort 函数


Pytorch torch 参考手册 Pytorch torch 参考手册

torch.argsort 是 PyTorch 中用于返回排序后索引的函数。它返回将张量排序后各元素原位置的索引。

函数定义

torch.argsort(input, dim=-1, descending=False, stable=True)

使用示例

实例

import torch

# 创建张量
x = torch.tensor([3, 1, 2])

# 返回排序后的索引
indices = torch.argsort(x)
print(indices)

输出结果为:

tensor([1, 2, 0])

Pytorch torch 参考手册 Pytorch torch 参考手册