当前位置:网站首页>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=
边栏推荐
- MySQL database installation (detailed)
- Workflow番外篇
- [debug highlights] Expected input batch_size (1) to match target batch_size (0)
- 2022-07-30:以下go语言代码输出什么?A:[]byte{} []byte;B:[]byte{} []uint8;C:[]uint8{} []byte;D:[]uin8{} []uint8。
- 信息系统项目管理师核心考点(五十五)配置管理员(CMO)的工作
- MySQL-如何分库分表?一看就懂
- 关于小白安装nodejs遇到的问题(npm WARN config global `--global`, `--local` are deprecated. Use `--location=glob)
- Temporal线上部署
- With MVC, why DDD?
- Numpy中np.meshgrid的简单用法示例
猜你喜欢

MySQL-如何分库分表?一看就懂

On-line monitoring system for urban waterlogging and water accumulation in bridges and tunnels

Sun Wenlong, Secretary General of the Open Atom Open Source Foundation |

Interview | Cheng Li, CTO of Alibaba: Cloud + open source together form a credible foundation for the digital world

CentOS7 install MySQL graphic detailed tutorial

分布式事务——分布式事务简介、分布式事务框架 Seata(AT模式、Tcc模式、Tcc Vs AT)、分布式事务—MQ

MySQL8--Windows下使用压缩包安装的方法

Why use Flink and how to get started with Flink?

CentOS7 安装MySQL 图文详细教程

Temporal介绍
随机推荐
1. Get data - requests.get()
Numpy中np.meshgrid的简单用法示例
Error EPERM operation not permitted, mkdir 'Dsoftwarenodejsnode_cache_cacach Two solutions
.NET-9. A mess of theoretical notes (concepts, ideas)
centos7安装mysql5.7
Pytorch教程Introduction中的神经网络实现示例
ERROR 1819 (HY000) Your password does not satisfy the current policy requirements
Mysql——字符串函数
Create componentized development based on ILRuntime hot update
【ORACLE Explain 详解】
【LeetCode-SQL每日一练】——2. 第二高的薪水
[Cloud Native] DevOps (5): Integrating Harbor
Sun Wenlong, Secretary General of the Open Atom Open Source Foundation |
Three oj questions on leetcode
Reference code series_1. Hello World in various languages
MySQL transaction isolation level, rounding
为什么要用Flink,怎么入门使用Flink?
Interview | Cheng Li, CTO of Alibaba: Cloud + open source together form a credible foundation for the digital world
ERROR 1064 (42000) You have an error in your SQL syntax; check the manual that corresponds to your
面试官,不要再问我三次握手和四次挥手