Pytorch Tensor的统计属性实例讲解


Posted in Python onDecember 30, 2019

1. 范数

示例代码:

import torch
 
a = torch.full([8], 1)
b = a.reshape([2, 4])
c = a.reshape([2, 2, 2])
 
# 求L1范数(所有元素绝对值求和)
print(a.norm(1), b.norm(1), c.norm(1))
# 求L2范数(所有元素的平方和再开根号)
print(a.norm(2), b.norm(2), c.norm(2))
 
# 在b的1号维度上求L1范数
print(b.norm(1, dim=1))
# 在b的1号维度上求L2范数
print(b.norm(2, dim=1))
 
# 在c的0号维度上求L1范数
print(c.norm(1, dim=0))
# 在c的0号维度上求L2范数
print(c.norm(2, dim=0))

输出结果:

tensor(8.) tensor(8.) tensor(8.)
tensor(2.8284) tensor(2.8284) tensor(2.8284)
tensor([4., 4.])
tensor([2., 2.])
tensor([[2., 2.],
  [2., 2.]])
tensor([[1.4142, 1.4142],
  [1.4142, 1.4142]])

2. 一些常用操作

(1)均值、累加、最小、最大、累积

示例代码:

b = torch.arange(8).reshape(2, 4).float()
print(b)
# 均值,累加,最小,最大,累积
print(b.mean(), b.sum(), b.min(), b.max(), b.prod())
# 打平后的最小最大值索引
print(b.argmax(), b.argmin())

输出结果:

tensor([[0., 1., 2., 3.],
  [4., 5., 6., 7.]])
tensor(3.5000) tensor(28.) tensor(0.) tensor(7.) tensor(0.)
tensor(7) tensor(0)

注意:上面的argmax、argmin操作默认会将Tensor打平后取最大值索引和最小值索引,如果不希望Tenosr打平,而是求给定维度上的索引,需要指定在哪一个维度上求最大值索引或最小值索引。

比如,有shape=[4, 10]的Tensor,表示4张图片在10分类的概率结果,我们需要知道每张图片的最可能的分类结果:

a = torch.rand(4, 10)
print(a)
# 在第二维度上求最大值索引
print(a.argmax(dim=1))

输出结果:

tensor([[0.0711, 0.5641, 0.7945, 0.6964, 0.3609, 0.5817, 0.1705, 0.6913, 0.1263,
   0.8346],
  [0.0810, 0.0771, 0.1983, 0.0344, 0.1067, 0.9591, 0.8515, 0.3046, 0.0491,
   0.1291],
  [0.3527, 0.2676, 0.9859, 0.2656, 0.1985, 0.3759, 0.8221, 0.3571, 0.5340,
   0.7759],
  [0.0969, 0.3954, 0.5478, 0.3543, 0.8253, 0.9291, 0.4960, 0.4390, 0.3780,
   0.5858]])
tensor([9, 5, 2, 5])

(2)直接使用max和min配合dim参数也可以获得最值索引,同时得到最值的具体值:

print(c.max(dim=1))

输出结果:

(tensor([0.9589, 1.7394, 1.3448, 2.2079]), tensor([2, 2, 5, 7]))

(3)使用keepdim=True可以保持应有的dim,即仅仅是将求最值的那个dim的size变成了1,返回的结果是符合原Tensor语义的。

print(c.argmax(dim=1, keepdim=True))
print(c.max(dim=1, keepdim=True))

输出结果:

tensor([[2],
  [2],
  [5],
  [7]])
(tensor([[0.9589],
  [1.7394],
  [1.3448],
  [2.2079]]), tensor([[2],
  [2],
  [5],
  [7]]))

(4)取前k大/前k小/第k小的概率值及其索引

使用topk代替max可以完成更灵活的需求,有时候不是仅仅要概率最大的那一个,而是概率最大的k个。如果不是求最大的k个,而是求最小的k个,只要使用参数largest=False,kthvalue还可以取第k小的概率值及其索引。

示例代码:

# 2个样本,分为10个类别的置信度
d = torch.randn(2, 10) 
# 最大概率的3个类别
print(d.topk(3, dim=1)) 
# 最小概率的3个类别
print(d.topk(3, dim=1, largest=False)) 
# 求第8小概率的类别(一共10个那就是第3大)
print(d.kthvalue(8, dim=1))

输出结果:

(tensor([[2.0692, 1.6490, 0.9526],
  [1.5983, 1.5737, 1.5532]]), tensor([[6, 3, 5],
  [8, 1, 2]]))
(tensor([[-1.0023, -0.6423, 0.0655],
  [-1.2959, -1.1504, -0.9859]]), tensor([[4, 0, 2],
  [0, 5, 3]]))
(tensor([0.9526, 1.5532]), tensor([5, 2]))

(5)比较操作

示例代码:

import torch
 
a = torch.randn(2, 3)
b = torch.randn(2, 3)
print(a)
print(b)
# 比较是否大于0,是对应位置返回1,否对应位置返回0,注意得到的是ByteTensor
print(a > 0) 
print(torch.gt(a, 0))
# 是否不等于0,是对应位置返回1,否对应位置返回0
print(a != 0)
# 比较每个位置是否相等,是对应位置返回1,否对应位置返回0
print(torch.eq(a, b)) 
# 比较每个位置是否相等,全部相等时才返回True
print(torch.equal(a, b), torch.equal(a, a))

