当前位置:网站首页>numpy和pytorch中的元素拼接操作:stack,concatenat,cat
numpy和pytorch中的元素拼接操作:stack,concatenat,cat
2022-07-31 05:09:00 【Cheng-O】
numpy中对ndarray的拼接
numpy.concatenate
将多个矩阵沿着一个已经存在的维度进行拼接
需求:两个张量的维度的数量需要相同,同时除了拼接的维度,其它维度的形状需要相同。
np.concatenate((,),axis=)
'''
第一个参数为要拼接的矩阵组成的元组
第二个参数为要拼接的维度
'''
a = np.arange(3*3).reshape((3,3))
b = np.arange(3*4).reshape((3,4))
a,b
(array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]]),
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]]))
np.concatenate([a,b],axis=1)
array([[ 0, 1, 2, 0, 1, 2, 3],
[ 3, 4, 5, 4, 5, 6, 7],
[ 6, 7, 8, 8, 9, 10, 11]])
numpy.stack
将具有相同维度和形状的矩阵,在一个新的维度上进行堆叠
需求:矩阵具有完全相同的尺寸
np.stack((,), axis=)
'''
参数和上面的函数相同
'''
x1 = np.arange(9).reshape((3,3))
x2 = np.arange(10,19,1).reshape((3,3))
y2 = np.stack((x1,x2),axis=0)
输出:
[[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]]
[[10 11 12]
[13 14 15]
[16 17 18]]]
'y2.shape': (2,3,3)
PS:
np.hstack(tup) = np.concatenate(tup, axis=1)
np.vstack(tup) = np.concatenate(tup, axis=0)
pytorch中对ndarray的拼接
pytorch中同样存在两个函数和numpy中的两种操作相同,分别为:
torch.cat((,), dim=) -> numpy.concatenate((,), axis=)
torch.stack((,), dim=) - > numpy.stack((,), axis=)
区别:
torch对tensor张量进行拼接,函数的第二个参数为dim=
numpy对ndarray进行拼接,函数的第二个参数为axis=
边栏推荐
- DVWA shooting range environment construction
- MySQL(更新中)
- wx.miniProgram.navigateTo在web-view中跳回小程序并传参
- ABC D - Distinct Trio (Number of k-tuples
- MySQL8.0安装教程,在Linux环境安装MySQL8.0教程,最新教程 超详细
- 对list集合进行分页,并将数据显示在页面中
- mysql uses on duplicate key update to update data in batches
- A complete introduction to JSqlParse of Sql parsing and conversion
- ERROR 1064 (42000) You have an error in your SQL syntax; check the manual that corresponds to your
- ERP Production Operation Control Kingdee
猜你喜欢
MySQL database addition, deletion, modification and query (detailed explanation of basic operation commands)
MySQL transaction isolation level, rounding
sql statement - how to query data in another table based on the data in one table
【mysql 提高查询效率】Mysql 数据库查询好慢问题解决
快速掌握并发编程 --- 基础篇
mysql存储过程
【一起学Rust】Rust学习前准备——注释和格式化输出
DVWA靶场环境搭建
Multiple table query of sql statement
DVWA安装教程(懂你的不懂·详细)
随机推荐
MySQL forgot password
MySQL事务(transaction) (有这篇就足够了..)
ERP Production Operation Control Kingdee
Multiple table query of sql statement
Interview Redis High Reliability | Master-Slave Mode, Sentinel Mode, Cluster Cluster Mode
110 MySQL interview questions and answers (continuously updated)
Information System Project Manager Core Test Site (55) Configuration Manager (CMO) Work
面试Redis 高可靠性|主从模式、哨兵模式、Cluster集群模式
With MVC, why DDD?
[mysql improves query efficiency] Mysql database query is slow to solve the problem
mysql5.7.35安装配置教程【超级详细安装教程】
快速掌握并发编程 --- 基础篇
面试官,不要再问我三次握手和四次挥手
Simple read operation of EasyExcel
mysql存储过程
wx.miniProgram.navigateTo在web-view中跳回小程序并传参
Unity mobile game performance optimization series: performance tuning for the CPU side
sql语句之多表查询
城市内涝及桥洞隧道积水在线监测系统
Centos7 install mysql5.7 steps (graphical version)