Pytorch学习之torch用法----比较操作(Comparison Ops)


Posted in Python onJune 28, 2020

1. torch.eq(input, other, out=None)

说明: 比较元素是否相等,第二个参数可以是一个数,或者是第一个参数同类型形状的张量

参数:

input(Tensor) ---- 待比较张量

other(Tenosr or float) ---- 比较张量或者数

out(Tensor,可选的) ---- 输出张量

返回值: 一个torch.ByteTensor张量,包含了每个位置的比较结果(相等为1,不等为0)

>>> a = torch.Tensor([[1, 2], [3, 4]])
>>> b = torch.Tensor([[1, 1], [4, 4]])
>>> torch.eq(a, b)
tensor([[1, 0],
  [0, 1]], dtype=torch.uint8)

2. torch.equal(tensor1, tensor2, out=None)

说明: 如果两个张量有相同的形状和元素值,则返回true,否则False

参数:

tensor1(Tenosr) ---- 比较张量1

tensor2(Tensor) ---- 比较张量2

out(Tensor,可选的) ---- 输出张量

>>> a = torch.Tensor([1, 2])
>>> b = torch.Tensor([1, 2])
>>> torch.equal(a, b)
True

3. torch.ge(input, other, out=None)

说明: 逐元素比较input和other,即是否input >= other。

参数:

input(Tensor) ---- 待对比的张量

other(Tensor or float) ---- 对比的张量或float值

out(Tensor,可选的) ---- 输出张量,

>>> a = torch.Tensor([[1, 2], [3, 4]])
>>> b = torch.Tensor([[1, 1], [4, 4]])
>>> torch.ge(a, b)
tensor([[1, 1],
  [0, 1]], dtype=torch.uint8)

4. torch.gt(input, other, out=None)

说明: 逐元素比较input和other,即是否input > other

参数:

input(Tensor) ---- 要对比的张量

other(Tensor or float) ---- 要对比的张量或float值

out(Tensor,可选的) ---- 输出张量

>>> a = torch.Tensor([[1, 2], [3, 4]])
>>> b = torch.Tensor([[1, 1], [4, 4]])
>>> torch.gt(a, b)
tensor([[0, 1],
  [0, 0]], dtype=torch.uint8)

5. torch.kthvalue(input, k, dim=None, out=None)

说明: 取输入张量input指定维度上第k个最小值。如果不指定dim。默认为最后一维。返回一个元组(value, indices), 其中indices是原始输入张量中沿dim维的第k个最小值下标。

参数:

input(Tensor) ---- 要对比的张量

k(int) ---- 第k个最小值

dim(int, 可选的) ---- 沿着此维度进行排序

out(tuple,可选的) ---- 输出元组

>>> x = torch.arange(1, 6)
>>> x
tensor([1, 2, 3, 4, 5])
>>> torch.kthvalue(x, 4)
torch.return_types.kthvalue(
values=tensor(4),
indices=tensor(3))
>>> torch.kthvalue(x, 1)
torch.return_types.kthvalue(
values=tensor(1),
indices=tensor(0))

6. torch.le(input, other, out=None)

说明: 逐元素比较input和other,即是否input <= other.

参数:

input(Tenosr) ---- 要对比的张量

other(Tensor or float) ---- 对比的张量或float值

out(Tensor,可选的) ---- 输出张量

>>> a = torch.Tensor([[1, 2], [3, 4]])
>>> b = torch.Tensor([[1, 1], [4, 4]])
>>> torch.le(a, b)
tensor([[1, 0],
  [1, 1]], dtype=torch.uint8)

7. torch.lt(input, other, out=None)

说明: 逐元素比较input和other,即是否input < other

参数:

input(Tensor) ---- 要对比的张量

other(Tensor or float) ---- 对比的张量或float值

out(Tensor,可选的) ---- 输出张量

>>> a = torch.Tensor([[1, 2], [3, 4]])
>>> b = torch.Tensor([[1, 1], [4, 4]])
>>> torch.lt(a, b)
tensor([[0, 0],
  [1, 0]], dtype=torch.uint8)

8. torch.max(input)

说明: 返回输入张量所有元素的最大值

参数:

input(Tensor) ---- 输入张量

>>> a = torch.randn(1, 3)
>>> a
tensor([[ 0.1553, -0.4140, 1.8393]])
>>> torch.max(a)
tensor(1.8393)

9. torch.max(input, dim, max=None, max_indices=None)

说明: 返回输入张量给定维度上每行的最大值,并同时返回每个最大值的位置索引。

参数:

input(Tensor) ---- 输入张量

dim(int) ---- 指定的维度

max(Tensor,可选的) ---- 结果张量,包含给定维度上的最大值

max_indices(LongTensor,可选的) ---- 结果张量,包含给定维度上每个最大值的位置的索引。

