【Pytorch】torch.sort()方法使用

官方文档地址:https://pytorch.org/docs/stable/generated/torch.sort.html?highlight=sort#torch.sort

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

作用:沿给定维度按值升序对输入张量的元素进行排序。如果没有给出 dim,则选择输入的最后一个维度。如果descending 为True,则元素按值降序排序。如果 stable 为 True,则排序例程变得稳定,保持等效元素的顺序。返回 (values, indices) 的命名元组,其中值是排序后的值,索引是原始输入张量中元素的索引。

参数:

  • input (Tensor) – the input tensor.

  • dim (int, optional) – the dimension to sort along

  • descending (bool, optional) – controls the sorting order (ascending or descending)

  • stable (bool, optional) – makes the sorting routine stable, which guarantees that the order of equivalent elements is preserved.

返回值:
out (tuple, optional) – the output tuple of (Tensor, LongTensor) that can be optionally given to be used as output buffers

使用案例如下:

x = torch.randn(3, 4)
sorted, indices = torch.sort(x)
sorted, indices = torch.sort(x, 0)
x = torch.tensor([0, 1] * 9)
x.sort()
x.sort(stable=True)
# torch.return_types.sort(
#    values=tensor([0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1]),
#    indices=tensor([ 0,  2,  4,  6,  8, 10, 12, 14, 16,  1,  3,  5,  7,  9, 11, 13, 15, 17]))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

智绘山河

你的鼓励可能解决你下一个问题

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

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

打赏作者

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

抵扣说明:

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

余额充值