Pytorch-----(3A)基本的统计

一、问题

进行基本的张量统计如均值、中位数、众数等;进行基本的统计有助于应用概率分布和统计推断。Torch功能与Numpy类似,但是Torch函数支持GPU加速。以下是创建基本统计量的函数;

二、如何实现

1D张量统计均值很简单,但是计算2D张量的统计量需要额外的参数,因为在这些计算中需要指定维度。

python 复制代码
#计算均值
torch.mean(torch.tensor([10., 10., 13., 10., 34.,
45., 65., 67., 87., 89., 87., 34.]))
tensor(45.9167)

# 计算行和列的统计量

d = torch.randn(4, 5)
d
tensor([[-1.6406, 0.9295, 1.2907, 0.2612, 0.9711], [ 0.3551, 0.8562, -0.3635, -0.1552, -1.2282], [ 1.2445, 1.1750, -0.2217, -2.0901, -1.2658], [-1.8761, -0.6066, 0.7470, 0.4811, 0.6234]])

torch.mean(d,dim=0) # 0维表示平行纵轴方向,即按列统计
tensor([-0.4793, 0.5885, 0.3631, -0.3757, -0.2249])
torch.mean(d,dim=1)
tensor([ 0.3624, -0.1071, -0.2316, -0.1262]) # 1维表示平行纵轴方向,即按行统计

中位数、众数和标准差类似:

python 复制代码
#计算列中位数
torch.median(d,dim=0)
#结果:
torch.return_types.median( values=tensor([-1.6406, 0.8562, -0.2217, -0.1552, -1.2282]), indices=tensor([0, 1, 2, 1, 1]))

#计算行中位数
torch.median(d,dim=1)
torch.return_types.median( values=tensor([ 0.9295, -0.1552, -0.2217, 0.4811]), indices=tensor([1, 3, 2, 3]))

# 计算众数
torch.mode(d)
torch.return_types.mode( values=tensor([-1.6406, -1.2282, -2.0901, -1.8761]), indices=tensor([0, 4, 3, 0]))

torch.mode(d,dim=0)
torch.return_types.mode( values=tensor([-1.8761, -0.6066, -0.3635, -2.0901, -1.2658]), indices=tensor([3, 3, 1, 2, 2]))

torch.mode(d,dim=1)
torch.return_types.mode( values=tensor([-1.6406, -1.2282, -2.0901, -1.8761]), indices=tensor([0, 4, 3, 0]))
#由上可见计算mode 不指定dim时,默认是dim=1

标准差反映了偏离中心度量的程度,指示着数据或变量的一致性,它表明数据中是否有较大的波动或异常。

python 复制代码
#compute the standard deviation

torch.std(d)
tensor(1.0944)

torch.std(d,dim=0)
tensor([1.5240, 0.8083, 0.7911, 1.1730, 1.1889])

torch.std(d,dim=1)
tensor([1.1807, 0.7852, 1.4732, 1.1165])

#计算方差
torch.var(d)
tensor(1.1978)
torch.var(d,dim=0)
tensor([2.3224, 0.6534, 0.6259, 1.3758, 1.4134])
torch.var(d,dim=1)
tensor([1.3940, 0.6166, 2.1703, 1.2466])

# 计算最小、最大值
torch.min(d)
tensor(-2.0901)
torch.min(d,dim=0)
torch.return_types.min( values=tensor([-1.8761, -0.6066, -0.3635, -2.0901, -1.2658]), indices=tensor([3, 3, 1, 2, 2]))
torch.min(d,dim=1)
torch.return_types.min( values=tensor([-1.6406, -1.2282, -2.0901, -1.8761]), indices=tensor([0, 4, 3, 0]))

# 张量排序 默认是1维的;
torch.sort(d)
torch.return_types.sort( values=tensor([[-1.6406, 0.2612, 0.9295, 0.9711, 1.2907], [-1.2282, -0.3635, -0.1552, 0.3551, 0.8562], [-2.0901, -1.2658, -0.2217, 1.1750, 1.2445], [-1.8761, -0.6066, 0.4811, 0.6234, 0.7470]]), indices=tensor([[0, 3, 1, 4, 2], [4, 2, 3, 0, 1], [3, 4, 2, 1, 0], [0, 1, 3, 4, 2]]))

