Pytorch Tensor的统计属性实例讲解

yipeiwu_com7年前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 含子图的gif生成时内存溢出的方法

今天想用python做个demo,含两个子图的动态gif,代码如下: import matplotlib.pyplot as plt import imageio,os import...

Python AES加密实例解析

本文主要是对aes加密技术做一个简要分析,然后使用Python实现,具体介绍如下。 AES,是美国联邦政府采用的一种加密技术,AES有几个模式,其中CBC模式是公认的安全性最好的模式,被...

Python迭代器和生成器定义与用法示例

本文实例讲述了Python迭代器和生成器定义与用法。分享给大家供大家参考,具体如下: 迭代器 iter() 迭代器是访问集合中元素的一种方式,迭代器 object 从集合中的第一个元素开...

Python3数据库操作包pymysql的操作方法

Python3数据库操作包pymysql的操作方法

以下代码实现环境是mac系统,本地配置mysql服务端和navicat premium客户端,python环境是配置了pymysql的anaconda3。 首先,与数据库建立connec...

python 字符串追加实例

通过一个for循环,将一个一个字符追加到字符串中: 方法一: string = '' str=u"追加字符" for i in range(len(str)): string+=...