PyTorch中Tensor的数据统计示例
作者:Steven·简谈 时间:2021-03-05 01:04:53
张量范数:torch.norm(input, p=2) → float
返回输入张量 input 的 p 范数
举个例子:
>>> import torch
>>> a = torch.full([8], 1)
>>> b = a.view(2, 4)
>>> c = a.view(2, 2, 2)
>>> a.norm(1), b.norm(1), c.norm(1)# 求 1- 范数
(tensor(8.), tensor(8.), tensor(8.))
>>> a.norm(2), b.norm(2), c.norm(2)# 求 2- 范数
(tensor(2.8284), tensor(2.8284), tensor(2.8284))
>>> a.norm(3), b.norm(3), c.norm(3)# 求 ∞- 范数
(tensor(2.), tensor(2.), tensor(2.))
>>> b
tensor([[1., 1., 1., 1.],
[1., 1., 1., 1.]])
>>> b.norm(1, 1) # 在 1 维度上求 1- 范数
tensor([4., 4.])
>>> b.norm(2, 1) # 在 1 维度上求 2- 范数
b.norm(1, 2)
>>> c
tensor([[[1., 1.],
[1., 1.]],
[[1., 1.],
[1., 1.]]])
>>> c.norm(1, 0) # 在 0 维度上求 1- 范数
tensor([[2., 2.],
[2., 2.]])
>>> c.norm(2, 0) # 在 0 维度上求 2- 范数
tensor([[1.4142, 1.4142],
[1.4142, 1.4142]])
只有一个参数时,表示对整个张量求范数,参数表示范数的幂指数值。
有两个参数时,表示在张量某一维度对尺寸中每一部分求范数,第一个参数是范数的幂指数值,第二个参数是选择的维度。
张量统计
最基础的统计方法,比如张量中的最小值、最大值、均值、累加、累积。
举个例子:
>>> a = torch.arange(8).view(2, 4).float()
>>> a
tensor([[0., 1., 2., 3.],
[4., 5., 6., 7.]])
>>> a.min(), a.max(), a.mean(), a.sum(), a.prod() # 分别求最小值、最大值、均值、累加、累积
(tensor(0.), tensor(7.), tensor(3.5000), tensor(28.), tensor(0.))
>>> a.argmin(), a.argmax() # 分别是把张量打平后最小值、最大值的索引
(tensor(0), tensor(7))
>>> a.argmin(1), a.argmax(1) # 不打平求 1 维度中每一部分最小值、最大值的索引
(tensor([0, 0]), tensor([3, 3]))
dim和keepdim
>>> a = torch.randn(5, 10)
>>> a
tensor([[-0.6346, -0.9074, 0.1525, 0.1901, -0.5391, -0.2437, 1.0150, -0.0427,
-1.5336, 0.8542],
[-0.1879, 1.9947, -0.3524, -1.2559, -0.8129, -0.3018, 0.5654, 0.8428,
-0.3517, -0.7787],
[ 0.0686, 0.6166, 0.2632, -0.0947, -0.5592, -1.4041, 1.5565, 1.5616,
-1.3076, -0.1137],
[ 0.5205, -1.5716, -1.1277, 0.8096, -0.2123, -0.0974, 0.7698, 1.1373,
0.5165, 0.5256],
[-0.4162, 0.3170, 0.2368, 1.1695, -0.1960, -0.3285, 0.2420, 1.6468,
0.2646, 0.4573]])
>>> a.max(dim=1)
(tensor([1.0150, 1.9947, 1.5616, 1.1373, 1.6468]), tensor([6, 1, 7, 7, 7]))
>>> a.argmax(dim=1)
tensor([6, 1, 7, 7, 7])
max 添加 dim 后不仅显示了 1 维度中每一部分的最大值,还显示了其索引
>>> a.max(dim=1, keepdim=True)
(tensor([[1.0150],
[1.9947],
[1.5616],
[1.1373],
[1.6468]]), tensor([[6],
[1],
[7],
[7],
[7]]))
>>> a.argmax(dim=1, keepdim=True)
tensor([[6],
[1],
[7],
[7],
[7]])
保持维度一致。添加 keepdim 后,得出的结果维度不改变,原来是二维的数据,得出的结果还是二维。不添加得出的结果就是一维的。
比较操作
torch.topk(input, k, dim=None, largest=True, sorted=True, out=None) -> (Tensor, LongTensor)
沿给定 dim 维度返回输入张量 input 中 k 个最大值。 如果不指定 dim,则默认为 input 的最后一维。 如果为 largest 为 False ,则返回最小的 k 个值。
返回一个元组 (values,indices),其中 indices 是原始输入张量 input 中测元素下标。 如果设定布尔值 sorted 为_True_,将会确保返回的 k 个值被排序。
torch.kthvalue(input, k, dim=None, out=None) -> (Tensor, LongTensor) 取输入张量 input 指定维上第 k 个最小值。如果不指定 dim,则默认为 input 的最后一维。
返回一个元组 (values,indices),其中indices是原始输入张量input中沿dim维的第 k 个最小值下标。
举个例子:
>>> b = torch.randn(5, 10)
>>> b
tensor([[ 0.1863, 0.0160, -1.0657, -1.8984, 2.3274, 0.6534, 1.8126, 1.8666,
0.4830, -0.7800],
[-0.9359, -1.0655, 0.8321, 1.6265, 0.6812, -0.2870, 0.6987, 0.6067,
-0.1318, 0.7819],
[-3.1129, 0.9571, -0.1319, -1.0016, 0.7267, 0.1060, -0.2926, 0.3492,
1.0026, 0.2924],
[-0.7101, -0.8327, 0.5463, 0.3805, -0.8720, -1.6723, 0.0365, 1.5540,
0.1940, 1.4294],
[ 0.4174, -0.9414, -0.0351, -1.6142, -0.7802, -2.3916, -2.4822, 0.7233,
-0.7037, 0.2725]])
>>> b.topk(3, dim=1)
(tensor([[2.3274, 1.8666, 1.8126],
[1.6265, 0.8321, 0.7819],
[1.0026, 0.9571, 0.7267],
[1.5540, 1.4294, 0.5463],
[0.7233, 0.4174, 0.2725]]), tensor([[4, 7, 6],
[3, 2, 9],
[8, 1, 4],
[7, 9, 2],
[7, 0, 9]]))
>>> b.topk(3, dim=1, largest=False)
(tensor([[-1.8984, -1.0657, -0.7800],
[-1.0655, -0.9359, -0.2870],
[-3.1129, -1.0016, -0.2926],
[-1.6723, -0.8720, -0.8327],
[-2.4822, -2.3916, -1.6142]]), tensor([[3, 2, 9],
[1, 0, 5],
[0, 3, 6],
[5, 4, 1],
[6, 5, 3]]))
>>> a.kthvalue(8, dim=1)
(tensor([0.1034, 0.8940, 0.6155, 0.4210, 0.1955]), tensor([1, 2, 6, 4, 7]))
topk 添加 largest=False 就是返回最小,不添加就是返回最大。
kthvalue 返回以从大到小排列的指定位置的数。上面代码中即为返回第 8 小的数。
torch.eq(input, other, out=None) → Tensor
比较元素相等性。第二个参数可为一个数或与第一个参数同类型形状的张量。
torch.equal(tensor1, tensor2) → bool
如果两个张量有相同的形状和元素值,则返回 True ,否则 False。
举个例子:
>>> a = torch.ones(2, 3)
>>> b = torch.randn(2, 3)
>>> torch.eq(a, b)
tensor([[0, 0, 0],
[0, 0, 0]], dtype=torch.uint8)
>>> torch.eq(a, a)
tensor([[1, 1, 1],
[1, 1, 1]], dtype=torch.uint8)
>>> torch.equal(a, a)
True
eq 比较张量中的每个数据,equal 比较整个张量
来源:https://blog.csdn.net/weixin_44613063/article/details/89741228
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Access数据库下如何使用通用对话框的问题
在ASP与ASP.NET之间共享对话状态(2)
利用webstrom调试Vue.js单页面程序的方法教程
![](https://img.aspxhome.com/file/2023/5/55905_0s.jpg)
浅谈Python中的作用域规则和闭包
分享一些可视信息设计资源
![](https://img.aspxhome.com/file/UploadPic/200910/6/ui-info-share-banner-41s.jpg)
python如何给内存和cpu使用量设置限制
基于python获取本地时间并转换时间戳和日期格式
![](https://img.aspxhome.com/file/2023/9/86539_0s.png)
php环境下利用session防止页面重复刷新的具体实现
基于Python实现经典植物大战僵尸游戏
![](https://img.aspxhome.com/file/2023/8/84038_0s.jpg)
用python求一个数组的和与平均值的实现方法
Win10下python 2.7.13 安装配置方法图文教程
![](https://img.aspxhome.com/file/2023/4/80624_0s.png)
python anaconda 安装 环境变量 升级 以及特殊库安装的方法
Python二叉树的遍历操作示例【前序遍历,中序遍历,后序遍历,层序遍历】
浅析Python pandas模块输出每行中间省略号问题
设计师挖掘用户需求浅谈
我对PyTorch dataloader里的shuffle=True的理解
![](https://img.aspxhome.com/file/2023/5/97175_0s.png)
新手教程:如何设置五大类MySQL参数
python中必会的四大高级数据类型(字符,元组,列表,字典)
![](https://img.aspxhome.com/file/2023/3/75373_0s.jpg)
Python脚本调试工具安装过程
![](https://img.aspxhome.com/file/2023/6/103296_0s.jpg)