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>)
相关推荐
lilihuigz2 分钟前
WordPress AI代理:开源CMS如何成为智能网络操作系统的核心驱动力 - WP站长
人工智能·开源·cms
不知名的老吴8 分钟前
一文看懂:针对大语言模型的提示注入攻击
人工智能·语言模型·自然语言处理
yantaohk11 分钟前
PCDN还能赚钱吗?普通人用闲置宽带赚钱的机会、收益和风险分析
大数据·人工智能·内容运营
唐叔在学习16 分钟前
TodoList:我的待办助手
python·程序员·ai编程·待办应用
小妖同学学AI21 分钟前
天啊!现在连AI团队都能一键“召唤”了?!零代码搞定智能应用开发!
人工智能
MediaTea23 分钟前
AI 术语通俗词典:ID3 算法
人工智能·算法
薛定猫AI26 分钟前
【深度解析】Open Design 本地优先 AI 设计系统:用多模型 Agent 生成高保真 UI 原型
人工智能·ui
Old Uncle Tom28 分钟前
《企业AI成功部署实战指南:51 次成功部署的经验教训》给我们的启发
人工智能
卷卷说风控35 分钟前
【卷卷观察】Agent Skills 为什么突然火了?我花了一晚上研究,结论有点反直觉
人工智能
deephub42 分钟前
BM25 + Vectors:为什么真实 RAG 系统通常两者都需要
人工智能·大语言模型·向量检索·rag·bm25