>>> a = torch.randn(4, 4)
>>> a
tensor([[ 0.4067, -0.7722, -0.6560, -0.9621],
  [-0.8754, 0.0282, -0.7947, -0.1870],
  [ 0.4300, 0.5444, 0.3180, 1.2647],
  [ 0.0775, 0.5886, 0.1662, 0.8986]])
>>> torch.max(a, 1)
torch.return_types.max(
values=tensor([0.4067, 0.0282, 1.2647, 0.8986]),
indices=tensor([0, 1, 3, 3]))

10. torch.max(input, other, out=None)

说明: 返回两个元素的最大值。

参数:

input(Tensor) ---- 待比较张量

other(Tensor) ---- 比较张量

out(Tensor,可选的) ---- 结果张量

>>> a = torch.randn(4)
>>> a
tensor([ 0.5767, -1.0841, -0.0942, -0.9405])
>>> b = torch.randn(4)
>>> b
tensor([-0.6375, 1.4165, 0.2738, -0.8996])
>>> torch.max(a, b)
tensor([ 0.5767, 1.4165, 0.2738, -0.8996])

11.torch.min(input)

说明: 返回输入张量所有元素的最小值

参数:

input(Tensor) ---- 输入张量

>>> a = torch.randn(1, 4)
>>> a
tensor([[-0.8142, -0.9847, -0.3637, 0.5191]])
>>> torch.min(a)
tensor(-0.9847)

12. torch.min(input, dim, min=None, min_indices=None)

说明: 返回输入张量给定维度上每行的最小值,并同时返回每个最小值的位置索引

参数:

input(Tensor) ---- 输入张量

dim(int) ---- 指定的维度

min(Tensor,可选的) ---- 结果张量,包含给定维度上的最小值

min_indices(LongTensor,可选的) ---- 结果张量,包含给定维度上每个最小值的位置索引。

>>> a = torch.randn(4, 4)
>>> a
tensor([[-0.0243, -0.7382, 0.3102, 0.9720],
  [-0.3805, -0.7999, -1.2856, 0.2657],
  [-1.0284, -0.1638, -0.8840, 1.2679],
  [-1.0347, -2.3428, 0.3107, 1.0575]])
>>> torch.min(a, 1)
torch.return_types.min(
values=tensor([-0.7382, -1.2856, -1.0284, -2.3428]),
indices=tensor([1, 2, 0, 1]))

13. torch.ne(input, other, out=None)

说明: 逐元素比较input和other,即是否input 不等于 other。第二个参数可以为一个数或与第一个参数相同形状和类型的张量

参数:

input(Tensor) ---- 待对比的张量

other(Tensor or float) ---- 对比的张量或float值

out(Tensor, 可选的) ---- 输出张量

** 返回值:** 一个torch.ByteTensor 张量,包含了每个位置的比较结果,如果tensor和other不相等为True,返回1.

>>> import torch
>>> a = torch.Tensor([[1, 2], [3, 4]])
>>> b = torch.Tensor([[1, 1], [4, 4]])
>>> torch.ne(a, b)
tensor([[0, 1],
  [1, 0]], dtype=torch.uint8)

14. torch.sort(input, dim=None, descending=False, out=None)

说明: 对输入张量input沿指定维度按升序排序,如果不给定dim,则默认为输入的最后一维。如果指定参数descending为True,则按降序排序。

参数:

input(Tensor) ---- 要排序的张量

dim(int,可选的) ---- 沿着此维度排序

descending(bool,可选的) ---- 布尔值,控制升序排序

out(tuple,可选的) ---- 输出张量

返回值: 为ByteTensor类型或与tensor相同类型,为元组(sorted_tensor,sorted_indices),sorted_indices为原始输入中的下标

>>> x = torch.randn(3, 4)
>>> x
tensor([[-0.3613, -0.2583, -0.4276, -1.3106],
  [-1.1577, -0.7505, 1.7217, -0.6247],
  [-0.1338, 0.4423, 0.0280, -1.4796]])
>>> sorted, indices = torch.sort(x)
>>> sorted
tensor([[-1.3106, -0.4276, -0.3613, -0.2583],
  [-1.1577, -0.7505, -0.6247, 1.7217],
  [-1.4796, -0.1338, 0.0280, 0.4423]])
>>> indices
tensor([[3, 2, 0, 1],
  [0, 1, 3, 2],
  [3, 0, 2, 1]])

15. torch.topk(input, dim=None, largest=True, sorted=True, out=None)

说明: 沿指定dim维度返回输入张量input中k个最大值。如果不指定dim,则默认input的最后一维,如果largest为False,则返回最小的k个值。

参数:

input(Tensor) ---- 输入张量

k(int) ---- “top-k"中的k值

dim(int,可选的) ---- 排序的维度

largest(bool,可选的) ---- 布尔值,控制返回最大或最小值

