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 分钟前
火山引擎 × 央视网 打造 2026 世界杯沉浸式观赛盛宴
人工智能·音视频开发
夏季疯38 分钟前
读论文:STARS 是什么结构?一个统一的歌声自动标注框架
python
IT_陈寒43 分钟前
Java线程池这个坑我算是踩明白了
前端·人工智能·后端
创安达科技44 分钟前
2026 市政太阳能警示设施采购,多维度设备性能评估参考
人工智能·太阳能道钉灯·创安达
isyangli_blog1 小时前
西钻—— 图影智绘创作课堂 (豆包、千问)
人工智能
影山飞雄1 小时前
Systematic-Debugging skill深度解析
人工智能
光测实验室1 小时前
3种Python降噪算法实测:我把处理速度提升了20倍
python
AOwhisky2 小时前
Python 学习笔记(第三期)——流程控制核心知识点自测与详解
开发语言·笔记·python·学习·云原生·运维开发·流程控制
tyqtyq222 小时前
药品说明书解读 —— AI 安全用药助手,鸿蒙原生应用深度解析
人工智能·学习·华为·生活·harmonyos
feiyu_gao2 小时前
别再让 AI 从零开始了
人工智能·架构·aigc