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

相关推荐
OpenBayes贝式计算11 分钟前
教程上新丨基于500万小时语音数据,Qwen3-TTS实现3秒语音克隆及精细调控
人工智能·深度学习·机器学习
CoovallyAIHub24 分钟前
开源:YOLO最强对手?D-FINE目标检测与实例分割框架深度解析
人工智能·算法·github
用户2576595759091 小时前
当人类知识学会自己奔跑--skill
人工智能
个入资料3 小时前
阿里云ecs+飞书搭建openclaw
人工智能
孤烟4 小时前
【RAG 实战系列 02】检索精度翻倍!混合检索(稀疏 + 稠密)实战教程
人工智能·llm
明明如月学长4 小时前
OpenClaw 帮我睡后全自动完成了老板交代的任务
人工智能
uuware5 小时前
Lupine.Press + AI 助您分分钟搞定技术项目的文档网站
人工智能·前端框架
海上日出5 小时前
使用 QuantStats 进行投资组合绩效分析:Python 量化实战指南
人工智能
Qinana5 小时前
150行代码搞定私有知识库!Node.js + LangChain 打造最小化 RAG 系统全流程
人工智能·程序员·node.js
猿猿长成记5 小时前
AI专栏 | AI大法则之思维链、自洽性、思维树
人工智能