当前位置:网站首页>Element concatenation operations in numpy and pytorch: stack, concatenat, cat
Element concatenation operations in numpy and pytorch: stack, concatenat, cat
2022-07-31 05:31:00 【Cheng-O】
Splicing of ndarray in numpy
numpy.concatenate
Concatenate multiple matrices along an existing dimension
Requirement: The number of dimensions of the two tensors needs to be the same, and the shapes of other dimensions need to be the same except for the spliced dimension.
np.concatenate((,),axis=)'''The first parameter is a tuple of matrices to be concatenatedThe second parameter is the dimension to be spliced'''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
Stack matrices of the same dimension and shape on a new dimension
Requirement: The matrices have exactly the same dimensions
np.stack((,), axis=)'''The parameters are the same as the function above'''x1 = np.arange(9).reshape((3,3))x2 = np.arange(10,19,1).reshape((3,3))y2 = np.stack((x1,x2),axis=0)output:[[[ 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)
Splicing of ndarrays in pytorch
There are also two functions in pytorch that are the same as the two operations in numpy, namely:
torch.cat((,), dim=) -> numpy.concatenate((,), axis=)
torch.stack((,), dim=) -> numpy.stack((,), axis=)
Difference:
torch concatenates tensor tensors, the second parameter of the function is dim=
numpy concatenates ndarray, the second parameter of the function is axis=
边栏推荐
- 1. 获取数据-requests.get()
- CentOS7 —— yum安装mysql
- .NET-6.WinForm2.NanUI learning and summary
- 工作流编排引擎-Temporal
- On-line monitoring system for urban waterlogging and water accumulation in bridges and tunnels
- MySQL8--Windows下使用压缩包安装的方法
- 关于小白安装nodejs遇到的问题(npm WARN config global `--global`, `--local` are deprecated. Use `--location=glob)
- 剑指offer专项突击版 ---- 第 6 天
- ABC D - Distinct Trio (Number of k-tuples
- Interview Redis High Reliability | Master-Slave Mode, Sentinel Mode, Cluster Cluster Mode
猜你喜欢

DVWA shooting range environment construction

MySQL transaction isolation level, rounding

Linux系统安装mysql(rpm方式安装)
【一起学Rust】Rust学习前准备——注释和格式化输出

Redis Advanced - Cache Issues: Consistency, Penetration, Penetration, Avalanche, Pollution, etc.

Minesweeper game (written in c language)

信息系统项目管理师核心考点(五十五)配置管理员(CMO)的工作

ES source code API call link source code analysis

CentOS7 —— yum安装mysql

Distributed transaction processing solution big PK!
随机推荐
CentOS7 —— yum安装mysql
MySQL(更新中)
SQL语句中对时间字段进行区间查询
MYSQL一站式学习,看完即学完
Kubernetes 证书可用年限修改
MySQL transaction (transaction) (this is enough..)
Mysql application cannot find my.ini file after installation
Flink sink redis 写入Redis
【一起学Rust】Rust的Hello Rust详细解析
C语言教程(二)-printf及c自带的数据类型
为什么要用Flink,怎么入门使用Flink?
Three oj questions on leetcode
Kubernetes加入集群的TOKEN值过期
Flink sink ES 写入 ES(带密码)
工作流编排引擎-Temporal
mysql stored procedure
Refinement of the four major collection frameworks: Summary of List core knowledge
docker安装postgresSQL和设置自定义数据目录
面试官竟然问我怎么分库分表?幸亏我总结了一套八股文
Typec手机有线网卡网线转网口转接口快充方案