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>)
相关推荐
Irene19912 小时前
Python 卸载与安装(以卸载3.13.3,装3.13.13为例)
python
予早2 小时前
使用 pyrasite-ng 和 guppy3 做内存分析
python·内存分析
hef2887 小时前
如何生成特定SQL的AWR报告_@awrsqrpt.sql深度剖析单条语句性能
jvm·数据库·python
小程故事多_807 小时前
Agent+Milvus,告别静态知识库,打造具备动态记忆的智能AI助手
人工智能·深度学习·ai编程·milvus
code_pgf7 小时前
Llama 3详解
人工智能·llama
ComputerInBook7 小时前
数字图像处理(4版)——第 3 章——(图像的)强度变换和空间滤波(Rafael C.Gonzalez&Richard E. Woods)
图像处理·人工智能·计算机视觉·强度变换和空间滤波
爱写代码的小朋友7 小时前
生成式人工智能(AIGC)在开放式教育问答系统中的知识表征与推理机制研究
人工智能·aigc
Jinkxs7 小时前
从语法纠错到项目重构:Python+Copilot 的全流程开发效率提升指南
python·重构·copilot
技术专家8 小时前
Stable Diffusion系列的详细讨论 / Detailed Discussion of the Stable Diffusion Series
人工智能·python·算法·推荐算法·1024程序员节
m0_488913018 小时前
万字长文带你梳理Llama开源家族:从Llama-1到Llama-3,看这一篇就够了!
人工智能·学习·机器学习·大模型·产品经理·llama·uml