栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【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]))
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/829376.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号