torch.sort(d,dim=0)
torch.return_types.sort( values=tensor([[-1.8761, -0.6066, -0.3635, -2.0901, -1.2658], [-1.6406, 0.8562, -0.2217, -0.1552, -1.2282], [ 0.3551, 0.9295, 0.7470, 0.2612, 0.6234], [ 1.2445, 1.1750, 1.2907, 0.4811,0.9711]]), indices=tensor([[3, 3, 1, 2, 2], [0, 1, 2, 1, 1], [1, 0, 3, 0, 3], [2, 2, 0, 3, 0]]))

torch.sort(d,dim=0,descending=True) #降序排列
torch.return_types.sort( values=tensor([[ 1.2445, 1.1750, 1.2907, 0.4811, 0.9711], [ 0.3551, 0.9295, 0.7470, 0.2612, 0.6234], [-1.6406, 0.8562, -0.2217, -0.1552, -1.2282], [-1.8761, -0.6066, -0.3635, -2.0901, -1.2658]]), indices=tensor([[2, 2, 0, 3, 0], [1, 0, 3, 0, 3], [0, 1, 2, 1, 1], [3, 3, 1, 2, 2]]))

torch.sort(d,dim=1,descending=True)
torch.return_types.sort( values=tensor([[ 1.2907, 0.9711, 0.9295, 0.2612, -1.6406], [ 0.8562, 0.3551, -0.1552, -0.3635, -1.2282], [ 1.2445, 1.1750, -0.2217, -1.2658, -2.0901], [ 0.7470, 0.6234, 0.4811, -0.6066, -1.8761]]), indices=tensor([[2, 4, 1, 3, 0], [1, 0, 3, 2, 4], [0, 1, 2, 4, 3], [2, 4, 3, 1, 0]]))
python 复制代码
from torch.autograd import Variable
Variable(torch.ones(2,2),requires_grad=True)
tensor([[1., 1.], [1., 1.]], requires_grad=True)

a, b = 12,23
x1 = Variable(torch.randn(a,b),requires_grad=True)
x2 = Variable(torch.randn(a,b),requires_grad=True)
x3 =Variable(torch.randn(a,b),requires_grad=True)

c = x1 * x2  
d = a + x3
e = torch.sum(d)

e.backward()

print(e)

tensor(3278.1235, grad_fn=<SumBackward0>)
相关推荐
魔障阿Q12 分钟前
windows使用bat脚本激活conda环境
人工智能·windows·python·深度学习·conda
Wnq1007216 分钟前
巡检机器人数据处理技术的创新与实践
网络·数据库·人工智能·机器人·巡检机器人
洋芋爱吃芋头19 分钟前
hadoop中的序列化和反序列化(3)
大数据·hadoop·python
Eric.Lee202135 分钟前
数据集-目标检测系列- 冥想 检测数据集 close_eye>> DataBall
人工智能·目标检测·计算机视觉·yolo检测·眼睛开闭状态检测识别
零炻大礼包41 分钟前
【MCP】服务端搭建(python和uv环境搭建、nodejs安装、pycharma安装)
开发语言·python·uv·mcp
来自星星的坤1 小时前
Python 爬虫基础入门教程(超详细)
开发语言·爬虫·python
胡乱儿起个名1 小时前
Relay算子注册(在pytorch.py端调用)
c++·人工智能·tvm·编译器·ai编译器
Dxy12393102162 小时前
Python+OpenCV实现手势识别与动作捕捉:技术解析与应用探索
开发语言·python·opencv
嘉图明2 小时前
《从数据殖民到算法正义:破解AI垄断的伦理与技术路径》
人工智能·算法
shadowtalon2 小时前
基于CNN的猫狗图像分类系统
人工智能·深度学习·神经网络·机器学习·计算机视觉·分类·cnn