当前位置:网站首页>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=
边栏推荐
- MySQL8.0.26安装配置教程(windows 64位)
- Linux的mysql报ERROR 1045 (28000) Access denied for user ‘root‘@‘localhost‘ (using password NOYSE)
- CentOS7 安装MySQL 图文详细教程
- Summary of MySQL common interview questions (recommended collection!!!)
- 剑指offer专项突击版 ---- 第2天
- Unity resources management series: Unity framework how to resource management
- Flink sink ES 写入 ES(带密码)
- 【一起学Rust】Rust的Hello Rust详细解析
- 12 reasons for MySQL slow query
- Numpy中np.meshgrid的简单用法示例
猜你喜欢
Shell重油常压塔模拟仿真与控制
110 MySQL interview questions and answers (continuously updated)
信息系统项目管理师核心考点(五十五)配置管理员(CMO)的工作
Minesweeper game (written in c language)
CentOS7 —— yum安装mysql
STM32 - DMA
Unity Framework Design Series: How Unity Designs Network Frameworks
有了MVC,为什么还要DDD?
Three oj questions on leetcode
MySQL forgot password
随机推荐
MySQL事务(transaction) (有这篇就足够了..)
ES source code API call link source code analysis
The monitoring of Doris study notes
Simple read operation of EasyExcel
<urlopen error [Errno 11001] getaddrinfo failed>的解决、isinstance()函数初略介绍
A complete introduction to JSqlParse of Sql parsing and conversion
[MQ I can speak for an hour]
可点击也可直接复制指定内容js
Centos7 install mysql5.7 steps (graphical version)
datagrip带参sql查询
SQL行列转换
【一起学Rust】Rust的Hello Rust详细解析
Minesweeper game (written in c language)
On-line monitoring system for urban waterlogging and water accumulation in bridges and tunnels
Minio upload file ssl certificate is not trusted
.NET-9. A mess of theoretical notes (concepts, ideas)
Duplicate entry 'XXX' for key 'XXX.PRIMARY' solution.
一文了解大厂的DDD领域驱动设计
tf.keras.utils.get_file()
Input length must be multiple of 8 when decrypting with padded cipher