Pytorch Tensor的统计属性实例讲解

yipeiwu_com6年前Python基础

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设计】。

相关文章

基于python中staticmethod和classmethod的区别(详解)

例子 class A(object): def foo(self,x): print "executing foo(%s,%s)"%(self,x) @classm...

PySide和PyQt加载ui文件的两种方法

本文实例为大家分享了PySide和PyQt加载ui文件的具体实现代码,供大家参考,具体内容如下 在用PySide或PyQt的时候,经常用到要将画好的ui文件导入到代码里使用,下面是两种调...

Python3.4学习笔记之 idle 清屏扩展插件用法分析

本文实例讲述了Python3.4 idle 清屏扩展插件用法。分享给大家供大家参考,具体如下: python idle 清屏问题的解决,使用python idle都会遇到一个常见而又懊恼...

python嵌套字典比较值与取值的实现示例

python嵌套字典比较值与取值的实现示例

前言 本文通过示例给大家介绍了python嵌套字典比较值,取值,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。 示例代码 #取值import types allGu...

django+echart绘制曲线图的方法示例

django+echart绘制曲线图的方法示例

声明:请事先到官网下载echarts,另外本文引用了adminlte模板构建前台页面 views: <!-- /.row --> <div class="r...