PyTorch笔记3----------统计学相关函数

1.基础函数

python 复制代码
import torch
a = torch.rand(2,2)
print("a:\n",a)
print('########################')
print("平均值:\n",torch.mean(a,dim=0))
print("总和:\n",torch.sum(a,dim=0))
print("所有元素的积:\n",torch.prod(a,dim=0))
print("最大值:\n",torch.argmax(a,dim=0))
print("最小值:\n",torch.argmin(a,dim=0))
print("标准差:\n",torch.std(a,dim=0))
print("方差:\n",torch.var(a,dim=0))
print("中位数:\n",torch.median(a,dim=0))
print("众数:\n",torch.mode(a,dim=0))

2.直方图

python 复制代码
import torch
a = torch.rand(2,2) * 10
print("a:\n",a)
print('########################')
# 6为直方图的个数,0为最小值,0为最大值
print(torch.histc(a,6,0,0))

3.频数:输出结果为1~9出现的次数

python 复制代码
import torch
a = torch.randint(0,10,[10])
print("a:\n",a)
print('########################')
print(torch.bincount(a))

4.随机抽样

  • 定义随机种子:torch.manual_seed()
  • 定义随机数满足的分布:torch.normal()
python 复制代码
import torch
torch.manual_seed(1)
mean = torch.rand(1,2)
std = torch.rand(1,2)
a = torch.normal(mean,std)
print(a)

5.范数运算

  • 用来度量某个向量空间(或矩阵)中的每个向量的长度或大小
  • 范数定义需要满足的条件
    • 非负性
    • 齐次性
    • 三角不等式
  • 常用范数有:0范数、1范数、2范数、p范数、核范数
    • torch.dist(input,other,p)
      • 计算两个tensor的p范数
python 复制代码
import torch
a = torch.rand(1,1)
b = torch.rand(1,1)
print("a:\n",a)
print("b:\n",b)
print('l1:\n',torch.dist(a,b,p=1))
print('l2:\n',torch.dist(a,b,p=2))
print('l3:\n',torch.dist(a,b,p=3))
  • torch.norm()
    • 计算某个tensor的范数
python 复制代码
import torch
a = torch.rand(1,1)
print("a:\n",a)
print('l1:\n',torch.norm(a,p=1))
print('l2:\n',torch.norm(a))
print('l3:\n',torch.norm(a,p=3))
print('l3:\n',torch.norm(a,p='fro')) #核范数

知识点为听课总结笔记,课程为B站"2025最新整合!公认B站讲解最强【PyTorch】入门到进阶教程,从环境配置到算法原理再到代码实战逐一解读,比自学效果强得多!":2025最新整合!公认B站讲解最强【PyTorch】入门到进阶教程,从环境配置到算法原理再到代码实战逐一解读,比自学效果强得多!_哔哩哔哩_bilibili

相关推荐
众趣科技38 分钟前
数字孪生重构智慧园区:众趣科技何以成为 VR 园区领域标杆
人工智能·3d·智慧城市·空间计算
心勤则明2 小时前
Spring AI 会话记忆实战:从内存存储到 MySQL + Redis 双层缓存架构
人工智能·spring·缓存
ARM+FPGA+AI工业主板定制专家4 小时前
基于GPS/PTP/gPTP的自动驾驶数据同步授时方案
人工智能·机器学习·自动驾驶
长鸳词羡4 小时前
wordpiece、unigram、sentencepiece基本原理
人工智能
ㄣ知冷煖★4 小时前
【GPT5系列】ChatGPT5 提示词工程指南
人工智能
科士威传动4 小时前
丝杆支撑座在印刷设备如何精准运行?
人工智能·科技·自动化·制造
taxunjishu5 小时前
DeviceNet 转 Modbus TCP 协议转换在 S7-1200 PLC化工反应釜中的应用
运维·人工智能·物联网·自动化·区块链
kalvin_y_liu6 小时前
智能体框架大PK!谷歌ADK VS 微软Semantic Kernel
人工智能·microsoft·谷歌·智能体
爱看科技6 小时前
智能眼镜行业腾飞在即,苹果/微美全息锚定“AR+AI眼镜融合”之路抢滩市场!
人工智能·ar
汇能感知8 小时前
光谱相机的探测器阵列
经验分享·笔记·科技