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

相关推荐
aqi003 小时前
15天学会AI应用开发(七)有了大模型为什么还要引入RAG
人工智能·python·大模型·ai编程·ai应用
用户5191495848454 小时前
libcurl Headers API 释放后重利用漏洞:跨请求复用头句柄导致堆内存安全风险
人工智能·aigc
踩蚂蚁4 小时前
自定义语音唤醒词:从训练到部署的完整链路实践
人工智能
用户5191495848454 小时前
CVE-2025-1094 PostgreSQL SQL注入与WebSocket劫持远程代码执行利用工具
人工智能·aigc
IT_陈寒5 小时前
SpringBoot自动配置这个坑,我踩进去又爬出来了
前端·人工智能·后端
冬奇Lab16 小时前
Agent 系列(23):Web Agent——让 Agent 真正浏览网页
人工智能·llm·agent
冬奇Lab16 小时前
每日一个开源项目(第135篇):codebase-memory-mcp - 给 AI Agent 一张代码库的知识图谱
人工智能·开源·llm