torch.sort
torch.sort()
方法原型:
torch.sort(input, dim=None, descending=False, out=None) -> (Tensor, LongTensor)
返回值:
A tuple of (sorted_tensor, sorted_indices) is returned, where the sorted_indices are the indices of the elements in the original input tensor.
参数
- input (Tensor) – the input tensor
形式上与 numpy.narray 类似 - dim (int, optional) – the dimension to sort along
维度,对于二维数据:dim=0 按列排序,dim=1 按行排序,默认 dim=1 - descending (bool, optional) – controls the sorting order (ascending or descending)
降序,descending=True 从大到小排序,descending=False 从小到大排序,默认 descending=Flase
实例
import torch
x = torch.randn(3,4)
x #初始值,始终不变
tensor([[-0.9950, -0.6175, -0.1253, 1.3536],[ 0.1208, -0.4237, -1.1313, 0.9022],[-1.1995, -0.0699, -0.4396, 0.8043]])
sorted, indices = torch.sort(x) #按行从小到大排序
sorted
tensor([[-0.9950, -0.6175, -0.1253, 1.3536],[-1.1313, -0.4237, 0.1208, 0.9022],[-1.1995, -0.4396, -0.0699, 0.8043]])
indices
tensor([[0, 1, 2, 3],[2, 1, 0, 3],[0, 2, 1, 3]])
sorted, indices = torch.sort(x, descending=True) #按行从大到小排序 (即反序)
sorted
tensor([[ 1.3536, -0.1253, -0.6175, -0.9950],[ 0.9022, 0.1208, -0.4237, -1.1313],[ 0.8043, -0.0699, -0.4396, -1.1995]])
indices
tensor([[3, 2, 1, 0],[3, 0, 1, 2],[3, 1, 2, 0]])
sorted, indices = torch.sort(x, dim=0) #按列从小到大排序
sorted
tensor([[-1.1995, -0.6175, -1.1313, 0.8043],[-0.9950, -0.4237, -0.4396, 0.9022],[ 0.1208, -0.0699, -0.1253, 1.3536]])
indices
tensor([[2, 0, 1, 2],[0, 1, 2, 1],[1, 2, 0, 0]])
sorted, indices = torch.sort(x, dim=0, descending=True) #按列从大到小排序
sorted
tensor([[ 0.1208, -0.0699, -0.1253, 1.3536],[-0.9950, -0.4237, -0.4396, 0.9022],[-1.1995, -0.6175, -1.1313, 0.8043]])
indices
tensor([[1, 2, 0, 0],[0, 1, 2, 1],[2, 0, 1, 2]])
官方文档:https://pytorch.org/docs/stable/torch.html
torch.sort相关推荐
- PyTorch 笔记(08)— Tensor 比较运算(torch.gt、lt、ge、le、eq、ne、torch.topk、torch.sort、torch.max、torch.min)
1. 常用函数 比较函数中有一些是逐元素比较,操作类似逐元素操作,还有一些类似归并操作,常用的比较函数如下表所示. 表中第一行的比较操作已经实现了运算符重载,因此可以使用 a>=b,a>b ...
- 代码阅读 | torch.sort函数
知识点https://blog.csdn.net/u012495579/article/details/106117511网友讲得非常清晰了. 代码如下: lengths = [17,17,19,23 ...
- torch.sort()用法
torch.sort()用法 torch.sort(input, dim=- 1, descending=False, stable=False, out=None) 这个函数用于将输入的tensor ...
- pytorch torch.sort()的用法以及例子
1.作用 对tensor中元素排序 2.用法 dim = -1,按照行排序,dim= 1按照列排序,descending=True,则递减排序,否则递增 3.例子 按照行排序 logits = tor ...
- 学习笔记CB012: LSTM 简单实现、完整实现、torch、小说训练word2vec lstm机器人
摘要: 真正掌握一种算法,最实际的方法,完全手写出来. LSTM(Long Short Tem Memory)特殊递归神经网络,神经元保存历史记忆,解决自然语言处理统计方法只能考虑最近n个词语而忽略更 ...
- torch 深度学习(3)
torch 深度学习(3) 损失函数,模型训练 前面我们已经完成对数据的预处理和模型的构建,那么接下来为了训练模型应该定义模型的损失函数,然后使用BP算法对模型参数进行调整 损失函数 Criterio ...
- Torch深度学习的60分钟教程(翻译)
这篇博客是官方Tutorial的翻译(加一丢丢自己的见解吧),原文链接为https://github.com/soumith/cvpr2015/blob/master/Deep%20Learning% ...
- python torch exp_PyTorch官方中文文档:torch
torch 包 torch 包含了多维张量的数据结构以及基于其上的多种数学操作.另外,它也提供了多种工具,其中一些可以更有效地对张量和任意类型进行序列化. 它有CUDA 的对应实现,可以在NVIDIA ...
- 小白学Pytorch系列--Torch API (7)
小白学Pytorch系列–Torch API (7) Comparison Ops allclose 此函数检查输入和其他是否满足条件: >>> torch.allclose(tor ...
- pytorch常用代码
20211228 https://mp.weixin.qq.com/s/4breleAhCh6_9tvMK3WDaw 常用代码段 本文代码基于 PyTorch 1.x 版本,需要用到以下包: impo ...
最新文章
- hibernate.hbm.xml配置文件解析
- acctmod-ftp.sh
- python之lxml处理xml
- Android笔记(二十五) ListView的缓存机制与BaseAdapter
- python 运行日志logging代替方案
- 软件工程团队项目方案(四):高校申请指南
- pandas筛选某个列值是否位于某个列表内
- linux多个svn仓库权限管理,svn建立仓库而且设置用户的权限
- 【备忘】老罗Android开发视频教程[共9大知识点] 下载
- Carhart四因子模型实用攻略
- mysql sql语句 参数化_参数化SQL语句
- 2018年马哥Linux
- 短视频从业者实操资料大全(含代运营案例+报价单+合同协议 +分镜头脚本+管理运营)共180份
- 对话DeepMind创始人:建立通用人工智能
- 产品和运营区别是什么,别迷惑了。
- HTML网页入门练习——淘宝抢购模块设计
- word编写页码,取消封面编码
- 三个选择框,当前框选过之后的数据其他两个不能选择
- 开发岗校招求职攻略——面试准备(7.2胸有成竹-技术面技巧)
- 微商城系统之商家平台任务自动分析处理系统_OctShop
热门文章
- python中用来返回序列的最大函数_Python内置函数____________用来返回序列中的最大元素。...
- PyTorch 和 Albumentations 实现图像分类(猫狗大战)
- C语言四分位数Quartile算法(附完整源码)
- Vue 开源项目库汇总
- 《如何正确评估自己的工作能力》--马薇薇
- 计算机软件实习每日学习打卡(2)20201203
- 浙大python网_Python爬虫学习(7):浙大软院网号嗅探
- 钢筋穿入女子太阳穴 消防及时破拆将其营救
- signature=735f4378ec01919f23285d0d2557be19,OPENSSL编程 第二十章 椭圆曲线
- 身份证号码中间位数隐藏