当前位置:网站首页>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,也可以拆分。

边栏推荐
- 身为技术管理者应该具备的素质(猜想)
- Minio8.x version setting policy bucket policy
- 你真的了解 Session 和 Cookie 吗?
- How to not overwrite the target source data when dBSwitch data migrates data increments
- 记一次PG主从搭建及数据同步性能测试流程
- 神芷迦蓝寺
- Mysql5.7版本如何实现主从同步
- Graph node deployment
- Choose a qualified futures company to open an account
- Analyze the maker education DNA needed in the new era
猜你喜欢

Deploy redis with docker for high availability master-slave replication

Day 6.重大医疗伤害事件网络舆情能量传播过程分析*———以“魏则西事件”为例

给测试小姐姐的第三封信 | ORACLE存储过程知识分享和测试说明

Seektiger will launch STI fusion mining function to obtain Oka pass

Okaleido launched the fusion mining mode, which is the only way for Oka to verify the current output

我想不通,MySQL 为什么使用 B+ 树来作索引?

Build a complete system in the maker education movement

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

Seven enabling schemes of m-dao help Dao ecology move towards mode and standardization

NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
随机推荐
Activity之应用进程创建流程简析
kettle如何处理文本数据传输为‘‘而不是null
dpdk 网络协议栈 vpp OvS DDos SDN NFV 虚拟化 高性能专家之路
Personal collection code cannot be used for business collection
PHP的CI框架学习
Minio fragment upload lifting fragment size limit - chunk size must be greater than 5242880
GBASE 8C——SQL参考6 sql语法(15)
GBASE 8C——SQL参考6 sql语法(6)
2020年PHP中级面试知识点及答案
2022/7/26 考试总结
Move protocol launched a beta version, and you can "0" participate in p2e
The NFT market pattern has not changed. Can okaleido set off a new round of waves?
Handler操作记录 Only one Looper may be created per thread
Dimitra and ocean protocol interpret the secrets behind agricultural data
Jenkins build image automatic deployment
Choose a qualified futures company to open an account
Day 9. Graduate survey: A love–hurt relationship
Cap principle
The main advantage of face brushing payment users is their high degree of intelligence
使用Docker部署Redis进行高可用主从复制