输出结果:

tensor([[-0.1425, -1.1142, 0.2224],
  [ 0.6142, 1.7455, -1.1776]])
tensor([[-0.0774, -1.1012, -0.4862],
  [-0.3110, -0.2110, 0.0381]])
tensor([[0, 0, 1],
  [1, 1, 0]], dtype=torch.uint8)
tensor([[0, 0, 1],
  [1, 1, 0]], dtype=torch.uint8)
tensor([[1, 1, 1],
  [1, 1, 1]], dtype=torch.uint8)
tensor([[0, 0, 0],
  [0, 0, 0]], dtype=torch.uint8)
False True

以上这篇Pytorch Tensor的统计属性实例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
pymongo给mongodb创建索引的简单实现方法
May 06 Python
Python单例模式的两种实现方法
Aug 14 Python
Python 快速实现CLI 应用程序的脚手架
Dec 05 Python
Python和Java进行DES加密和解密的实例
Jan 09 Python
python 实现在Excel末尾增加新行
May 02 Python
numpy向空的二维数组中添加元素的方法
Nov 01 Python
python3爬虫怎样构建请求header
Dec 23 Python
Pandas之Dropna滤除缺失数据的实现方法
Jun 25 Python
Tornado实现多进程/多线程的HTTP服务详解
Jul 25 Python
Python图像处理库PIL的ImageFilter模块使用介绍
Feb 26 Python
PyCharm MySQL可视化Database配置过程图解
Jun 09 Python
python爬虫请求头设置代码
Jul 28 Python
PyTorch中permute的用法详解
Dec 30 #Python
python实现多进程按序号批量修改文件名的方法示例
Dec 30 #Python
Pytorch Tensor基本数学运算详解
Dec 30 #Python
python垃圾回收机制(GC)原理解析
Dec 30 #Python
利用Python代码实现一键抠背景功能
Dec 29 #Python
如何利用pygame实现简单的五子棋游戏
Dec 29 #Python
Python使用正则实现计算字符串算式
Dec 29 #Python
You might like
php 取得瑞年与平年的天数的代码
2009/08/10 PHP
php smarty模版引擎中的缓存应用
2009/12/11 PHP
关于php 接口问题(php接口主要也就是运用curl,curl函数)
2013/07/01 PHP
PHP用strstr()函数阻止垃圾评论(通过判断a标记)
2013/09/28 PHP
php calender(日历)二个版本代码示例(解决2038问题)
2013/12/24 PHP
php图片合成方法(多张图片合成一张)
2017/11/25 PHP
nicejforms——美化表单不用愁
2007/02/20 Javascript
极易被忽视的javascript面试题七问七答
2016/02/15 Javascript
基于MVC5和Bootstrap的jQuery TreeView树形控件(二)之数据支持json字符串、list集合
2016/08/11 Javascript
vue的props实现子组件随父组件一起变化
2016/10/27 Javascript
JavaScript自定义函数实现查找两个字符串最长公共子串的方法
2016/11/24 Javascript
使用JavaScript触发过渡效果的方法
2017/01/19 Javascript
详解Vue生命周期的示例
2017/03/10 Javascript
JS实现含有中文字符串的友好截取功能分析
2017/03/13 Javascript
javascript异步编程的六种方式总结
2019/05/17 Javascript
关于vue.js中实现方法内某些代码延时执行
2019/11/14 Javascript
详解Vue的ref特性的使用
2020/01/24 Javascript
Vue3 的响应式和以前有什么区别,Proxy 无敌?
2020/05/20 Javascript
基于javascript处理nginx请求过程详解
2020/07/07 Javascript
Python多线程同步Lock、RLock、Semaphore、Event实例
2014/11/21 Python
Python将xml和xsl转换为html的方法
2015/03/10 Python
Python中的条件判断语句与循环语句用法小结
2016/03/21 Python
python线程池threadpool使用篇
2018/04/27 Python
css3 transform及原生js实现鼠标拖动3D立方体旋转
2016/06/20 HTML / CSS
详解CSS3中字体平滑处理和抗锯齿渲染
2017/03/29 HTML / CSS
浅谈HTML5 & CSS3的新交互特性
2016/07/19 HTML / CSS
荷兰美妆护肤品海淘网站:Beautinow(中文)
2020/11/22 全球购物
毕业生的自我评价
2013/12/30 职场文书
应届电子商务毕业自荐书范文
2014/02/11 职场文书
四风批评与自我批评范文
2014/10/14 职场文书
2014年监理工作总结范文
2014/11/17 职场文书
公务员政审个人总结
2015/02/12 职场文书
2015年财务部工作总结
2015/04/10 职场文书
Pytorch中的数据集划分&正则化方法
2021/05/27 Python
Go语言空白表示符_的实例用法
2021/07/04 Golang
springcloud整合seata
2022/05/20 Java/Android