当前位置:网站首页>记录贴:pytorch学习Part2
记录贴:pytorch学习Part2
2022-08-08 17:33:00 【安联之夜】
记录贴:pytorch学习Part2
一、合并与切割
#cat
a = torch.rand(4,32,8)
b = torch.rand(5,32,8)
torch.cat([a,b],dim=0)#对第一个维度进行合并,其中其他维度大小需要一致
#stack
a = torch.rand(32,8)
b = torch.rand(32,8)
torch.stack([a,b],dim=0)#两组数据进行堆叠,同时增加一个维度
#split
a = torch.rand(32,8)
c = torch.stack([a,a,a,a],dim=0)
aa,bb = c.split([3,1],dim=0)#把c拆成两份
aa,bb = c.split(2,dim=0)
#chunk
aa,bb = c.chunk(2,dim=0)
二、基本运算
a = torch.rand(3,4)
b = torch.rand(3,4)
torch.all(torch.eq(a+b,torch.add(a,b)))
torch.all(torch.eq(a-b,torch.sub(a,b)))
torch.all(torch.eq(a*b,torch.mul(a,b)))
torch.all(torch.eq(a/b,torch.div(a,b)))
#矩阵乘法
a = torch.tensor([[3.,3.],[3.,3.]])
b = torch.ones(2,2)
torch.mm(a,b)
torch.matmul(a,b)
[email protected]
#次方
a = torch.full([2,2],3)
a.pow(2)
aa = a**2
aa.sqrt()#平方
aa.rsqrt()#平方根倒数
a = torch.exp(torch.ones(2,2))
torch.log(a)
torch.log2(a)
a = torch.tensor(3.14)
a.floor()#向下取整
a.ceil()#向上取整
a.trunc()#取整数
a.frac()#取小数
a.round()#四舍五入
grad = torch.rand(2,3)
grad.max()
grad.median()
grad.clamp(0,10)#超过这个范围的赋值为0或10
三、数据统计
#范数
a = torch.full([8],1.)
b = a.view(2,4)
c = a.view(2,2,2)
a.norm(1)
b.norm(1,dim = 1)
a.prod()#a所有元素的乘积
a.argmax()
a.argmin()
a.max(dim=1,keepdim=True)
a = torch.randn(4,10)
a.topk(4,dim=1)#第一个维度里,最大的四个元素,并返回位置
a.kthvalue(8,dim=1) #第第一个维度里,找到第8小的元素,并返回位置
a > 0
torch.gt(a,0)
四、其他筛选操作
cond = torch.tensor([[0.6769,0.7271],[0.8884,0.4163]])
a = torch.tensor([[0.,0.],[0.,0.]])
torch.where(cond>0.5,a,b)#大于0.5返回a的元素,否则返回b
边栏推荐
猜你喜欢
uri (url urn 的区别)
R文件找不到问题
【DB运营管理/开发解决方案】上海道宁为您提供提高工作便利性的集成开发工具——Orange
正则在js中的使用
KITTI数据集简介(一)—— 传感器介绍
什么是服务网格?在微服务体系中又是如何使用的?
Prometheus+Grafana监控系统
[Paper Reading] RAL 2022: Receding Moving Object Segmentation in 3D LiDAR Data Using Sparse 4D Convolutions
The latest research from PNAS: 81% problem solving rate, neural network Codex opens the door to the world of advanced mathematics
grpc服务发现&负载均衡
随机推荐
三年软件工程真题
L2-024 部落 (25 分)(并查集)
L2-015 互评成绩 (25 分)
L2-016 愿天下有情人都是失散多年的兄妹 (25 分)
并发与并行
The latest research from PNAS: 81% problem solving rate, neural network Codex opens the door to the world of advanced mathematics
爬百度图片
数据库分析与优化
博客主页rrs代码
2.MySQL链接和创建数据库,创建数据表
LeetCode_Binary Tree_Medium_515. Find the maximum value in each tree row
leetcode:306. 累加数
2.5W 字详解线程与锁了,面试随便问!!
Superficial understanding of ports
4. S32K14X study notes: S32 Design Studio new and imported projects
L2-012 关于堆的判断 (25 分)(堆)
L2-028 秀恩爱分得快 (25 分)
grpc服务发现&负载均衡
LeaRun模型驱动开发框架 重塑企业生产力
Reprinted, the fragment speaks very well, the big guy