当前位置:网站首页>7.合并与分割
7.合并与分割
2022-07-27 05:13:00 【派大星的最爱海绵宝宝】
merge合并
torch.cat/torch.stack
1.cat
第一个参数是合并的两个tensor,第二个参数决定在哪个维度上合并
合并的维度可以不一样,但是其他属性要一样。
某个tensor是4行4列,dim=0时,竖着拼接;dim=1,横着拼接。
aa=torch.rand(5,32,8)
bb=torch.rand(4,32,8)
cc=torch.cat([aa,bb],dim=0)
print(cc.shape)
aa[5,32,8],b[4,32,8],保证后面两个维度相同

若是a[4,32,4],b[5,32,8],cat之前,需要先把a的最后一个4补充0到8,然后再拼接成[9,32,8]。
2.stack
可以完成某种意义上的cat,但是会创建一个新的维度。该新的维度的意义由具体的实际问题决定。
每个维度都需要相同。
aa=torch.rand(4,3,16,32)
bb=torch.rand(4,3,16,32)
cc=torch.stack([aa,bb],dim=0)
dd=torch.stack([aa,bb],dim=2)
print(cc.shape)
print(dd.shape)
stack后[4,3,2,16,32],因为stack的地方是16处,在16前面插入新的维度,当新维度取0时,16代表着a的16,当新维度取1时,16代表着b的16,a和b的第2个维度都是16。

a=torch.rand(32,8)
b=torch.rand(32,8)
c=torch.stack([a,b],dim=0)
print(c.shape)
假设这是两个班级的成绩,当两个班级的成绩合并后,成绩一起,但是依然属于不同的班级,此时的新的维度代表着不同的班级,意味着有2个班级,每个班级有32个人,,每个人有8门课程。
split分割
.split()/.chunk()
1.split
按长度拆分,
根据单元长度拆分,第一个参数是每个单元的长度是多少,第二个是要拆分的维度。
a=torch.rand(3,32,8)
b,c=a.split([2,1],dim=0)
d,e=b.split(1,dim=0)
print(b.shape)
print(c.shape)
print(d.shape)
print(e.shape)
[3,32,8]拆分成[2,32,8]和[1,32,8],这里的split([2,1],dim=0),取2个拆成一个,再取1个拆成一个。
长度不一样时,写成list形式,如[3,1,2],若都是相同的长度,可以直接写一个数,如2。

a=torch.rand(2,32,8)
b,c=a.split(2,dim=0)
这个错的原因是:
原有意思是,拆分成n块,每块长度为2。对于两个班级,我们不能拆分成长度为2的1块。

2.chunk
按数量拆分
a=torch.rand(3,32,8)
b,c=a.chunk(2,dim=0)
print(b.shape)
print(c.shape)
按照数量2拆分,即使第二个tensor数量不够2,也可以拆分。

边栏推荐
- How to open a general commodity futures account
- GBASE 8C——SQL参考6 sql语法(11)
- 如果面试官问你 JVM,额外回答“逃逸分析”技术会让你加分
- Move protocol launched a beta version, and you can "0" participate in p2e
- Seektiger's okaleido has a big move. Will the STI of ecological pass break out?
- 贪心高性能神经网络与AI芯片应用研修
- GBASE 8C——SQL参考6 sql语法(4)
- Docker deploys the stand-alone version of redis - modify the redis password and persistence method
- PHP的CI框架学习
- 2021中大厂php+go面试题(1)
猜你喜欢

【并发编程系列9】阻塞队列之PriorityBlockingQueue,DelayQueue原理分析

Personal collection code cannot be used for business collection

PHP 实现与MySQL的数据交互

新冠时空分析——Global evidence of expressed sentiment alterations during the COVID-19 pandemic

19.上下采样与BatchNorm

Day 7. Towards Preemptive Detection of Depression and Anxiety in Twitter

Sealem Finance - a new decentralized financial platform based on Web3

Sequel Pro下载及使用方法
基于PG-Oracle和MySQL的三库通用sql代码开发

手把手教你搭建钉钉预警机器人
随机推荐
记一次PG主从搭建及数据同步性能测试流程
jenkins构建镜像自动化部署
Minio fragment upload lifting fragment size limit - chunk size must be greater than 5242880
Choose a qualified futures company to open an account
dbswitch数据迁移数据增量时如何不覆盖目标源数据
基于深度神经网络的社交媒体用户级心理压力检测
kettle如何处理文本数据传输为‘‘而不是null
Okaleido launched the fusion mining mode, which is the only way for Oka to verify the current output
golang中slice切片使用的误区
使用Docker部署Redis进行高可用主从复制
MySQL索引优化相关原理
dpdk 网络协议栈 vpp OvS DDos SDN NFV 虚拟化 高性能专家之路
Move protocol launched a beta version, and you can "0" participate in p2e
MySQL索引分析除了EXPLAIN还有什么方法
GBASE 8C——SQL参考6 sql语法(11)
The main advantage of face brushing payment users is their high degree of intelligence
Face brushing payment will never be out of date, but will continue to change
Web3 traffic aggregation platform starfish OS interprets the "p2e" ecosystem of real business
【mysql学习】8
GBASE 8C——SQL参考6 sql语法(3)