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>)
相关推荐
江畔柳前堤33 分钟前
roLabelImg 详细安装教程
开发语言·人工智能·后端·云原生
阿里云大数据AI技术41 分钟前
分链路差异化设计的DSP准实时数仓|钛动科技基于阿里云实时计算 Flink 版 + DLF Paimon + EMR Serverless StarRocks 的实践
人工智能·flink
陕西企来客41 分钟前
2026年7月AI智能搜索曝光趋势研判
大数据·人工智能·机器学习·ai智能搜索曝光
05664644 分钟前
agent学习Day15——SQLAlchemy 查询过滤、分页与历史列表接口
python·学习·fastapi
阿里云大数据AI技术1 小时前
从算力到智能体,面向 Agentic AI 的基础设施演进
人工智能·agent
程序员爱德华2 小时前
Python与C++:异同点对比
c++·python
hangyuekejiGEO2 小时前
GEO技术服务选型指南
大数据·人工智能·python
阿里云大数据AI技术2 小时前
EMR Serverless Spark AI Function 的双维降本实践
人工智能·sql·spark
软萌萌的12 小时前
Java Spring Boot 修改yml配置&加载顺序规则
java·spring boot·python
维基框架3 小时前
GitHub源码处理提速 一趟扫描反而更慢
人工智能·github