PyTorch 笔记(08)— Tensor 比较运算(torch.gt、lt、ge、le、eq、ne、torch.topk、torch.sort、torch.max、torch.min)
1. 常用函数
比较函数中有一些是逐元素比较,操作类似逐元素操作,还有一些类似归并操作,常用的比较函数如下表所示。
表中第一行的比较操作已经实现了运算符重载,因此可以使用 a>=b
,a>b
,a !=b
和 a == b
,其返回的结果是一个 ByteTensor
,可用来选取元素。
max/min
操作有些特殊,以 max
为例,有以下三种使用情况:
t.max(tensor)
: 返回tensor
中最大的一个数;t.max(tensor,dim)
: 指定维上最大数,返回tensor
和下标;t.max(tensor1,tensor2)
: 比较两个tensor
相比较大的元素;
2. 使用示例
torch.gt
、torch.lt
、torch.ge
、torch.le
、torch.eq
、torch.ne
的函数参数和返回值是类似的,都如下所示:
Args:
input (Tensor): the tensor to compare
other (Tensor or float): the tensor or value to compare
out (Tensor, optional): the output tensor that must be aBoolTensor
Returns:
Tensor: Atorch.BoolTensor
containing a True at each location where comparison is true
2.1 torch.gt
In [1]: import torch as tIn [2]: a = t.Tensor([[1,2],[3,4]])In [3]: a
Out[3]:
tensor([[1., 2.],[3., 4.]])In [4]: a.gt(4)
Out[4]:
tensor([[False, False],[False, False]])In [7]: a.gt(t.Tensor([[1,1], [3, 3]]))
Out[7]:
tensor([[False, True],[False, True]])
2.2 torch.lt
函数参数同 torch.gt
。
In [9]: a.lt(4)
Out[9]:
tensor([[ True, True],[ True, False]])In [10]: a.lt(t.Tensor([[1,1], [3, 3]]))
Out[10]:
tensor([[False, False],[False, False]])In [11]:
2.3 torch.ge
In [12]: a.ge(4)
Out[12]:
tensor([[False, False],[False, True]])In [13]: a.ge(t.Tensor([[1,1], [3, 3]]))
Out[13]:
tensor([[True, True],[True, True]])
2.4 torch.le
In [14]: a.le(4)
Out[14]:
tensor([[True, True],[True, True]])In [15]: a.le(t.Tensor([[1,1], [3, 3]]))
Out[15]:
tensor([[ True, False],[ True, False]])In [16]:
2.5 torch.eq
In [16]: a.eq(4)
Out[16]:
tensor([[False, False],[False, True]])In [17]: a.eq(t.Tensor([[1,1], [3, 3]]))
Out[17]:
tensor([[ True, False],[ True, False]])
2.6 torch.ne
In [18]: a.ne(4)
Out[18]:
tensor([[ True, True],[ True, False]])In [19]: a.ne(t.Tensor([[1,1], [3, 3]]))
Out[19]:
tensor([[False, True],[False, True]])
2.7 torch.topk
函数定义如下:
topk(input, k, dim=None, largest=True, sorted=True, out=None) -> (Tensor, LongTensor)
参数说明:
Returns the :attr:`k` largest elements of the given :attr:`input` tensor alonga given dimension.If :attr:`dim` is not given, the last dimension of the `input` is chosen.If :attr:`largest` is ``False`` then the `k` smallest elements are returned.A namedtuple of `(values, indices)` is returned, where the `indices` are the indicesof the elements in the original `input` tensor.The boolean option :attr:`sorted` if ``True``, will make sure that the returned`k` elements are themselves sortedArgs:input (Tensor): the input tensor.k (int): the k in "top-k"dim (int, optional): the dimension to sort alonglargest (bool, optional): controls whether to return largest orsmallest elementssorted (bool, optional): controls whether to return the elementsin sorted orderout (tuple, optional): the output tuple of (Tensor, LongTensor) that can beoptionally given to be used as output buffers
In [21]: a
Out[21]:
tensor([[1., 2.],[3., 4.]])In [22]: a.topk(2)
Out[22]:
torch.return_types.topk(
values=tensor([[2., 1.],[4., 3.]]),
indices=tensor([[1, 0],[1, 0]]))In [24]: a.topk(1, dim=1)
Out[24]:
torch.return_types.topk(
values=tensor([[2.],[4.]]),
indices=tensor([[1],[1]]))In [25]:
2.8 torch.sort
函数定义如下:
sort(input, dim=-1, descending=False, out=None) -> (Tensor, LongTensor)
函数参数如下:
Sorts the elements of the :attr:`input` tensor along a given dimensionin ascending order by value.If :attr:`dim` is not given, the last dimension of the `input` is chosen.If :attr:`descending` is ``True`` then the elements are sorted in descendingorder by value.A namedtuple of (values, indices) is returned, where the `values` are thesorted values and `indices` are the indices of the elements in the original`input` tensor.Args:input (Tensor): the input tensor.dim (int, optional): the dimension to sort alongdescending (bool, optional): controls the sorting order (ascending or descending)out (tuple, optional): the output tuple of (`Tensor`, `LongTensor`) that canbe optionally given to be used as output buffers
In [28]: b = t.randn(2,3)In [29]: b
Out[29]:
tensor([[-0.1936, -1.8862, 0.1491],[ 0.8152, 1.1863, -0.4711]])In [30]: b.sort()
Out[30]:
torch.return_types.sort(
values=tensor([[-1.8862, -0.1936, 0.1491],[-0.4711, 0.8152, 1.1863]]),
indices=tensor([[1, 0, 2],[2, 0, 1]]))In [31]: b.sort(dim=0)
Out[31]:
torch.return_types.sort(
values=tensor([[-0.1936, -1.8862, -0.4711],[ 0.8152, 1.1863, 0.1491]]),
indices=tensor([[0, 0, 1],[1, 1, 0]]))In [32]:
2.9 torch.max
函数定义如下:
max(input, dim, keepdim=False, out=None) -> (Tensor, LongTensor)
函数参数如下:
Args:input (Tensor): the input tensor.dim (int): the dimension to reduce.keepdim (bool): whether the output tensor has :attr:`dim` retained or not. Default: ``False``.out (tuple, optional): the result tuple of two output tensors (max, max_indices)
或者:
Args:input (Tensor): the input tensor.other (Tensor): the second input tensorout (Tensor, optional): the output tensor.
示例如下:
In [2]: import torch as tIn [4]: a = t.Tensor([[1,2], [3,4]])In [5]: a
Out[5]:
tensor([[1., 2.],[3., 4.]])In [7]: a.max()
Out[7]: tensor(4.)In [8]: b = t.Tensor([[2,0], [2,6]])In [9]: a.max(b)
Out[9]:
tensor([[2., 2.],[3., 6.]])In [10]: a.max(dim=0)
Out[10]:
torch.return_types.max(
values=tensor([3., 4.]),
indices=tensor([1, 1]))In [11]: a.max(dim=1)
Out[11]:
torch.return_types.max(
values=tensor([2., 4.]),
indices=tensor([1, 1]))In [12]:
2.10 torch.min
函数定义如下:
min(input, dim, keepdim=False, out=None) -> (Tensor, LongTensor)
min(input, other, out=None) -> Tensor
min(input) -> Tensor
函数参数如下:
Args:input (Tensor): the input tensor.dim (int): the dimension to reduce.keepdim (bool): whether the output tensor has :attr:`dim` retained or not.out (tuple, optional): the tuple of two output tensors (min, min_indices)
或者:
Args:input (Tensor): the input tensor.other (Tensor): the second input tensorout (Tensor, optional): the output tensor.
使用示例:
In [13]: a = t.Tensor([[1,2], [3,4]])In [14]: a
Out[14]:
tensor([[1., 2.],[3., 4.]])In [15]: a.min()
Out[15]: tensor(1.)In [16]: b = t.Tensor([[2,0], [2,6]])In [17]: b
Out[17]:
tensor([[2., 0.],[2., 6.]])In [18]: a.min(b)
Out[18]:
tensor([[1., 0.],[2., 4.]])In [19]: a.min(dim=0)
Out[19]:
torch.return_types.min(
values=tensor([1., 2.]),
indices=tensor([0, 0]))In [20]: a.min(dim=1)
Out[20]:
torch.return_types.min(
values=tensor([1., 3.]),
indices=tensor([0, 0]))In [21]:
PyTorch 笔记(08)— Tensor 比较运算(torch.gt、lt、ge、le、eq、ne、torch.topk、torch.sort、torch.max、torch.min)相关推荐
- PyTorch 笔记(20)— torchvision 的 datasets、transforms 数据预览和加载、模型搭建(torch.nn.Conv2d/MaxPool2d/Dropout)
计算机视觉是深度学习中最重要的一类应用,为了方便研究者使用,PyTorch 团队专门开发了一个视觉工具包torchvision,这个包独立于 PyTorch,需通过 pip instal torchv ...
- pytorch 笔记:torchsummary
作用:打印神经网络的结构 以pytorch笔记:搭建简易CNN_UQI-LIUWJ的博客-CSDN博客 中搭建的CNN为例 import torch from torchsummary import ...
- PyTorch 笔记(07)— Tensor 的归并运算(torch.mean、sum、median、mode、norm、dist、std、var、cumsum、cumprod)
1. Tensor 归并运算函数 此类操作会使输出形状小于输入形状,并可以沿着某一维度进行指定操作,如加法, 既可以计算整个 tensor 的和,也可以计算 tensor 每一行或者 每一列的和, 常 ...
- PyTorch 笔记(02)— 常用创建 Tensor 方法(torch.Tensor、ones、zeros、eye、arange、linspace、rand、randn、new)
1. Tensor 概念分类 PyTorch 中的张量(Tensor)类似 NumPy 中的 ndarrays,之所以称之为 Tensor 的另一个原因是它可以运行在 GPU 中,以加速运算. 1.1 ...
- pytorch tensor 初始化_PyTorch简明笔记[1]-Tensor的初始化和基本操作
听麻麻说,偷偷收藏而不感谢是不礼貌的,至少应该点个赞~我觉得麻麻说的对! 不断地被人安利PyTorch,终于忍不住诱惑决定入坑了. 当初学习TensorFlow的时候,没有系统性地学习.之前TF的英文 ...
- 【 线性回归 Linear-Regression torch模块实现与源码详解 深度学习 Pytorch笔记 B站刘二大人(4/10)】
torch模块实现与源码详解 深度学习 Pytorch笔记 B站刘二大人 深度学习 Pytorch笔记 B站刘二大人(4/10) 介绍 至此开始,深度学习模型构建的预备知识已经完全准备完毕. 从本章开 ...
- PyTorch 笔记(19)— Tensor 用 GPU 加速
在 PyTorch 中以下数据结构分为 CPU 和 GPU 两个版本: Tensor nn.Module (包括常用的 layer .loss function ,以及容器 Sequential 等) ...
- torch的拼接函数_从零开始深度学习Pytorch笔记(13)—— torch.optim
前文传送门: 从零开始深度学习Pytorch笔记(1)--安装Pytorch 从零开始深度学习Pytorch笔记(2)--张量的创建(上) 从零开始深度学习Pytorch笔记(3)--张量的创建(下) ...
- 【Torch笔记】Tensor 简介与创建方法
[Torch笔记]Tensor 1 什么是 Tensor? Tensor,又称张量,它是矩阵向任意维度的推广. [Tensor 与 Variable(目前已弃用,但最好了解一下)] Variable ...
最新文章
- 342.基于高通量技术的微生物组研究实验设计
- 2012是团购移动电商年
- 【Android 安装包优化】Tint 着色器 ( 简介 | 布局文件中的 Tint 着色器基本用法 | 代码中使用 Tint 着色器添加颜色效果 )
- Redis-06Redis数据结构--集合Set
- 一些常用正则表达解析
- flex 图片上传并以二进制保存到oracle数据库,flex 加载并显示图片 图片转化成二进制...
- BZOJ3884 上帝与集合的正确用法 【欧拉定理】
- 速成pytorch学习——4天中阶API示范
- 实验3-3 比较大小 (10 分)
- mysql数据签名功能_分析型数据库 MySQL的签名机制有哪些?
-问答-阿里云开发者社区-阿里云...
- 无人驾驶到底怎么赚钱?很现实,八仙过海,各显神通
- 芝诺数解|「七」月是故乡明,月饼表浓情
- H5案例分享:html5移动开发细微之美
- epub文件是什么文件?如何在windows系统上打开?
- 英伟达 Nano 新手必读:Jetson Nano 深度学习算法模型基准性能测评
- java爬虫写一个百度图片下载器
- 【区块链 | 前端】前端开发人员入门区块链的最佳实践
- 【FPGA】一些基本模块代码
- Hack The Box——Scavenger
- ERP,中国软件的一代发展史