Pytorch Tensor的统计属性实例讲解
作者:洪流之源 时间:2023-11-21 22:27:52
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
来源:https://blog.csdn.net/weicao1990/article/details/93746789
标签:Pytorch,Tensor,统计属性
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python 实现IP子网计算
2022-04-24 20:27:37
多种网页弹出窗口代码
2007-11-24 21:13:00
Python中的is和id用法分析
2021-12-09 17:23:12
ADO.NET实现对SQL Server数据库的增删改查示例
2024-01-12 17:37:54
Python3中的tuple函数知识点讲解
2021-12-31 17:22:56
Python自动化办公之Word转PDF的实现
2022-10-04 05:30:50
![](https://img.aspxhome.com/file/2023/9/133799_0s.jpg)
MySQL 实现lastInfdexOf的功能案例
2024-01-20 15:16:51
vue 巧用过渡效果(小结)
2024-04-10 10:33:28
![](https://img.aspxhome.com/file/2023/2/139762_0s.png)
python整合ffmpeg实现视频文件的批量转换
2023-06-07 03:03:12
Python退火算法在高次方程的应用
2023-09-07 22:39:30
![](https://img.aspxhome.com/file/2023/6/128466_0s.png)
在Python中使用SQLite的简单教程
2022-05-12 04:01:46
使用Spring AOP实现MySQL数据库读写分离案例分析(附demo)
2024-01-16 04:47:03
![](https://img.aspxhome.com/file/2023/6/129246_0s.png)
简单了解Python3 bytes和str类型的区别和联系
2023-11-19 21:52:29
python插入排序算法的实现代码
2021-09-11 15:36:15
Python中类的定义、继承及使用对象实例详解
2023-07-11 17:10:37
简单介绍Python中的decode()方法的使用
2021-01-05 23:47:31
Python 含参构造函数实例详解
2023-02-06 13:16:48
使用python批量转换文件编码为UTF-8的实现
2023-03-07 03:19:41
pyCharm 设置调试输出窗口中文显示方式(字符码转换)
2022-04-22 00:23:52
![](https://img.aspxhome.com/file/2023/3/79803_0s.jpg)
《写给大家看的设计书》阅读笔记之亲密性原则
2009-07-08 20:02:00
![](https://img.aspxhome.com/file/UploadPic/20097/8/qmx_1-99s.jpg)