pytorch中的torch.sort()

1.用法介绍

pytorch中的torch.sort()是按照指定的维度对tensor张量的元素进行排序。

torch.sort(input, dim=-1, descending=False, stable=False, * ,out =None)
输入:

  • input(tensor): 表示输入的张量
  • dim(int, optional)表示张量元素排序的维度
  • descending(bool, optional)0表示升序,1表示降序
  • stable(bool, optional)保持张量中相等元素的顺序

输出:

  • 第一个tensor表示对应维度张量值排序;第二个tensor表示对应维度张量值得索引序列

2. 代码实例

torch.sort()的代码实例如下所示:

import torch

torch.manual_seed(2)
Tensor = torch.rand((3,3))
print(Tensor)

print(torch.sort(Tensor,0)) # print(Tensor.sort(0))
print(torch.sort(Tensor,1)) # print(Tensor.sort(1))

运行的实验结果如下所示:

tensor([[0.6147, 0.3810, 0.6371],
        [0.4745, 0.7136, 0.6190],
        [0.4425, 0.0958, 0.6142]])
torch.return_types.sort(
values=tensor([[0.4425, 0.0958, 0.6142],
        [0.4745, 0.3810, 0.6190],
        [0.6147, 0.7136, 0.6371]]),
indices=tensor([[2, 2, 2],
        [1, 0, 1],
        [0, 1, 0]]))
torch.return_types.sort(
values=tensor([[0.3810, 0.6147, 0.6371],
        [0.4745, 0.6190, 0.7136],
        [0.0958, 0.4425, 0.6142]]),
indices=tensor([[1, 0, 2],
        [0, 2, 1],
        [1, 0, 2]]))
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

道2024

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值