sorted(bool,可选的) ---- 布尔值,控制返回值是否排序

out(tuple,可选的) ---- 可选输出张量

返回值: 返回一个元组(values, indices),其中indices是原始输入张量input中排序元素下标。如果设定布尔值sorted为True,将会确保返回的k个值被排序

>>> x = torch.arange(1, 6)
>>> x
tensor([1, 2, 3, 4, 5])
>>> torch.topk(x, 3)
torch.return_types.topk(
values=tensor([5, 4, 3]),
indices=tensor([4, 3, 2]))
>>> torch.topk(x, 3, 0, largest=False)
torch.return_types.topk(
values=tensor([1, 2, 3]),
indices=tensor([0, 1, 2]))

以上这篇Pytorch学习之torch用法----比较操作(Comparison Ops)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python局域网ip扫描示例分享
Apr 03 Python
python中根据字符串调用函数的实现方法
Jun 12 Python
Python实现小数转化为百分数的格式化输出方法示例
Sep 20 Python
python获取本机所有IP地址的方法
Dec 26 Python
Django之无名分组和有名分组的实现
Apr 16 Python
linux下python中文乱码解决方案详解
Aug 28 Python
python输入错误后删除的方法
Oct 12 Python
python 遗传算法求函数极值的实现代码
Feb 11 Python
python itsdangerous模块的具体使用方法
Feb 17 Python
python中adb有什么功能
Jun 07 Python
你需要学会的8个Python列表技巧
Jun 24 Python
python实现启动一个外部程序,并且不阻塞当前进程
Dec 05 Python
PyTorch的torch.cat用法
Jun 28 #Python
使用pytorch 筛选出一定范围的值
Jun 28 #Python
解析python 中/ 和 % 和 //(地板除)
Jun 28 #Python
pytorch 常用函数 max ,eq说明
Jun 28 #Python
浅谈pytorch中torch.max和F.softmax函数的维度解释
Jun 28 #Python
Python turtle库的画笔控制说明
Jun 28 #Python
使用python修改文件并立即写回到原始位置操作(inplace读写)
Jun 28 #Python
You might like
discuz安全提问算法
2007/06/06 PHP
thinkphp中AJAX返回ajaxReturn()方法分析
2016/12/06 PHP
PHP实现微信公众号验证Token的示例代码
2019/12/16 PHP
php反序列化长度变化尾部字符串逃逸(0CTF-2016-piapiapia)
2020/02/15 PHP
禁止刷新,回退的JS
2006/11/25 Javascript
javascript基础知识大集锦(一) 推荐收藏
2011/01/13 Javascript
js中各浏览器中鼠标按键值的差异
2011/04/07 Javascript
文本框输入时 实现自动提示(像百度、google一样)
2012/04/05 Javascript
NodeJs中的非阻塞方法介绍
2012/06/05 NodeJs
jquery ajax对特殊字符进行转义防止js注入使用示例
2013/11/21 Javascript
jQuery如何实现点击页面获得当前点击元素的id或其他信息
2014/01/09 Javascript
jQuery实现仿路边灯箱广告图片轮播效果
2015/04/15 Javascript
AngularJS 让人爱不释手的八种功能
2016/03/23 Javascript
bootstrap模态框跳转到当前模板页面 框消失了而背景存在问题的解决方法
2020/11/30 Javascript
jQuery表单插件ajaxForm实例详解
2017/01/17 Javascript
react实现菜单权限控制的方法
2017/12/11 Javascript
vue自定义指令directive实例详解
2018/01/17 Javascript
vue源码解析之事件机制原理
2018/04/21 Javascript
React+EggJs实现断点续传的示例代码
2020/07/07 Javascript
wxPython学习之主框架实例
2014/09/28 Python
python执行子进程实现进程间通信的方法
2015/06/02 Python
Unicode和Python的中文处理
2017/03/19 Python
Python实现比较扑克牌大小程序代码示例
2017/12/06 Python
numpy数组做图片拼接的实现(concatenate、vstack、hstack)
2019/11/08 Python
python opencv 图像边框(填充)添加及图像混合的实现方法(末尾实现类似幻灯片渐变的效果)
2020/03/09 Python
BAILEY 44官网:美国制造的女性服装
2019/07/01 全球购物
JSF如何进行表格处理及取值
2012/08/06 面试题
药品质量检测应届生求职信
2013/11/14 职场文书
感恩老师的演讲稿
2014/05/06 职场文书
创先争优个人承诺书
2014/08/30 职场文书
公安交警个人对照检查材料思想汇报
2014/10/01 职场文书
2014年纠风工作总结
2014/12/08 职场文书
一年级数学上册复习计划
2015/01/17 职场文书
2015年学校政教工作总结
2015/07/20 职场文书
工商局调档介绍信
2015/10/22 职场文书
Pandas数据类型之category的用法
2021/06/28 Python